npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-chartist

v1.0.1

Published

generate SVG chart use Chartist on Node.js server side

Downloads

511

Readme

svg-chartist

Generate SVG chart use Chartist on Node.js server side

svg-chartist is a node.js server-side wrapper for Chartist, it modified from chartist-svg. Can be used to generate static SVG charts, you can insert raw SVG charts into HTML pages or export PDFs.

Installation

npm install svg-chartist --save

API

svg-chartist is exported as a function, you can reference it like this:

const chartistSvg = require('svg-chartist');

chartistSvg(type, chartData, opts)

A top-level function for generating static SVG chart. Returns a Promise that fulfilled with the static chart HTML.

Parameters

  • type {string} chart type. Values are: bar, line, pie
  • chartData {object} Chartist's data. The data object used to pie/bar/line chart. See Chartist's API for more
  • [opts] {object} optional options
  • [opts.options] {object} Chartist's options. See Chartist's API for more
  • [opts.resOptions] {array} Chartist's resOptions. See Chartist's API for more
  • [opts.onDraw] {Function} Chartist's 'draw' event listener
  • [opts.title] {object} chart's title options (height,width,fill etc). Title text is passed in by chartData.title
  • [opts.subtitle] {object} chart's subtitle options (height,width,fill etc). Subtitle text is passed in by chartData.subtitle
  • [opts.css] {string} Custom CSS will be appended to Chartist's CSS

Returns

  • <Promise> promise that resolves with the static chart HTML

Examples

Simple line chart

const chartistSvg = require('svg-chartist');
const fs = require('fs');

const data = {
  labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
  series: [
    [12, 9, 7, 8, 5],
    [2, 1, 3.5, 7, 3],
    [1, 3, 4, 5, 6]
  ]
};

const options = {
  fullWidth: true,
  chartPadding: {
    right: 40
  }
}

const opts = {
	options: options
}

chartistSvg('line', data, opts).then((html) => {
	fs.writeFileSync('./examples/simpleLineChart.html', html)
})

Simple pie chart

const chartistSvg = require('svg-chartist');
const fs = require('fs');

const data = {
  series: [5, 3, 4]
};

var sum = function(a, b) { return a + b };
const options = {
	width: 500,
	height: 300,
  labelInterpolationFnc: function(value) {
    return Math.round(value / data.series.reduce(sum) * 100) + '%';
  }
}

const opts = {
	options: options
}

chartistSvg('pie', data, opts).then((html) => {
	fs.writeFileSync('./examples/simplePieChart.html', html)
})

Custom bar chart

const chartistSvg = require('svg-chartist');
const fs = require('fs');

const data = {
	title: 'Custom bar chart',
	subtitle: 'by: itbilu.com',
  labels: ['Q1','Q2','Q3','Q4'],
  series: [
    [80, 50, 100, 75]
  ]
};

const options = {
	width: 700, 
  height: 350,
  stackBars: true,
  axisX: {
    showLabel: true,
    showGrid: false,
  }
}

const opts = {
	options: options,
	title: {
		height: 50,
		fill: "#4A5572"
	},
  css: `.ct-series-a .ct-bar, .ct-series-a .ct-line, .ct-series-a .ct-point, .ct-series-a .ct-slice-donut{
    stroke: #4A5572
  }`,
  onDraw: function (data) {
    if(data.type === 'bar') {
      data.element.attr({
        style: 'stroke-width: 30px'
      });
    }
  }
}

chartistSvg('bar', data, opts).then((html) => {
	fs.writeFileSync('./examples/customBarChart.html', html)
})