Chart.js Graph-like Charts (tree, force directed)

Overview

Chart.js Graphs

NPM Package Github Actions

Chart.js module for charting graphs. Adding new chart types: graph, forceDirectedGraph, dendogram, and tree.

force

dend_h

tree_v

radial

Works great with https://github.com/chartjs/chartjs-plugin-datalabels or https://github.com/chrispahm/chartjs-plugin-dragdata

data label

Open in CodePen

Related Plugins

Check out also my other chart.js plugins:

Install

npm install --save chart.js chartjs-chart-graph

Usage

see Samples on Github

CodePens

Graphviz Dot File Parsing

A Graphviz Dot File parsing package is located at https://github.com/sgratzl/chartjs-chart-graph-dot-parser. It creates compatible data structures to be consumed by this plugin.

Styling

The new chart types are based on the existing line controller. Tho, instead of showing a line per dataset it shows edges as lines. Therefore, the styling options for points and lines are the same. See also https://www.chartjs.org/docs/latest/charts/line.html. However, to avoid confusion, the line options have a default line prefix, e..g lineBorderColor to specify the edge border color and pointBorderColor to specify the node border color.

Data Structure

data: {
  labels: ['A', 'B', 'C'], // node labels
  datasets: [{
    data: [ // nodes as objects
      { x: 1, y: 2 }, // x, y will be set by the force directed graph and can be omitted
      { x: 3, y: 1 },
      { x: 5, y: 3 }
    ],
    edges: [ // edge list where source/target refers to the node index
      { source: 0, target: 1},
      { source: 0, target: 2}
    ]
  }]
},

Alternative structure for trees

data: {
  labels: ['A', 'B', 'C'], // node labels
  datasets: [{
    data: [ // nodes as objects
      { x: 1, y: 2 }, // x, y will be set by the force directed graph and can be omitted
      { x: 3, y: 1, parent: 0 },
      { x: 5, y: 3, parent: 0 }
    ]
  }]
},

Force Directed Graph

chart type: forceDirectedGraph

Computes the x,y position of nodes based on a force simulation. It is based on https://github.com/d3/d3-force/.

force

Open in CodePen

Options

Dendogram, Tree

chart types: dendogram, tree

The tree and dendograms layouts are based on https://github.com/d3/d3-hierarchy.

Dendogram Horizontal

dend_h

Open in CodePen

Dendogram Vertical

dend_v

Open in CodePen

Dendogram Radial

radial

Open in CodePen

Tidy Tree Horizontal

tree_h

Open in CodePen

Tidy Tree Vertical

tree_v

Open in CodePen

Tidy Tree Radial

radial

Open in CodePen

Options

ESM and Tree Shaking

The ESM build of the library supports tree shaking thus having no side effects. As a consequence the chart.js library won't be automatically manipulated nor new controllers automatically registered. One has to manually import and register them.

Variant A:

import { Chart, LinearScale, Point } from 'chart.js';
import { ForceDirectedGraphController, EdgeLine } from 'chartjs-chart-graph';

// register controller in chart.js and ensure the defaults are set
Chart.register(ForceDirectedGraphController, EdgeLine, LinearScale, Point);
...

new Chart(ctx, {
  type: ForceDirectedGraphController.id,
  data: [...],
});

Variant B:

import { ForceDirectedGraphChart } from 'chartjs-chart-graph';

new ForceDirectedGraphChart(ctx, {
  data: [...],
});

Development Environment

npm i -g yarn
yarn install
yarn sdks vscode

Building

yarn install
yarn build
Comments
  • Rotate forceDirectedGraph

    Rotate forceDirectedGraph

    Hi. I use your example with 'forceDirectedGraph' and i provided my example json.

    const data =`
    {
      "nodes": [
        {
          "id": "e1049793-0c77-4f32-b95d-8067613798d9"
        },
    	{
          "id": "c315f4a6-5f1d-464d-8f7b-e369d7d205cf"
        },
    	{
          "id": "524f4f3a-edf1-484b-9e5a-54f347937e27"
        },
    	{
          "id": "b55af307-e2ce-4d27-8851-5f204770d922"
        },
    	{
          "id": "df0b4f7d-8b70-4884-b249-635010d95be5"
        }
        
      ],
      "links": [
        {
          "source": "e1049793-0c77-4f32-b95d-8067613798d9",
          "target": "c315f4a6-5f1d-464d-8f7b-e369d7d205cf"
        },
    	{
          "source": "c315f4a6-5f1d-464d-8f7b-e369d7d205cf",
          "target": "524f4f3a-edf1-484b-9e5a-54f347937e27"
        },
    	{
          "source": "c315f4a6-5f1d-464d-8f7b-e369d7d205cf",
          "target": "b55af307-e2ce-4d27-8851-5f204770d922"
        },
    	{
          "source": "524f4f3a-edf1-484b-9e5a-54f347937e27",
          "target": "df0b4f7d-8b70-4884-b249-635010d95be5"
        },
    	{
          "source": "b55af307-e2ce-4d27-8851-5f204770d922",
          "target": "df0b4f7d-8b70-4884-b249-635010d95be5"
        }
      ]
    }`
    

    And it generates graph i wanted. But furthermore i want to have first node at the top and last note at the bottom. Currently I have left side, but I want right side. Do you know how can i do that? And I want labels to be visible all the time. image

    • Version: Newest
    • Browser: Chrome
    question 
    opened by sk0gen 8
  • Pan with chartjs-chart-graph?

    Pan with chartjs-chart-graph?

    Is there any (easy) way to add zoom / pan functionality to chartjs-chart-graph? I have tried chartjs-plugin-zoom and zoom works fine, but no pan.

    Check this fiddle: https://jsfiddle.net/y8jm4q3u

    Context

    • Version: Chart.js: 2.9.4, Chart.Graphs: 2.2.1, Zoom plugin: 0.7.7
    • Browser: Chrome Desktop 86.0.4240.111, Firefox 81.0.2

    Any help would be greatly appreciated. Thank you!

    question 
    opened by mossaiby 5
  • Dashed line in graph

    Dashed line in graph

    The documentation says that the config is derived from the line chart but it doesn’t seem to be working for lines. I am trying to change the line to a dashed line by adding borderDash: [5, 5]

    But it causes the graph rendering to fail with the below error:

    edgeLine.ts:58 Uncaught TypeError: Failed to execute 'setLineDash' on 'CanvasRenderingContext2D': The provided value cannot be converted to a sequence.
        at EdgeLine.draw (edgeLine.ts:58)
    
    bug 
    opened by ghost 4
  • Cannot require the module

    Cannot require the module

    Hi, thanks for publishing this library! When I require the module in my React App, I get a strange require error - see below

    To Reproduce

    Import the library using a normal require

    var test = require('chartjs-chart-graph')
    console.log(test)
    

    Another example with an actual portion of the code I'm using:

    import { makeStyles } from "@material-ui/core"
    import React from "react"
    import { useEffect, useState } from "react"
    import { ApiService } from "../../../services/Api.service"
    import { Project } from "../../../types/project"
    import { Chart } from "chart.js"
    import { ForceDirectedGraphController, EdgeLine } from 'chartjs-chart-graph';
    debugger
    
    const useStyles = makeStyles(theme => ({
        root: {
    
        },
        canvas: {
            maxWidth: '50%',
            marginLeft: 'auto',
            marginRight: 'auto'
        },
    }))
    
    

    Expected behavior Object is imported

    Actual Behavior

    TypeError: Cannot read property 'forEach' of undefined
        at http://localhost/static/js/1.chunk.js:77423:92
        at Module../node_modules/chartjs-chart-graph/build/index.esm.js (http://localhost/static/js/1.chunk.js:77427:5)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at fn (http://localhost/static/js/bundle.js:151:20)
        at Module../src/components/project/results/TaxaChart.tsx (c:\Users\dmarq\Documents\proteus\dayhoff\web\src\components\project\results\TaxaChart.tsx:7:1)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at fn (http://localhost/static/js/bundle.js:151:20)
        at Module../src/components/project-list/ProjectCollapsibleRow.tsx (http://localhost/static/js/main.chunk.js:3953:84)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at fn (http://localhost/static/js/bundle.js:151:20)
        at Module../src/components/project-list/ProjectsTable.tsx (http://localhost/static/js/main.chunk.js:4883:80)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at fn (http://localhost/static/js/bundle.js:151:20)
        at Module../src/components/project-list/Projects.tsx (http://localhost/static/js/main.chunk.js:4615:72)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at fn (http://localhost/static/js/bundle.js:151:20)
        at Module../src/App.tsx (http://localhost/static/js/main.chunk.js:228:91)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at fn (http://localhost/static/js/bundle.js:151:20)
        at Module../src/index.tsx (http://localhost/static/js/main.chunk.js:6928:62)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at fn (http://localhost/static/js/bundle.js:151:20)
        at Object.1 (http://localhost/static/js/main.chunk.js:7332:18)
        at __webpack_require__ (http://localhost/static/js/bundle.js:785:30)
        at checkDeferredModules (http://localhost/static/js/bundle.js:46:23)
        at Array.webpackJsonpCallback [as push] (http://localhost/static/js/bundle.js:33:19)
        at http://localhost/static/js/main.chunk.js:1:59
    

    Context

    • Version: ^3.0.0-beta.9
    • Chart.js version: 3.0.0-beta.13
    • Browser: Chrome

    Additional context What I have tried

    • Manually importing the library by downloading the source code
    • Cloning my own project on another folder
    • Use import syntax instead

    Please let me know if you need details on anything else

    bug 
    opened by diegomarquezp 3
  • support directed edges

    support directed edges

    It would be great if directed edges are supported as in an arrow head to indicate the direction of the edge

    User story

    support of directed graphs

    Additional context

    enhancement 
    opened by sgratzl 3
  • Support DOT graphviz format

    Support DOT graphviz format

    It would be great if this plugin supported creating trees from the GraphViz DOT standard format: https://www.graphviz.org/doc/info/lang.html. That would enable more use cases.

    User story I'm currently using chart.js for plotting data, and vis-network (https://visjs.github.io/vis-network/docs/network/) for displaying tree/hierarchical data because the source produces it in DOT format. This results in a large chunk of rendering-related code in my final software package that could be significantly reduced if I could simply use this plugin with chart.js.

    Just an idea, thanks for your library!

    enhancement 
    opened by s4m0r4m4 3
  • Make this extension available without npm

    Make this extension available without npm

    It would be great to be able to use this extension (and the other ones you've made) client-side, which cannot be done currently since the only way to install is through npm. Ideally it would be a single js file that can be loaded via a CDN, just like the original Chart.js.

    User story

    My use case is a small application to provide charts based on data given by the user. The whole thing fits in a few JavaScript files that are executed client-side. In fact, the whole thing works as a static site. I can use vanilla Chart.js, but this extension only works server-side currently, so I cannot use it.

    question answered 
    opened by JosephChataignon 2
  • Basic Implementation

    Basic Implementation

    Finished upgrading to ChartJS v3 and really liking the plugins you have here. Specifically the tree after working in the D3 world.

    How do I go about implementing? Meaning other plugins state something like "To create a sankey chart, include chartjs-chart-sankey.js after chart.js and then create the chart by setting the type attribute to 'sankey'".

    Easy enough. But I can't find any equivalent chartjs-chart-graph.js to reference. Everything in the samples are "index.umd.ts".

    I've tried bringing the entire folder structure into my project and reference but end up with errors ranging from "Cannot use import statement outside a module" to "Error: "dendogram" is not a registered controller.".

    I am building in just HTML5/Javascript environment.

    Any direction would be greatly appreciated.

    question answered 
    opened by glimpsedchaos 2
  • Is it possible to change the spacing between branches?

    Is it possible to change the spacing between branches?

    Hi Samuel, the library is working wonders. I've been able to display a taxonomical tree with lots of branches (~30)

    I'm having the following question... Is it possible to increment the space between each branch so some labels don't overlap?

    Screenshots / Sketches image That's the currently configured tree.

    Context

    • Version: 3.0.0-beta.9
    • Browser: Chrome
    question 
    opened by diegomarquezp 2
  • Bump ini from 1.3.5 to 1.3.7

    Bump ini from 1.3.5 to 1.3.7

    Bumps ini from 1.3.5 to 1.3.7.

    Commits
    • c74c8af 1.3.7
    • 024b8b5 update deps, add linting
    • 032fbaf Use Object.create(null) to avoid default object property hazards
    • 2da9039 1.3.6
    • cfea636 better git push script, before publish instead of after
    • 56d2805 do not allow invalid hazardous string as section name
    • See full diff in compare view
    Maintainer changes

    This version was pushed to npm by isaacs, a new releaser for ini since your current version.


    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 2
  • unable to install

    unable to install

    When I try to install the package using npm getting

    npm ERR! code E403 npm ERR! 403 403 Forbidden - GET https://dssits.jfrog.io/dssits/api/npm/npm/chartjs-chart-graph

    To Reproduce

    Expected behavior

    Screenshots

    Context

    • Version:
    • Browser:

    Additional context

    help wanted invalid question 
    opened by kanumuri9593 2
  • build(deps): bump json5 from 1.0.1 to 1.0.2

    build(deps): bump json5 from 1.0.1 to 1.0.2

    Bumps json5 from 1.0.1 to 1.0.2.

    Release notes

    Sourced from json5's releases.

    v1.0.2

    • Fix: Properties with the name __proto__ are added to objects and arrays. (#199) This also fixes a prototype pollution vulnerability reported by Jonathan Gregson! (#295). This has been backported to v1. (#298)
    Changelog

    Sourced from json5's changelog.

    Unreleased [code, diff]

    v2.2.3 [code, diff]

    v2.2.2 [code, diff]

    • Fix: Properties with the name __proto__ are added to objects and arrays. (#199) This also fixes a prototype pollution vulnerability reported by Jonathan Gregson! (#295).

    v2.2.1 [code, diff]

    • Fix: Removed dependence on minimist to patch CVE-2021-44906. (#266)

    v2.2.0 [code, diff]

    • New: Accurate and documented TypeScript declarations are now included. There is no need to install @types/json5. (#236, #244)

    v2.1.3 [code, diff]

    • Fix: An out of memory bug when parsing numbers has been fixed. (#228, #229)

    v2.1.2 [code, diff]

    ... (truncated)

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
Releases(v4.1.0)
Owner
Samuel Gratzl
Research Software Engineer with a focus on Data Visualization - author of @lineupjs and @upsetjs
Samuel Gratzl
A web app that shows visualizations of the most used graphs algorithms such as BFS, DFS, Dijsktra, Minimum spanning tree, etc. It allows you to draw your own graph.

Graph Visualizer Draw your own graphs and visualize the most common graph algorithms This web application allows you to draw a graph from zero, with p

Gonzalo Pereira 31 Jul 29, 2022
Free Bootstrap 5 Admin and Dashboard Template that comes with all essential dashboard components, elements, charts, graph and application pages. Download now for free and use with personal or commercial projects.

PlainAdmin - Free Bootstrap 5 Dashboard Template PlainAdmin is a free and open-source Bootstrap 5 admin and dashboard template that comes with - all e

PlainAdmin 238 Dec 31, 2022
A jsPlumb-based tree chart implementation for jQuery.

jsPlumbTree A jQuery plugin for generating a tree structure using jsPlumb. The tree is drawn from left to right, top to bottom. Please note that only

Daniele Ricci 4 Jul 27, 2022
Smoothie Charts: smooooooth JavaScript charts for realtime streaming data

Smoothie Charts is a really small charting library designed for live streaming data. I built it to reduce the headaches I was getting from watching ch

Joe Walnes 2.2k Dec 13, 2022
J2CL and GWT Charts library based on CHART.JS

Charba - J2CL and GWT Charts library based on CHART.JS What's Charba GWT Web toolkit doesn't have charting library available out of the box. There are

Pepstock.org 56 Dec 17, 2022
Beautiful charts for Angular based on Chart.js

ng2-charts slack Beautiful charts for Angular based on Chart.js Usage & Demo Samples using ng2-charts https://valor-software.com/ng2-charts/ Installat

Valor Software 2.2k Jan 4, 2023
Chart.js plugin to create charts with a hand-drawn, sketchy, appearance

chartjs-plugin-rough Chart.js plugin to create charts with a hand-drawn, sketchy, appearance Version 0.2 requires Chart.js 2.7.0 or later, and Rough.j

Akihiko Kusanagi 73 Dec 1, 2022
Chart.js module for creating treemap charts

chartjs-chart-treemap Chart.js v3.6.0 module for creating treemap charts. Implementation for Chart.js v2 is in 2.x branch Documentation You can find d

Jukka Kurkela 99 Dec 18, 2022
Chart.js Box Plots and Violin Plot Charts

Chart.js Box and Violin Plot Chart.js module for charting box and violin plots. This is a maintained fork of @datavisyn/chartjs-chart-box-and-violin-p

Samuel Gratzl 61 Dec 14, 2022
A Simple Dashboard Chart in Laravel Nova using Chart JS

A Simple Dashboard Chart in Laravel Nova using Chart JS. Starting create your own dashboard with Chart JS Integration can save your time and help you maintain consistency across standard elements such as Bar, Stacked, Line, Area, Doughnut and Pie Chart.

Kuncoro Wicaksono 177 Jan 4, 2023
Chart.js plugin to defer initial chart updates

Chart.js plugin to defer initial chart updates until the user scrolls and the canvas appears inside the viewport, and thus trigger the initial chart a

Chart.js 97 Nov 9, 2022
Bar Funnel Chart extension for Chart.js

Chart.BarFunnel.js Provides a Bar Funnel Chart for use with Chart.js Documentation To create a Bar Funnel Chart, include Chart.BarFunnel.js after Char

Chart.js 58 Nov 24, 2022
TradeX-chart is a trade chart written in plain (vanilla) JavaScript with minimal dependencies

TradeX-chart is a trade chart written in plain (vanilla) JavaScript with minimal dependencies; use it with any framework or backend.

null 24 Dec 12, 2022
A JavaScript library dedicated to graph drawing

sigma.js - v1.2.1 Sigma is a JavaScript library dedicated to graph drawing, mainly developed by @jacomyal and @Yomguithereal. Resources The website pr

Alexis Jacomy 10.3k Jan 3, 2023
a graph visualization library using web workers and jQuery

arbor.js -------- Arbor is a graph visualization library built with web workers and jQuery. Rather than trying to be an all-encompassing framework, a

Christian Swinehart 2.6k Dec 19, 2022
A React toolkit for graph visualization based on G6

Graphin A React toolkit for graph analysis based on G6 English | 简体中文 ✨ Features ?? Good-looking elements, standardized style configuration Graphin st

AntV team 823 Jan 7, 2023
Gephi - The Open Graph Viz Platform

Gephi - The Open Graph Viz Platform Gephi is an award-winning open-source platform for visualizing and manipulating large graphs. It runs on Windows,

Gephi 5.1k Jan 4, 2023
3D graph viewer powered by WebGL (three.js)

Graphosaurus A three-dimensional static graph viewer. (click the image to try it out) Demos EVE Online map Add nodes incrementally Documentation JSDoc

Corey Farwell 362 Dec 4, 2022
Simple tiny dependency graph engine, MobX inspired

?? Quarx Simple tiny dependency graph engine, MobX inspired Introduction In less than 200 lines of code and zero dependencies Quarx supports most of M

Dmitry Maevsky 22 Nov 2, 2022