An area chart stacked by percentage

by ling ding

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/react-with-addons.min.js"></script>
<script src="https://npmcdn.com/[email protected]/dist/react-dom.min.js"></script>
<script src="https://npmcdn.com/prop-types/prop-types.min.js"></script>
<script src="https://npmcdn.com/recharts/umd/Recharts.min.js"></script>
<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

CSS

body {
  margin: 0;
}
#container {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 10px;
  width: 800px;
  height: 800px;
  background-color: #fff;
}

Babel + JSX

const {AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip} = Recharts;
const data = [
      {month: '2015.01', a: 4000, b: 2400, c: 2400},
      {month: '2015.02', a: 3000, b: 1398, c: 2210},
      {month: '2015.03', a: 2000, b: 9800, c: 2290},
      {month: '2015.04'},
      {month: '2015.05', a: 1890, b: 4800, c: 2181},
      {month: '2015.06', a: 2390, b: 3800, c: 2500},
      {month: '2015.07', a: 3490, b: 4300, c: 2100},
			{month: '2015.07'},
];

const getPercent = (value, total) => {
	const ratio = total > 0 ? value / total : 0;
  
  return toPercent(ratio, 2);
};

const toPercent = (decimal, fixed = 0) => {
	return `${(decimal * 100).toFixed(fixed)}%`;
};

const StackedAreaChart = React.createClass({
	render () {
  	return (
    	<AreaChart width={600} height={400} data={data} 
            margin={{top: 10, right: 30, left: 0, bottom: 0}} >
        <XAxis dataKey="month"/>
        <YAxis tickFormatter={toPercent}/>
        <Area type='monotone' dataKey='a'  stroke='#8884d8' fill='#8884d8'/>
        <Area type='monotone' dataKey='b'  stroke='#82ca9d' fill='#82ca9d' />
        <Area type='monotone' dataKey='c'  stroke='#ffc658' fill='#ffc658' />
      </AreaChart>
    );
  }
})

ReactDOM.render(
  <StackedAreaChart />,
  document.getElementById('container')
);