1.0.3 • Published 5 years ago

fa-diagrams v1.0.3

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

npm version Build Status Coverage Status

fa-diagrams

SVG diagrams built from Font-Awesome icons

sample

Web Editor

Summary

Usage

back to top

Node module

const faDiagrams = require('fa-diagrams');

// data: example1
const data = {
  "nodes": [
    {
      "name": "node1",
      "icon": "laptop-code",
      "color": "#4E342E",
      "bottom": "my app"
    },
    {
      "name": "node2",
      "icon": "globe",
      "color": "#455A64",
      "bottom": "world"
    }
  ],
  "links": [
    {
      "from": "node1",
      "to": "node2",
      "color": "#333333",
      "top": {
        "icon": "envelope"
      },
      "bottom": "\"hello\""
    }
  ]
}

const svg = faDiagrams.compute(data); // string containing xml data

//export as SVG with fs
const fs = require('fs');
fs.writeFileSync('diagram.svg', svg, {encoding:'utf-8'});

//export as PNG with svg2img (npm i svg2img)
const svg2img = require('svg2img');
svg2img(svg, function(error, buffer) {
    fs.writeFileSync('diagram.png', buffer);
});

Will produce the diagram above

Html script

<head>
    ...
    <script src="fa-diagrams-data.min.js"></script>
    <script src="fa-diagrams.min.js"></script>
    ...
</head>
<body>
...
<script>
    ...
    const svg = faDiagrams.compute(data); // string containing xml data
    ...
</script>
...
</body>
</html>

Install

back to top

Static scripts

Get the scripts from this repository dist folder

<script src="fa-diagrams-data.min.js"></script> <!-- 1.04 MB -->
<script src="fa-diagrams.min.js"></script> <!-- 110.40 KB -->

CDN

<script src="https://cdn.jsdelivr.net/npm/fa-diagrams@latest/dist/fa-diagrams-data.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fa-diagrams@latest/dist/fa-diagrams.min.js"></script>

or

<script src="https://unpkg.com/fa-diagrams/dist/fa-diagrams-data.min.js"></script>
<script src="https://unpkg.com/fa-diagrams/dist/fa-diagrams.min.js"></script>

NPM

npm install fa-diagrams

Build from sources

You will need subversion installed (used for precise folder fetching in GitHub)

git clone https://github.com/klemek/fa-diagrams.git
cd fa-diagrams
npm install
node build.sh

API

back to top

You must pass as argument an object containing 3 keys:

options

Key (.subkey)Default valueInfoRedefined
placing.max-link-length3maximum stretching of links between nodesno
placing.diagonalstrueallow diagonal links to be madeno
rendering.beautifyfalseoutput a readable SVG fileno
rendering.scale256(in pixels) final icons sizeno
rendering.colorblackcolor of all elementsno
rendering.h-spacing1.3how width is stretched comparing to heightno
rendering.icons.scale1default scaling of iconsin node
rendering.icons.color''color of all iconsin node
rendering.sub-icons.scale0.4default scaling of sub-iconsin sub-icon
rendering.sub-icons.color''color of all sub-iconsin sub-icon
rendering.sub-icons.margin0.3margin between sub-icons and elementsin sub-icon
rendering.links.scale1default scaling of linksin link
rendering.links.color''color of all links (might be redefined in link definition)in link
rendering.links.size0forced size/length of the links (0 means it will be computed from the distance between the nodes)in link
rendering.texts.font'Arial'font family of the texts (might be redefined in sub-elements definition)in text
rendering.texts.font-size15font size of the textsin sub-text
rendering.texts.font-style'normal'font style of the texts (see Font styles)in sub-text
rendering.texts.color''color of all textsin sub-text
rendering.texts.margin0.2margin between texts and elementsin sub-text
rendering.texts.line-height1.2height of each line in font sizein sub-text

nodes

Array of object as following:

KeyTypeRequiredInfo
namestringyesused in links to reference nodes
iconstring/objectyesname of the Font-Awesome icon of the node (see Icon names)
top, bottom, left, rightstring or objectnosee Sub-elements
colorstringnoredefined the color
scalenumbernoredefine this node icon scale
x, ynumbernoforce the position of this node

links

Array of object as following:

KeyTypeRequiredInfo
fromstringyessource node name
tostringyesdestination node name
typestringnolink's appearance (see Link types)
top, bottom or left, rightstring or objectnoleft and right are relative of the link's direction, top and bottom relative of the link's angle, see Sub-elements
colorstringnoredefine the color
scalenumbernoredefine this link scale
sizenumbernoforced size/length of the link

Sub-elements

Elements meant to be drawn along-side a node/link.
There are two types: text and icons

Texts

You can just enter a string to be considered a text but you can define a text with more options as following:

KeyTypeRequiredInfo
textstringyesvalue of your text
colorstringnoredefine the color
fontstringnoredefine the font family
font-sizenumbernoredefine the font size
font-stylestringnoredefine the font style (see Font styles)
marginnumbernoredefine the margin with the parent element
line-heightnumbernoheight of each line in font size

Icons

You can define a relative icon with the following:

