Skip to content

Instantly share code, notes, and snippets.

@rrag
Last active August 19, 2019 11:01
Show Gist options
  • Select an option

  • Save rrag/95ffd539fa4e0b4544cf to your computer and use it in GitHub Desktop.

Select an option

Save rrag/95ffd539fa4e0b4544cf to your computer and use it in GitHub Desktop.
Line, Scatter Chart with react-stockcharts
license: MIT
height: 420
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React Stockcharts - LineAndScatterChart Example</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.7/d3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.17.0/babel.min.js"></script>
<!-- <script type="text/javascript" src="//unpkg.com/react-stockcharts@latest/dist/react-stockcharts.min.js"></script> -->
<script type="text/javascript" src="//rrag.github.io/react-stockcharts/dist/react-stockcharts.min.js"></script>
</head>
<body>
<span id="iconPreload" class="glyphicon glyphicon-arrow-down"></span>
<div id="chart">
</div>
<script>
// Use babel transform so the examples work on the browser
d3.request("./LineAndScatterChart.jsx")
.get(function(err, data) {
var outputEl = document.getElementById('chart');
try {
var output = Babel.transform(data.responseText, { presets: ["es2015", "react", "stage-3"] }).code;
eval(output);
} catch (ex) {
outputEl.innerHTML = 'ERROR: ' + ex.message;
}
})
</script>
</body>
</html>
"use strict";
var { ChartCanvas, Chart, series, scale, coordinates, tooltip, axes, helper } = ReStock;
var { BarSeries, LineSeries, AreaSeries, ScatterSeries, CircleMarker, SquareMarker, TriangleMarker } = series;
var { discontinuousTimeScaleProvider } = scale;
var { CrossHairCursor, MouseCoordinateX, MouseCoordinateY } = coordinates;
var { OHLCTooltip } = tooltip;
var { XAxis, YAxis } = axes;
var { fitWidth, TypeChooser } = helper;
class LineAndScatterChart extends React.Component {
render() {
var { data, type, width, ratio } = this.props;
return (
<ChartCanvas ratio={ratio} width={width} height={400}
margin={{ left: 70, right: 70, top: 20, bottom: 30 }}
type={type}
pointsPerPxThreshold={1}
seriesName="MSFT"
data={data}
xAccessor={d => d.date} xScaleProvider={discontinuousTimeScaleProvider}
xExtents={[new Date(2012, 0, 1), new Date(2012, 2, 2)]}>
<Chart id={1}
yExtents={d => [d.high, d.low, d.AAPLClose, d.GEClose]}>
<XAxis axisAt="bottom" orient="bottom"/>
<YAxis
axisAt="right"
orient="right"
// tickInterval={5}
// tickValues={[40, 60]}
ticks={5}
/>
<MouseCoordinateX
at="bottom"
orient="bottom"
displayFormat={d3.timeFormat("%Y-%m-%d")} />
<MouseCoordinateY
at="right"
orient="right"
displayFormat={d3.format(".2f")} />
<LineSeries
yAccessor={d => d.AAPLClose}
stroke="#ff7f0e"
strokeDasharray="Dot" />
<ScatterSeries
yAccessor={d => d.AAPLClose}
marker={SquareMarker}
markerProps={{ width: 6, stroke: "#ff7f0e", fill: "#ff7f0e" }} />
<LineSeries
yAccessor={d => d.GEClose}
stroke="#2ca02c" />
<ScatterSeries
yAccessor={d => d.GEClose}
marker={TriangleMarker}
markerProps={{ width: 8, stroke: "#2ca02c", fill: "#2ca02c" }} />
<LineSeries
yAccessor={d => d.close}
strokeDasharray="LongDash" />
<ScatterSeries
yAccessor={d => d.close}
marker={CircleMarker}
markerProps={{ r: 3 }} />
<OHLCTooltip forChart={1} origin={[-40, 0]}/>
</Chart>
<CrossHairCursor />
</ChartCanvas>
);
}
}
LineAndScatterChart.propTypes = {
data: React.PropTypes.array.isRequired,
width: React.PropTypes.number.isRequired,
ratio: React.PropTypes.number.isRequired,
type: React.PropTypes.oneOf(["svg", "hybrid"]).isRequired,
};
LineAndScatterChart.defaultProps = {
type: "svg",
};
LineAndScatterChart = fitWidth(LineAndScatterChart);
var parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S");
d3["tsv"]("//rrag.github.io/react-stockcharts/data/MSFT.tsv", (err, data) => {
/* change MSFT.tsv to MSFT_full.tsv above to see how this works with lots of data points */
data.forEach((d, i) => {
d.date = new Date(d3.timeParse("%Y-%m-%d")(d.date).getTime());
d.open = +d.open;
d.high = +d.high;
d.low = +d.low;
d.close = +d.close;
d.volume = +d.volume;
// console.log(d);
});
/* change the type from hybrid to svg to compare the performance between svg and canvas */
ReactDOM.render(<TypeChooser type="hybrid">{type => <LineAndScatterChart data={data} type={type} />}</TypeChooser>, document.getElementById("chart"));
});
@SiripiGiridharReddy

Copy link
Copy Markdown

How do we write test cases for LineAndScatterChart components,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment