Skip to content

Instantly share code, notes, and snippets.

@rrag
Last active Aug 19, 2019
Embed
What would you like to do?
CandleStickChart with volume histogram overlay v2
license: MIT
height: 420
"use strict";
var { ChartCanvas, Chart, series, scale, coordinates, tooltip, axes, indicator, helper } = ReStock;
var { CandlestickSeries, BarSeries } = series;
var { discontinuousTimeScaleProvider } = scale;
var { XAxis, YAxis } = axes;
var { fitWidth, TypeChooser } = helper;
class CandleStickStockScaleChartWithVolumeBarV2 extends React.Component {
render() {
var { data, type, width, ratio } = this.props;
return (
<ChartCanvas ratio={ratio} width={width} height={400}
margin={{left: 50, right: 50, top:10, bottom: 30}} type={type}
seriesName="MSFT"
data={data}
xAccessor={d => d.date} xScaleProvider={discontinuousTimeScaleProvider}
xExtents={[new Date(2012, 0, 1), new Date(2012, 6, 2)]}>
<Chart id={1} yExtents={d => [d.high, d.low]}>
<XAxis axisAt="bottom" orient="bottom"/>
<YAxis axisAt="right" orient="right" ticks={5} />
<CandlestickSeries />
</Chart>
<Chart id={2} origin={(w, h) => [0, h - 150]} height={150} yExtents={d => d.volume}>
<YAxis axisAt="left" orient="left" ticks={5} tickFormat={d3.format(".0s")}/>
<BarSeries yAccessor={d => d.volume} fill={(d) => d.close > d.open ? "#6BA583" : "red"} />
</Chart>
</ChartCanvas>
);
}
}
CandleStickStockScaleChartWithVolumeBarV2.propTypes = {
data: React.PropTypes.array.isRequired,
width: React.PropTypes.number.isRequired,
ratio: React.PropTypes.number.isRequired,
type: React.PropTypes.oneOf(["svg", "hybrid"]).isRequired,
};
CandleStickStockScaleChartWithVolumeBarV2.defaultProps = {
type: "svg",
};
CandleStickStockScaleChartWithVolumeBarV2 = fitWidth(CandleStickStockScaleChartWithVolumeBarV2);
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 => <CandleStickStockScaleChartWithVolumeBarV2 data={data} type={type} />}</TypeChooser>, document.getElementById("chart"));
});
"use strict";
var { ChartCanvas, Chart, DataSeries } = ReStock;
var { CandlestickSeries, HistogramSeries } = ReStock.series;
var { StockscaleTransformer } = ReStock.transforms;
var { XAxis, YAxis } = ReStock.axes;
var { fitWidth, TypeChooser } = ReStock.helper;
class CandleStickStockScaleChartWithVolumeHistogramV2 extends React.Component {
render() {
var { data, type, width } = this.props;
return (
<ChartCanvas width={width} height={400}
margin={{left: 50, right: 50, top:10, bottom: 30}} initialDisplay={100}
dataTransform={[ { transform: StockscaleTransformer } ]}
data={data} type={type}>
<Chart id={1} >
<XAxis axisAt="bottom" orient="bottom"/>
<YAxis axisAt="right" orient="right" ticks={5} />
<DataSeries id={0} yAccessor={CandlestickSeries.yAccessor} >
<CandlestickSeries />
</DataSeries>
</Chart>
<Chart id={2} height={150} origin={(w, h) => [0, h - 150]}>
<YAxis axisAt="left" orient="left" ticks={5} tickFormat={d3.format("s")}/>
<DataSeries id={0} yAccessor={(d) => d.volume} >
<HistogramSeries fill={(d) => d.close > d.open ? "#6BA583" : "red"} />
</DataSeries>
</Chart>
</ChartCanvas>
);
}
}
CandleStickStockScaleChartWithVolumeHistogramV2.propTypes = {
data: React.PropTypes.array.isRequired,
width: React.PropTypes.number.isRequired,
type: React.PropTypes.oneOf(["svg", "hybrid"]).isRequired,
};
CandleStickStockScaleChartWithVolumeHistogramV2.defaultProps = {
type: "svg",
};
CandleStickStockScaleChartWithVolumeHistogramV2 = fitWidth(CandleStickStockScaleChartWithVolumeHistogramV2);
var parseDate = d3.time.format("%Y-%m-%d").parse;
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(parseDate(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 => <CandleStickStockScaleChartWithVolumeHistogramV2 data={data} type={type} />}</TypeChooser>, document.getElementById("chart"));
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React Stockcharts - CandleStickStockScaleChartWithVolumeBarV2 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("./CandleStickStockScaleChartWithVolumeBarV2.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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment