Hand-drawn Style SVG Chart Library – chart.xkcd

Category: Chart & Graph , Javascript , Recommended | September 5, 2019
Author:timqian
Views Total:467 views
Official Page:Go to website
Last Update:September 5, 2019
License:MIT

Preview:

Hand-drawn Style SVG Chart Library – chart.xkcd

Description:

chart.xkcd is a JavaScript plotting library for drawing XKCD-style hand-drawn charts using SVG and plain JavaScript.

Supported Chart types:

  • Line
  • Bar
  • Scatter
  • Donut/Pie

How to use it:

Install & download the chart.xkcd.

# NPM
$ npm install chart.xkcd --save

Import the minified version of the chart.xkcd library into the document.

import chartXkcd from 'chart.xkcd';
<script src="https://cdn.jsdelivr.net/npm/chart.xkcd@latest/dist/chart.xkcd.min.js"></script>

Create an SVG element for the chart.

<svg class="myChart"></svg>

Create a bar chart using the chartXkcd.Bar method.

const svg = document.querySelector('.myChart');
new chartXkcd.Bar(svg, {
  // title: 'Monthly income of an indie developer',
  // xLabel: 'Month',
  // yLabel: '$ Dollors',
  data: {
    labels: ['github stars', 'patrons'],
    datasets: [{
      // label: 'Battle',
      data: [100, 2],
    }],
  },
});

Create a line chart using the chartXkcd.Line method.

new chartXkcd.Line(svg, {
  // title: 'Monthly income of an indie developer',
  // xLabel: 'Month',
  // yLabel: '$ Dollors',
  data: {
    labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
    datasets: [{
      label: 'Plan',
      data: [30, 70, 200, 300, 500, 800, 1500, 2900, 5000, 8000],
    }, {
      label: 'Reality',
      data: [0, 1, 30, 70, 80, 100, 50, 80, 40, 150],
    // // }, {
    // //   label: 'vivi',
    // //   data: [5, 12, 20, 7, 10,  35,15, 9, 20, 9, 10, 6],
    // }, {
      // label: 'weweyang',
      // data: [10, 20, 7, 12,  10, 15, 9, 20, 35, 9, 6, 17],
    }],
  },
});

Create a scatter chart using the chartXkcd.XY method.

const svg = document.querySelector('.xy-chart');
new chartXkcd.XY(svg, {
  title: 'Pokemon farms', //optional
  xLabel: 'Coodinate', //optional
  yLabel: 'Count', //optional
  data: {
    datasets: [{
      label: 'Pikachu',
      data: [{ x: 3, y: 10 }, { x: 4, y: 122 }, { x: 10, y: 100 }, { x: 1, y: 2 }, { x: 2, y: 4 }],
    }, {
      label: 'Squirtle',
      data: [{ x: 3, y: 122 }, { x: 4, y: 212 }, { x: -3, y: 100 }, { x: 1, y: 1 }, { x: 1.5, y: 12 }],
    }],
  }
});

Create a pie/donut chart using the chartXkcd.Pie method.

new chartXkcd.Pie(svg, {
  // title: 'What Tim made of',
  data: {
    labels: ['a', 'b', 'e', 'f', 'g'],
    datasets: [{
      data: [500, 200, 80, 90, 100],
    }],
  },
});

Optional settings to customize the chart.

new chartXkcd.Method(svg, {
    data: {
      // data here
    },
    options: {
      xTickCount: 3
      yTickCount: 3,
      legendPosition: chart.Xkcd.positionType.upLeft // or upRight
      dataColors: [], // an array of colors
      fontFamily: '', // font family
      showLine: false, // for Scatter chart
      timeFormat: undefined, // for Scatter chart, checkout the dayjs: https://cssscript.com/date-time-nanipulation-library-dayjs/
      dotSize: 1, // for Scatter chart
      innerRadius: .5 // for Dount/Pie chart
    }
});

Changelog:

v1.1 (09/05/2019)

  • Allows to customize the font family and data color

You Might Be Interested In:


Leave a Reply