Code Monkey home page Code Monkey logo

js-sequence-diagrams's Introduction

JS Sequence Diagrams Bower Build Status Code Climate Libraries.io License

Generates UML sequence diagrams from simple text
https://bramp.github.io/js-sequence-diagrams/

by Andrew Brampton 2012-2017

Example

We turn

Alice->Bob: Hello Bob, how are you?
Note right of Bob: Bob thinks
Bob-->Alice: I am good thanks!

into

Sample generated UML diagram

Requirements

You will need Snap.svg, Web Font Loader (if you wish to use custom fonts), underscore.js (or lodash), and optionally jQuery.

Installation

bower

Run bower install bramp/js-sequence-diagrams and include the scripts below:

<script src="{{ bower directory }}/bower-webfontloader/webfont.js" />
<script src="{{ bower directory }}/snap.svg/dist/snap.svg-min.js" />
<script src="{{ bower directory }}/underscore/underscore-min.js" />
<script src="{{ bower directory }}/js-sequence-diagrams/dist/sequence-diagram-min.js" />

also import the CSS if you plan to use the hand drawn theme:

<link href="{{ bower directory }}/js-sequence-diagrams/dist/sequence-diagram-min.css" rel="stylesheet" />

Not using bower? No problem. Just download the dependencies, and include them yourself. If you plan to use the hand draw theme, don't forget to put the two fontfiles in your css folder: /fonts/daniel/danielbd.woff and /fonts/daniel/danielbd.woff2

Usage

You can use the Diagram class like:

<div id="diagram">Diagram will be placed here</div>
<script> 
  var d = Diagram.parse("A->B: Does something");
  var options = {theme: 'simple'};
  d.drawSVG('diagram', options);
</script>

or use jQuery to do all the work:

<script src="{{ bower directory }}/jquery/dist/jquery.min.js" />
<div class="diagram">A->B: Message</div>
<script>
  var options = {theme: 'hand'};
  $(".diagram").sequenceDiagram(options);
</script>

For full examples check out the demo site.

Options

var options = {
    // Change the styling of the diagram, typically one of 'simple', 'hand'. New themes can be registered with registerTheme(...).
    theme: string,

    // CSS style to apply to the diagram's svg tag. (Only supported if using snap.svg)
    css_class: string,
};

Styling

The following CSS classes are applied to the SVG diagram when using snap.svg:

  • sequence: Applies to main SVG tag.
  • title: Applied to the title of the diagram.
  • actor: Applied to the actors.
  • signal: Applied to the signals.
  • note: Applied to all notes.

The diagram can then be customised, for example:

.signal text {
    fill: #000000;
}
.signal text:hover {
    fill: #aaaaaa
}
.note rect, .note path {
    fill: #ffff00;
}
.title rect, .title path,
.actor rect, .actor path {
    fill: #ffffff
}

Raphaël Deprecation

Version 1.x of this library used Raphaël for drawing the diagrams, however, Raphaël had some limitations, and since disappeared from the Internet. We've decided to move to Snap.svg, which is a pure SVG implementation, instead of Raphaël which in addition to SVG, also supported VML (on Internet Explorer). This support of VML made it impossible to use some newer SVG capabilities. Native SVG allows us to use CSS styling, better font support, animations and more.

To aid in the transition Version 2.x will support both Raphaël and Snap.svg (preferring Snap.svg). If you include Raphaël instead of snap.svg, it will default to using Raphaël as the rendering library. For example

<script src="{{ bower directory }}/raphael/raphael-min.js"></script>

There are also four transitional themes, 'snapSimple', 'snapHand', 'raphaelSimple', 'raphaelHand', which force the use of either Snap.svg, or Raphaël.

The plan is to drop support for Raphaël in a future release, simplifying the library, and reducing the file size.

Adding a Font

Raphael requires Cufon style fonts. Find the font you want in ttf or otf format, visit Cufon's site and process it into a javascript file. Then ensure the font is included via the HTML, or recompile after altering main.js. So far only the hand drawn font, Daniel Bold, has been included.

Build requirements

The build is managed by a Makefile, and uses various tools available from npm. Thus both make and npm are required, and can easily be installed on any Linux or Mac machine.

make

The Makefile will use npm to install all the dev dependencies, build, and test.

Testing

