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

@janiscommerce/format-data-to-google-chart

v1.2.0

Published

A package that formats the data for use in Google charts

Downloads

7

Readme

Format data to google chart

Build Status Coverage Status npm version

A package for format an array of object for create the data for many Google Charts.

Installation

npm install @janiscommerce/format-data-to-google-chart

Usage

const { LineChart } = require('@janiscommerce/format-data-to-google-chart');

const lineChart = new LineChart({
	label: {
		source: 'date'
	},
	values: [
		{
			source: 'quantity'
		},
		{
			source: 'quantityColor',
			attributes: {
				role: 'style'
			}
		},
		{
			source: 'base',
			value: 5
		}
	]
});

lineChart.setData(sampleData);

const { data } = lineChart.parse();

// data preview

[
	['date', 'quantity', { role: 'style' }, 'base'],
	['2020-04-15', 10, 'blue', 5],
	['2020-04-16', 20, '#b87333', 5],
	['2020-04-17', 60, 'color: #e5e4e2', 5]
];

Label and Values items properties

| Property | type | description | required | | ----------- | -------- | ------------------------------------------------------------------- | -------- | | source | string | Field name for find value in the data | true | | value | any | Fixed value to use instead of the data | false | | title | string | Title for view in chart labels | false | | valueMapper | function | Function for modify value to show | false | | titleMapper | function | Function for modify title to show | false | | attributes | object | Object for use as title for modify chart (styles, annotations, etc) | false |

Examples

LineChart

const { LineChart } = require('@janiscommerce/format-data-to-google-chart');

const sampleData = [
	{
		id: 1,
		date: '2020-04-15',
		name: 'First element',
		quantity: 10,
		double: 20
	},
	{
		id: 2,
		date: '2020-04-16',
		name: 'Second element',
		quantity: 20,
		double: 40
	},
	{
		id: 3,
		date: '2020-04-17',
		name: 'Third element',
		quantity: 60,
		double: 120
	}
];

const lineChart = new LineChart({
	label: {
		source: 'date'
	},
	values: [
		{
			source: 'quantity'
		},
		{
			source: 'double'
		}
	]
});

lineChart.setData(sampleData);

const { data } = lineChart.parse();

// data preview
[
	['date', 'quantity', 'double'],
	['2020-04-15', 10, 20],
	['2020-04-16', 20, 40],
	['2020-04-17', 60, 120]
];

PieChart

const { PieChart } = require('@janiscommerce/format-data-to-google-chart');

const sampleData = [
	{
		id: 1,
		name: 'First element',
		quantity: 10
	},
	{
		id: 2,
		name: 'Second element',
		quantity: 20
	},
	{
		id: 3,
		name: 'Third element',
		quantity: 60
	}
];

const pieChart = new PieChart({
	label: {
		source: 'name'
	},
	values: [
		{
			source: 'quantity'
		}
	]
});

pieChart.setData(sampleData);

const { data } = pieChart.parse();

// data preview
[
	['name', 'quantity'],
	['First element', 10],
	['Second element', 20],
	['Third element', 60]
];

TableChart

const { TableChart } = require('@janiscommerce/format-data-to-google-chart');

const sampleData = [
	{
		id: 1,
		name: 'First element',
		quantity: 10
	},
	{
		id: 2,
		name: 'Second element',
		quantity: 20
	},
	{
		id: 3,
		name: 'Third element',
		quantity: 60
	}
];

const tableChart = new TableChart({
	values: [{ source: 'id' }, { source: 'name' }, { source: 'quantity' }]
});

tableChart.setData(sampleData);

const { data } = tableChart.parse();

// data preview
[
	[{ label: 'id' }, { label: 'name' }, { label: 'quantity' }],
	[1, 'First element', 10],
	[2, 'Second element', 20],
	[3, 'Third element', 60]
];

BarChart

const { BarChart } = require('@janiscommerce/format-data-to-google-chart');

const sampleData = [
	{
		id: 1,
		name: 'First element',
		quantity: 10,
		color: 'red',
		key: 'A1'
	},
	{
		id: 2,
		name: 'Second element',
		quantity: 20,
		color: 'blue',
		key: 'A2'
	},
	{
		id: 3,
		name: 'Third element',
		quantity: 60,
		color: 'black',
		key: 'A3'
	}
];
const barChart = new BarChart({
	label: {
		source: 'name'
	},
	values: [{ source: 'quantity' }]
});

barChart.setData(sampleData);

const { data } = barChart.parse();

// data preview
[
	['name', 'quantity'],
	['First element', 10],
	['Second element', 20],
	['Third element', 60]
];
const barChart = new BarChart({
	label: {
		source: 'name'
	},
	values: [
		{ source: 'quantity' },
		{
			source: 'color',
			attributes: { role: 'style' }
		},
		{
			source: 'key',
			attributes: { role: 'annotation' }
		}
	]
});

barChart.setData(sampleData);

const { data } = barChart.parse();

// data preview
[
	['name', 'quantity', { role: 'style' }, { role: 'annotation' }],
	['First element', 10, 'red', 'A1'],
	['Second element', 20, 'blue', 'A2'],
	['Third element', 60, 'black', 'A3']
];

ColumnChart

const { ColumnChart } = require('@janiscommerce/format-data-to-google-chart');

const sampleData = [
	{
		id: 1,
		date: '2020-04-15',
		name: 'First element',
		quantity: 10,
		quantityColor: 'blue',
		double: 20
	},
	{
		id: 2,
		date: '2020-04-16',
		name: 'Second element',
		quantity: 20,
		quantityColor: '#b87333',
		double: 40
	},
	{
		id: 3,
		date: '2020-04-17',
		name: 'Third element',
		quantity: 60,
		quantityColor: 'color: #e5e4e2',
		double: 120
	}
];

const ColumnChart = new ColumnChart({
	label: {
		source: 'date'
	},
	values: [
		{
			source: 'quantity'
		},
		{
			source: 'quantityColor',
			attributes: { role: 'style' }
		},
		{
			source: 'double'
		}
	]
});

ColumnChart.setData(sampleData);

const { data } = ColumnChart.parse();

// data preview

[
	['date', 'quantity', { role: 'style' }, 'double'],
	['2020-04-15', 10, 'blue', 20],
	['2020-04-16', 20, '#b87333', 40],
	['2020-04-17', 60, 'color: #e5e4e2', 120]
];