A bar chart which has customized bar
by coktopus
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 {PropTypes} = React;
const {BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend, Cell} = Recharts;
const colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"];
const data = [
{name: 'Page A', uv: 50, ex: 75},
{name: 'Page B', uv: 30, ex: 75},
{name: 'Page C', uv: 20, ex: 98},
{name: 'Page D', uv: 27, ex: 39},
{name: 'Page E', uv: 100, ex: 48},
{name: 'Page F', uv: 23, ex: 38},
{name: 'Page G', uv: 34, ex: 43},
];
const getPath = (x, y, width, height) => {
return `M ${x},${y} h ${width} v ${height} h ${-width} Z`;
};
const CustomBar = (props) => {
const { fill, x, y, width, height, ex } = props;
return (
<g>
<path d={getPath(x, y, width, height)} fill={fill}></path>
<path d={getPath(x, y, width, height+100)} fill="#00d108"></path>
</g>
);
};
CustomBar.propTypes = {
fill: PropTypes.string,
x: PropTypes.number,
y: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number
};
const CustomShapeBarChart = React.createClass({
render () {
return (
<BarChart width={600} height={300} data={data}
margin={{top: 20, right: 30, left: 20, bottom: 5}}>
<XAxis dataKey="name"/>
<YAxis padding={{bottom: 100}}/>
<CartesianGrid strokeDasharray="3 3"/>
<Bar dataKey="uv"
shape={<CustomBar fill="#0b5288"/>}
label={{ position: 'top' }}>
</Bar>
</BarChart>
);
}
})
ReactDOM.render(
<CustomShapeBarChart />,
document.getElementById('container')
);