We use qunit for testing. It can be ran from the command line, or via a browser. The command line actually tests multiple permutations of lodash, Underscore, and with and without minification.

make test
...
Global summary:
┌───────┬───────┬────────────┬────────┬────────┬─────────┐
│ Files │ Tests │ Assertions │ Failed │ Passed │ Runtime │
├───────┼───────┼────────────┼────────┼────────┼─────────┤
│ 1     │ 13    │ 231        │ 0      │ 231    │ 250     │
└───────┴───────┴────────────┴────────┴────────┴─────────┘

or make and then open test/qunit.html in a browser. Finally a simple playground is available at test/test.html.

How to release

  • Make sure all changes checked in
  • Bump version in src/main.js and bower.json
  • make clean
  • make
  • git add -f src/main.js bower.json dist/*
  • git commit -m "Released version 2.x.x"
  • git push origin master
  • git tag -a v2.x.x -m v2.x.x
  • git push origin v2.x.x

TODO

  • Other themes

  • Automate the release process

  • Testing that checks the generated SVG is correct

  • Improve the hand drawn theme

    • "Note left of Bob: " generates a small empty box.
    • The font seems to have extra margin at the bottom.
    • The wiggly lines don't always touch.
  • Dozens of other issues on https://github.com/bramp/js-sequence-diagrams/issues

Contributors

via GitHub

Thanks

This project makes use of Jison, snap.svg, underscore.js, and the awesome Daniel font (which is free to use for any purpose).

Many thanks to Web Sequence Diagrams which greatly inspired this project, and forms the basis for the syntax.

Related

Licence (Simplified BSD License)

Copyright (c) 2012-2017, Andrew Brampton All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

js-sequence-diagrams's People

Contributors

ajbogh avatar bennycode avatar bramp avatar davidw avatar eloyesp avatar magnars avatar masonm avatar orthographic-pedant avatar qur2 avatar raboof avatar scopicisco avatar stefrave avatar vaibhavvashishtha avatar vermeerdirk avatar vitoravelino avatar workroomprds avatar ziluvatar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-sequence-diagrams's Issues

Should consider Entity-Relationship diagram

According to the readme file of the project, the fundamental goal of the library is drawing sequence diagrams, but I think you should seriously consider entity-relationship diagrams and such since you run through the problem of connecting textboxes with lines and arrows. It would be nice to have a tool such as this one for complete uml. It doesn't have to be the same labrary, it could also be as a spin-off of the project or expand the reach of the projects.

What do you think? Is it possible given the current architecture of software? Have you considered before?

Printing with PhantomJS 1.9.1

Hi,

I'm not sure if this is an issue with js-sequence-diagrams, Raphael, PhantomJS or with my diagram, but I'm getting errors when using PhantomJS to print a page with a js-sequence-diagrams generated diagram.

Sample command:

phantomjs rasterize.js file:///path/to/seq-diag.html test.pdf

I was originally rasterizing/printing a HTML document that used js-sequence-diagrams. That was quite a large document, so I pulled out one of the generated SVG elements and gradually removed sub-elements until I was getting the error with the minimum SVG diagram.

seq-diag.html

<!DOCTYPE html>

<html>

<body>

<script src="../src/site/resources/lib/jquery-1.9.1.js"></script>

<script src="../src/site/resources/lib/raphael-min.js"></script>
<script src="../src/site/resources/lib/underscore-min.js"></script>
<script src="../src/site/resources/lib/sequence-diagram-min.beautified.js"></script>

<pre class="sequence-diagram">
Andrew->China: Says Hello
Note right of China: China thinks\nabout it
China-->Andrew: How are you?
Andrew->>China: I am good thanks!
</pre>

<script>
(function() {
    console.log("Before render");
    try {
        $(".sequence-diagram").sequenceDiagram({ theme: "simple" });
        console.log("Render success");
    } catch (e) {
        console.log(e);
    }
}());
</script>

</body>

</html>

seq-diag.html error

adobe-reader-drawing-error

simple2.svg

Command:

phantomjs rasterize.js file:///path/to/simple2.svg test.pdf

SVG:

<!--
Is this the minimum diagram that fails to load in Adobe Reader?
-->
<svg style="overflow: hidden; position: relative;" height="318" version="1.1" width="475" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <desc style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">
    Created with Raphael 2.1.0</desc>
    <defs style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">
        <path style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block">
        </path>
        <marker style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" id="raphael-marker-endblock55" markerHeight="5" markerWidth="5" orient="auto" refX="2.5" refY="2.5">
            <use style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" xlink:href="#raphael-marker-block" transform="rotate(180 2.5 2.5) scale(1,1)" stroke-width="1.0000" fill="#000" stroke="none">
            </use>
        </marker>
    </defs>
    <path style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" fill="none" stroke="#000000" d="M50,96C50,96,204.95384871959686,96,234.99128912080778,96" stroke-width="2" marker-end="url(#raphael-marker-endblock55)" stroke-dasharray="0">
    </path>
</svg>

Add multi-line notes

WSD supports multi-line notes which is nice to have for longer text. Can it get supported pls? :)

`make test` broken

when I run make test I'll get this:

$ make test
node grammar.js test

fs.js:381
  var r = binding.read(fd, buffer, offset, length, position);
                  ^
Error: EISDIR, illegal operation on a directory
    at Object.fs.readSync (fs.js:381:19)
    at Object.fs.readFileSync (fs.js:208:28)
    at Object.commonjsMain [as main] (/Users/tobi/Projects/js/js-sequence-diagrams/grammar.js:394:32)
    at Object.<anonymous> (/Users/tobi/Projects/js/js-sequence-diagrams/grammar.js:398:11)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
make: *** [test] Error 1

any idea what's wrong ? thx

Can Participant aliases support newlines?

Hi,

If I use the code:

Participant sample\nmodule

It looks like this (this is the behaviour I want):

sample-module-1

If I use the code:

Participant sample\nmodule as module

It looks like this (this is not the behaviour I want, I would like the image to appear as above, but I can use the alias in the rest of the diagram):

sample-module-2

Is this a bug or behaving as expected?

Thanks.

Any plans to support producing sequence diagrams from 'real' code

For example,

actor.y      = offsetY;
actor.height = height;
this.draw_text_box(actor, actor.name, ACTOR_MARGIN, ACTOR_PADDING, this._font);

Would produce a sequence diagram roughly equivalent to

this->actor: y
this->actor: height
this->this: draw_text_box

Obviously this means being able to parse multiple languages... Java, Javascript, Ruby, C# etc...

Would be great to be able to copy/paste code into the webpage to see how the interactions look... long term, would be great to have the feature built into the IDE, "view as sequence diagram"

Theming Approach

I've been looking at how to approach themes and wondered if you had any thoughts on it so far. I can think of a couple of approaches:

  1. Extend the existing theme approach but allow more flexibility with line colours, fills etc.
  2. Add CSS classes to the individual elements e.g. title, actor, signal, note etc. and use external style sheets to add fill and stroke colours.

My thoughts are that option 1 might become unwieldy as the number of theme options grows larger whereas option 2 keeps a nice separation of concerns. The jQuery initialisation would then look something like this:

$(function() {
    $(".sequence").sequenceDiagram({linestyle: 'hand',themecss:'css/mytheme.css'}); 
});

Any thoughts?

Themes

Provide a variety of themes with colors (in case it's easy to do).

Issue running make

I've got to say that this is a great and truly useful library - good work!

I have forked and run make against a new local feature branch.

Running test/test.html works fine as is but if I change it to use sequence-diagram-min.js I get the following JS error in the console:
Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.

Any ideas? The same happens if I try and use the minified version in my own project.

Export as image

Would it be possible to export the result as PNG image like the svg export ?

Compatibility with UML spec

I am not a connaisseur of UML at all but according to Wikipedia “… solid arrows with full heads are synchronous calls, solid arrows with stick heads are asynchronous calls and dashed arrows with stick heads are return messages.”

I currently see no option to draw a dashed arrow with stick heads, or is there?

Scale SVG to fit in container?

It seems to create a fixed width svg. If there isn't, how about adding an option to make it scale to the width of the container?

Dead link on http://bramp.github.io/js-sequence-diagrams/

In the Usage section of your website the line

<script src="sequence-diagram-min.js"></script>

has a dead link to https://raw.github.com/bramp/js-sequence-diagrams/master/sequence-diagram-min.js

This makes it a bit hard to integrate your software into sites and caused some headache and frustration to me.

Using

https://raw.github.com/bramp/js-sequence-diagrams/master/build/sequence-diagram-min.js

should work instead, but probably you have a better link.

Onclick event?

hello it would be nice to be able to have an onclick event

[Suggestion] Single actor action

I would like to suggest this:

Actor: action

could be shorthand for this:

Note over Actor: action

That gives single-actor actions using the same syntax as two-actor actions.

Better doc for sarting.

Hi,

Just found this project and I think it's awesome!
I just had it running but was painful: Makefile not working (on OSX with latest gem and node modules).
I had to generate the js file as an independent step and then feed it to uglifyjs to avoid this error:

WARN: ERROR: Unexpected token eof «undefined», expected punc «(» [-:462,6]

/usr/local/share/npm/lib/node_modules/uglify-js/lib/parse.js:199
    throw new JS_Parse_Error(message, line, col, pos);

The doc should state more clearly what js library is needed (maybe a full html page with libs from cdnjs?)
Also, running lodash instead of underscore is not possible, the example code breaks because of the _.each iterating on an array starting from offset 1 and not 0 (cf. the distances of an actor).

Will try to issue a PR, but it will take some time.

Color ?

Only black and white so far?
no colors available?

Getting the title

This is more of a question - after you do diagram.drawSVG() how do I get the title of the diagram in a variable? I want to change the window title with it to work on multiple sequence diagram.

Thanks.

Support for operators

Is there a support for some operations?
Something like:

Alice->Bob: Authentication Request
note right of Bob: Bob thinks about it
Bob->Alice: Authentication Response
alt text1
    A->B: text
else text2
    A->B: text
end
opt text
    A->B: text
end
loop text
    A->B: text
end

parallel message flows

Great project.

I'd like to have the parallel message flows, in some way.

For the following example:
A->B:hello
B->A:hello_ack
C->D:hello
D->C:hello_ack

The communications between C and D do not depend on A and B, so communications between C and D do not necessarily happen after that between A and B. And in the generated chart, the first hello and the second hello may be side by side, not necessary from top to down.

Install via npm and bower

Would be nice to have js-sequence-diagrams available via npm and bower so we don't have to manually download/update the library.

inline formatting

I'm missing inline formatting. Is it hard to implement that?

Inspired by yuml.me, I'm thinking of something like the below to color a note with a grey background.

Alice->Bob: Hello Bob, how are you?
Note{bg:#333} right of Bob: Bob thinks
Bob-->Alice: I am good thanks!

Invalid character '\u0171'

While following the docs, I was using HTML/JavaScript locally and having problems loading sequence-diagram-min.js.

Chrome was saying Uncaught SyntaxError: Unexpected token ILLEGAL and Safari was saying SyntaxError: Invalid character '\u0171'.

I had to change the script tag from <script src="sequence-diagram-min.js"></script> to <script src="sequence-diagram-min.js" charset="UTF-8"></script>

Might be helpful to change that in the docs, but not sure if I am n00bing things up in some other way with my local non-webserver import.

Thanks for the library and the time you put into it, it's great.

-Ben

plantuml?

Just wondering, are you aware of PlantUML? It's the tool I've been using for the longest time and is fairly comprehensive. It would be nice to have compatible syntax as there is support for it in Eclipse, IDEA, etc.

Spaces before -> or other arrows

Try out these two samples. This generates the desired sequence diagram:

Jim->  John: Hi
John-> Jim: Hi

But I like to whitespaceify things so that the code reads more nicely:

Jim  -> John: Hi
John -> Jim: Hi

This code creates actors named Jim__ and John_ (underscores represent spaces) in addition to the Jim and John created on the right-hand sides.

In short, it seems like space is stripped from the front of actor names, but not the end. Sorry if this has been raised before - I dug through the closed issues and couldn't see anything that looked similar.

Fix the Makefile to run on the mac.

As reported in #1

I just had it running but was painful: Makefile not working (on OSX with latest gem and node modules).
I had to generate the js file as an independent step and then feed it to uglifyjs to avoid this error:

WARN: ERROR: Unexpected token eof «undefined», expected punc «(» [-:462,6]

/usr/local/share/npm/lib/node_modules/uglify-js/lib/parse.js:199
     throw new JS_Parse_Error(message, line, col, pos);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.