Get started

Charts help you tell accurate and convincing stories around data with beautiful and accessible visualizations.

Resources

Get started with charts

Refer to the contribution guidelines before you start building. If you want to develop your own components, see the React Storybook to get started.

Visit the component status page for a complete list of available components.

Install packages

Using yarn:


$ yarn add @carbon/charts @carbon/charts-react d3

If you prefer npm:


$ npm install --save @carbon/charts @carbon/charts-react d3

Build your first chart

To start, try the BarChart component with the example code below:

import React from 'react';
import { defaultColors } from '@carbon/charts';
import { BarChart } from '@carbon/charts-react';
import '@carbon/charts/style.css';
const stackedBarOptions = {
// ...see next section
};
class App extends React.Component {
state = {
stackedBarData: {
// ...see next section
},
};
render() {
return (
<div className="App">
<BarChart
data={this.state.stackedBarData}
options={stackedBarOptions}
width="100%"
height={400}
/>
</div>
);
}
}
// ...

Start the server


cd packages/react
yarn run storybook

Launch your browser and go to http://localhost:9005/.

Data and options

Data and options follow the same model in all charts, with minor exceptions and differences in specific components.

For example, you only need to provide one data set for pie and donut charts. In the case of donut charts, you can pass in an additional field called center in your options when configuring the donut center.

Additional options available vary by chart type. For more examples see the data demo.

Below are sample data and options that can be used in a stacked bar chart:

import { BarChart, defaultColors } from '@carbon/charts';
// Use the below if you're loading bundles through a CDN
// const { BarChart, defaultColors } = Charts;
const stackedBarData = {
labels: ['Quantity', 'Leads', 'Sold', 'Restocking', 'Misc'],
datasets: [
{
label: 'Dataset 1',
backgroundColors: [defaultColors[0]],
data: [65000, 29123, 35213, 51213, 16932],
},
{
label: 'Dataset 2',
backgroundColors: [defaultColors[1]],
data: [32432, 21312, 56456, 21312, 34234],
},
{
label: 'Dataset 3',
backgroundColors: [defaultColors[2]],
data: [12312, 23232, 34232, 12312, 34234],
},
],
};
const stackedBarOptions = {
scales: {
x: {
title: '2018 Annual Sales',
},
y: {
title: 'Figures',
formatter: axisValue => {
return `${axisValue / 1000}k`;
},
stacked: true,
},
},
legendClickable: true,
containerResizable: true,
};

Troubleshooting

If you experience any issues while getting started with Carbon Charts, head over to the GitHub repo for more guidance and support. Please create a GitHub issue if you find a bug or need more help.