KeyTypeRequiredInfo
iconstring/objectyesname of the Font-Awesome icon of the sub-element (see Icon names)
colorstringnoredefine the color
scalenumbernoredefine this sub-icon scale
marginnumbernoredefine the margin with the parent element

More info

back to top

Icon names

Icons are fetched from Font-Awesome free icons.
When you reference an icon, for example circle, it's searched in the solid folder then regular then brands. If, in this case you want the hollow circle from the regular style, just enter regular circle or circle regular instead. It's very flexible as you can copy-paste from an HTML page far fa-circle and it will also works.

You can also specify a custom icon by entering the following object instead of a string:

KeyTypeRequiredInfo
pathstringyesthe SVG path of your icon
widthstringnothe custom width of the path (if blank, height is taken, then the height of all icons: 512)
heightstringnothe custom height of the path (if blank, width is taken)

Link types

Here are the accepted types and their preview :

npm.io

You can use none to make an invisible link.

Font styles

First, you should use web-safe fonts to be sure your SVG will be rendered correctly

Here are the available styles :

  • normal
  • bold
  • italic
  • oblique
  • underlined
  • overlined
  • striked

The style can also be a compound like bold italic.

Thrown errors

Errors can be thrown in these 2 cases :

  • Nodes could not be placed based on the constraints:
    • Failed to place nodes
  • A node or a link have an invalid property
    • Node 'name' is invalid at key ...

Change icons

If you want to change the icons you can edit/build your own resources.json as following:

{
    "name";: "squares",
    "height";: 60, //height of all icons
    "index";: [ // which categories are there and in which order it should be searched
        "solid",
        "hollow"
    ],
    "icons";: {
      "solid";: {
        "square";:{
          "path";:"M10 10v-40h40v40z",
          "width";:60
        }
      },
      "hollow";: {
        "square";:{
          "path";:"M10 10v-40h40v40h-40m10 10v-20h20v20h-20z",
          "width";:60
        }
      }
    }
}

Examples

back to top

Example 1 : Hello World

npm.io

{
  "nodes": [
    {
      "name": "node1",
      "icon": "laptop-code",
      "color": "#4E342E",
      "bottom": "my app"
    },
    {
      "name": "node2",
      "icon": "globe",
      "color": "#455A64",
      "bottom": "world"
    }
  ],
  "links": [
    {
      "from": "node1",
      "to": "node2",
      "color": "#333333",
      "top": {
        "icon": "envelope"
      },
      "bottom": "\"hello\""
    }
  ]
}
nodes:
  - name: node1
    icon: laptop-code
    color: '#4E342E'
    bottom: my app
  - name: node2
    icon: globe
    color: '#455A64'
    bottom: world
links:
  - from: node1
    to: node2
    color: '#333333'
    top:
      icon: envelope
    bottom: '"hello"'

Example 2 : Web App

npm.io

{
  "options": {
    "placing": {
      "diagonals": false
    },
    "rendering": {
      "icons": {
        "color": "#00695C"
      },
      "links": {
        "color": "#26A69A"
      },
      "texts": {
        "color": "#004D40",
        "font": "mono",
        "font-size": 12,
        "margin": 0.25
      },
      "sub-icons": {
        "color": "#004D40"
      }
    }
  },
  "nodes": [
    {
      "name": "client",
      "icon": "laptop",
      "bottom": "user"
    },
    {
      "name": "page",
      "icon": "file-code",
      "top": "index.html"
    },
    {
      "name": "js",
      "icon": "js-square",
      "bottom": "front-end"
    },
    {
      "name": "server",
      "icon": "node",
      "bottom": "back-end"
    },
    {
      "name": "db",
      "icon": "database"
    }
  ],
  "links": [
    {
      "from": "client",
      "to": "page"
    },
    {
      "from": "page",
      "to": "js",
      "type": "double",
      "top": {
        "icon": "vuejs"
      },
      "bottom": "VueJS",
      "direction": "down"
    },
    {
      "from": "js",
      "to": "server",
      "type": "split-double",
      "direction": "right",
      "top": {
        "text": "Ajax"
      },
      "bottom": {
        "text": "JSON"
      }
    },
    {
      "from": "db",
      "to": "server",
      "type": "double",
      "bottom": "Sequelize"
    }
  ]
}
options:
  placing:
    diagonals: false
  rendering:
    icons:
      color: '#00695C'
    links:
      color: '#26A69A'
    texts:
      color: '#004D40'
      font: mono
      font-size: 12
      margin: 0.25
    sub-icons:
      color: '#004D40'
nodes:
  - name: client
    icon: laptop
    bottom: user
  - name: page
    icon: file-code
    top: index.html
  - name: js
    icon: js-square
    bottom: front-end
  - name: server
    icon: node
    bottom: back-end
  - name: db
    icon: database
links:
  - from: client
    to: page
  - from: page
    to: js
    type: double
    top:
      icon: vuejs
    bottom: VueJS
    direction: down
  - from: js
    to: server
    type: split-double
    direction: right
    top:
      text: Ajax
    bottom:
      text: JSON
  - from: db
    to: server
    type: double
    bottom: Sequelize
1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago