Skip to content

Instantly share code, notes, and snippets.

@pbeshai
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbeshai/7cec2430fe6f4c6543af to your computer and use it in GitHub Desktop.
Save pbeshai/7cec2430fe6f4c6543af to your computer and use it in GitHub Desktop.
import React from 'react';
import d3 from 'd3';
// CSS via webpack
require('styles/LineChart.css');
const LineChart = React.createClass({
propTypes: {
data: React.PropTypes.array.isRequired,
height: React.PropTypes.number.isRequired,
width: React.PropTypes.number.isRequired,
xKey: React.PropTypes.string, // the key for the x value
yKey: React.PropTypes.string // the key for the y value
},
getDefaultProps() {
return {
xKey: 'x',
yKey: 'y'
};
},
render() {
const { data, width, height, xKey, yKey } = this.props;
// create x and y scales that map the chart dimensions to the min/max x and y values in the data
const xMin = 0, xMax = width, yMin = height, yMax = 0;
const x = d3.scale.linear().domain(d3.extent(data, (d) => d[xKey])).range([xMin, xMax]);
const y = d3.scale.linear().domain(d3.extent(data, (d) => d[yKey])).range([yMin, yMax]);
// function to generate an svg path's d attribute for the series
// note: interpolation set to 'monotone' ensures the path goes through the points
const line = d3.svg.line()
.interpolate('monotone')
.x((d) => x(d[xKey]))
.y((d) => y(d[yKey]));
return (
<div>
<svg ref='svg' width={width} height={height} className='chart line-chart'>
<path d={line(data)} className='series' />
</svg>
</div>
);
}
});
export default LineChart;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment