Chart.xkcd is a chart library plots “sketchy”, “cartoony” or “hand-drawn” styled charts.
It’s easy to get started with chart.xkcd. All that’s required is the script included in your page along with a single <svg>
node to render the chart.
In the following example we create a line chart.
JS part of the example
const svg = document.querySelector('.line-chart') new chartXkcd.Line(svg, { title: '', xLabel: '', yLabel: '', data: {...}, options: {}, });
title
: optional title of the chartxLabel
: optional x label of the chartyLabel
: optional y label of the chartdata
: the data you want to visulizeoptions
: optional configurations to customize how the chart looksYou can install chart.xkcd via script tag in HTML or via npm
<script src="https://cdn.jsdelivr.net/npm/chart.xkcd@1.1/dist/chart.xkcd.min.js" integrity="sha256-NkH6G4XRcQ5Bsfs7O6yh9mw1SJLEOJWCtWqko6VjF34=" crossorigin="anonymous"></script> <script> const myChart = new chartXkcd.Line(svg, {...}); </script>
Install
npm i chart.xkcd
Usage
import chartXkcd from 'chart.xkcd'; const myChart = new chartXkcd.Line(svg, {...});
Other ways
Line chart displays series of data points in the form of lines. It can be used to show trend data, or comparison of different data sets.
const lineChart = new chartXkcd.Line(svg, { title: 'Monthly income of an indie developer', // optional xLabel: 'Month', // optional yLabel: '$ Dollars', // optional 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], }], }, options: { // optional yTickCount: 3, legendPosition: chartXkcd.config.positionType.upLeft } })
yTickCount
: customize tick numbers you want to see on the y axis (default 3
)showLegend
: display legend near chart (default true
)legendPosition
: specify where you want to place the legend. (default chartXkcd.config.positionType.upLeft
)
Possible values:
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.upRight
chartXkcd.config.positionType.downLeft
chartXkcd.config.positionType.downRight
dataColors
: array of colors for different datasetsfontFamily
: customize font family used in the chartunxkcdify
: disable xkcd effect (default false
)strokeColor
: stroke colors (default black
)backgroundColor
: color for BG (default white
)XY chart is used to plot points by specifying their XY coordinates.
You can also plot XY line chart by connecting the points.
const svg = document.querySelector('.xy-chart'); new chartXkcd.XY(svg, { title: 'Pokemon farms', //optional xLabel: 'Coordinate', //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 }], }], }, options: { //optional xTickCount: 5, yTickCount: 5, legendPosition: chartXkcd.config.positionType.upRight, showLine: false, timeFormat: undefined, dotSize: 1, }, });
xTickCount
: customize tick numbers you want to see on the x axis (default 3
)yTickCount
: customize tick numbers you want to see on the y axis (default 3
)showLegend
: display legend near chart (default true
)legendPosition
: specify where you want to place the legend (default chartXkcd.config.positionType.upLeft
)
Possible values:
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.downLeft
chartXkcd.config.positionType.downRight
showLine
: connect the points with lines (default: false
)timeFormat
: specify the time format if the x values are time (default undefined
)
chart.xkcd use dayjs to format time, you can find the all the available formats heredotSize
: you can change size of the dots if you want (default 1
)dataColors
: array of colors for different datasetsfontFamily
: customize font family used in the chartunxkcdify
: disable xkcd effect (default false
)strokeColor
: stroke colors (default black
)backgroundColor
: color for BG (default white
)Another example of XY chart: XY line chart with timeFormat
A bar chart provides a way of showing data values represented as vertical bars
const barChart = new chartXkcd.Bar(svg, { title: 'github stars VS patron number', // optional // xLabel: '', // optional // yLabel: '', // optional data: { labels: ['github stars', 'patrons'], datasets: [{ data: [100, 2], }], }, options: { // optional yTickCount: 2, }, });
yTickCount
: customize tick numbers you want to see on the y axisdataColors
: array of colors for different datasetsfontFamily
: customize font family used in the chartunxkcdify
: disable xkcd effect (default false
)strokeColor
: stroke colors (default black
)backgroundColor
: color for BG (default white
)A stacked bar chart provides a way of showing data values represented as vertical bars
new chartXkcd.StackedBar(svg, { title: 'Issues and PR Submissions', xLabel: 'Month', yLabel: 'Count', data: { labels: ['Jan', 'Feb', 'Mar', 'April', 'May'], datasets: [{ label: 'Issues', data: [12, 19, 11, 29, 17], }, { label: 'PRs', data: [3, 5, 2, 4, 1], }, { label: 'Merges', data: [2, 3, 0, 1, 1], }], }, });
yTickCount
: customize tick numbers you want to see on the y axisdataColors
: array of colors for different datasetsfontFamily
: customize font family used in the chartunxkcdify
: disable xkcd effect (default false
)strokeColor
: stroke colors (default black
)backgroundColor
: color for BG (default white
)showLegend
: display legend near chart (default true
)legendPosition
: specify where you want to place the legend (default chartXkcd.config.positionType.upLeft
)
Possible values:
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.downLeft
chartXkcd.config.positionType.downRight
const pieChart = new chartXkcd.Pie(svg, { title: 'What Tim is made of', // optional data: { labels: ['a', 'b', 'e', 'f', 'g'], datasets: [{ data: [500, 200, 80, 90, 100], }], }, options: { // optional innerRadius: 0.5, legendPosition: chartXkcd.config.positionType.upRight, }, });
innerRadius
: specify empty pie chart radius (default: 0.5
)
innerRadius
to 0
showLegend
: display legend near chart (default true
)legendPosition
: specify where you want to place the legend. (default chartXkcd.config.positionType.upLeft
)
Possible values:
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.downLeft
chartXkcd.config.positionType.downRight
dataColors
: array of colors for different datasetsfontFamily
: customize font family used in the chartunxkcdify
: disable xkcd effect (default false
)strokeColor
: stroke colors (default black
)backgroundColor
: color for BG (default white
)const radar = new chartXkcd.Radar(svg, { title: 'Letters in random words', // optional data: { labels: ['c', 'h', 'a', 'r', 't'], datasets: [{ label: 'ccharrrt', // optional data: [2, 1, 1, 3, 1], }, { label: 'chhaart', // optional data: [1, 2, 2, 1, 1], }], }, options: { // optional showLegend: true, dotSize: .8, showLabels: true, legendPosition: chartXkcd.config.positionType.upRight, }, });
showLabels
: display labels near every line (default false
)ticksCount
: customize tick numbers you want to see on the main line (default 3
)dotSize
: you can change size of the dots if you want (default 1
)showLegend
: display legend near chart (default false
)legendPosition
: specify where you want to place the legend. (default chartXkcd.config.positionType.upLeft
)
Possible values:
chartXkcd.config.positionType.upLeft
chartXkcd.config.positionType.upRight
chartXkcd.config.positionType.downLeft
chartXkcd.config.positionType.downRight
dataColors
: array of colors for different datasetsfontFamily
: customize font family used in the chartunxkcdify
: disable xkcd effect (default false
)strokeColor
: stroke colors (default black
)backgroundColor
: color for BG (default white
)