Example shows React Stacked Bar 100% Chart with indexlabels shown for all the datapoints.
import React, { Component } from 'react';
import CanvasJSReact from '@canvasjs/react-charts';
//var CanvasJSReact = require('@canvasjs/react-charts');
var CanvasJS = CanvasJSReact.CanvasJS;
var CanvasJSChart = CanvasJSReact.CanvasJSChart;
class App extends Component {
render() {
const options = {
theme: 'light2',
title: {
text: "Time Spent in Holiday Season"
},
animationEnabled: true,
axisY: {
title: "Percent",
suffix: "%"
},
legend: {
horizontalAlign: 'center',
verticalAlign: 'bottom'
},
toolTip: {
shared: true
},
data: [{
type: "stackedBar100",
showInLegend: true,
color: "#BBDEFB",
name: "With Friends",
indexLabel: "#percent%",
toolTipContent: "{label} {name}: #percent%",
dataPoints: [
{ y: 320, label: "Shah" },
{ y: 300, label: "Joe" },
{ y: 400, label: "Fin" },
{ y: 220, label: "Larry" }
]
},
{
type: "stackedBar100",
showInLegend: true,
name: "Eating Out",
color: "#80DEEA",
indexLabel: "#percent%",
toolTipContent: "{name}: #percent%",
dataPoints: [
{ y: 320, label: "Shah" },
{ y: 320, label: "Joe" },
{ y: 280, label: "Fin" },
{ y: 420, label: "Larry" }
]
},
{
type: "stackedBar100",
showInLegend: true,
name: "Reading",
color: "#FFAB91",
indexLabel: "#percent%",
toolTipContent: "{name}: #percent%",
dataPoints: [
{ y: 120, label: "Shah" },
{ y: 120, label: "Joe" },
{ y: 300, label: "Fin" },
{ y: 120, label: "Larry" }
]
},
{
type: "stackedBar100",
showInLegend: true,
name: "Shopping",
color: "#B0BEC5",
indexLabel: "#percent%",
toolTipContent: "{name}: #percent%",
dataPoints: [
{ y: 320, label: "Shah" },
{ y: 220, label: "Joe" },
{ y: 150, label: "Fin" },
{ y: 420, label: "Larry" }
]
},
{
type: "stackedBar100",
showInLegend: true,
name: "Travel",
color: "#BCAAA4",
indexLabel: "#percent%",
toolTipContent: "{name}: #percent%",
dataPoints: [
{ y: 120, label: "Shah" },
{ y: 160, label: "Joe" },
{ y: 140, label: "Fin" },
{ y: 80, label: "Larry" }
]
},
{
type: "stackedBar100",
showInLegend: true,
name: "Internet",
color: "#A5D6A7",
indexLabel: "#percent%",
toolTipContent: "{name}: #percent%",
dataPoints: [
{ y: 104, label: "Shah" },
{ y: 120, label: "Joe" },
{ y: 300, label: "Fin" },
{ y: 420, label: "Larry" }
]
}]
};
return (
<div>
<CanvasJSChart options={options}
/* onRef={ref => this.chart = ref} */
/>
{/*You can get reference to the chart instance as shown above using onRef. This allows you to access all chart properties and methods*/}
</div >
);
}
}
export default App;
In the above example, legend is enabled by setting showInLegend property to true & the text shown in legend is changed by setting name property.
Note For step by step instructions, follow our React Integration Tutorial