Skip to content

Instantly share code, notes, and snippets.

@rrag

rrag/.block

Last active Aug 19, 2019
Embed
What would you like to do?
CandleStickChartWithHoverTooltip with react-stockcharts
license: MIT
height: 420
"use strict";
var { ChartCanvas, Chart, series, scale, coordinates, tooltip, axes, indicator, helper } = ReStock;
var { CandlestickSeries, BarSeries, LineSeries, AreaSeries } = series;
var { discontinuousTimeScaleProvider } = scale;
var { EdgeIndicator } = coordinates;
var { CurrentCoordinate } = coordinates;
var { OHLCTooltip, MovingAverageTooltip, HoverTooltip } = tooltip;
var { XAxis, YAxis } = axes;
var { ema, sma } = indicator;
var { fitWidth, TypeChooser } = helper;
var dateFormat = d3.timeFormat("%Y-%m-%d");
var numberFormat = d3.format(".2f");
function tooltipContent(calculators) {
return ({ currentItem, xAccessor }) => {
return {
x: dateFormat(xAccessor(currentItem)),
y: [
{ label: "open", value: currentItem.open && numberFormat(currentItem.open) },
{ label: "high", value: currentItem.high && numberFormat(currentItem.high) },
{ label: "low", value: currentItem.low && numberFormat(currentItem.low) },
{ label: "close", value: currentItem.close && numberFormat(currentItem.close) },
]
.concat(calculators.map(each => ({
label: each.tooltipLabel(),
value: numberFormat(each.accessor()(currentItem)),
stroke: each.stroke()
})))
.filter(line => line.value)
};
};
}
const keyValues = ["high", "low", "open"];
class CandleStickChartWithHoverTooltip extends React.Component {
removeRandomValues(data) {
return data.map((item) => {
const numberOfDeletion = Math.floor(Math.random() * keyValues.length) + 1;
for (let i = 0; i < numberOfDeletion; i += 1){
const randomKey = keyValues[Math.floor(Math.random() * keyValues.length)];
item[randomKey] = undefined;
}
return item;
});
}
render() {
var { data, type, width, ratio } = this.props;
// remove some of the data to be able to see
// the tooltip resize
data = this.removeRandomValues(data);
var ema20 = ema()
.id(0)
.windowSize(20)
.merge((d, c) => {d.ema20 = c;})
.accessor(d => d.ema20);
var ema50 = ema()
.id(2)
.windowSize(50)
.merge((d, c) => {d.ema50 = c;})
.accessor(d => d.ema50);
var margin = { left: 80, right: 80, top: 30, bottom: 50 };
return (
<ChartCanvas ratio={ratio} height={400}
width={width}
margin={margin}
type={type}
seriesName="MSFT"
data={data}
calculator={[ema20, ema50]}
xAccessor={d => d.date}
xScaleProvider={discontinuousTimeScaleProvider}
xExtents={[new Date(2015, 0, 1), new Date(2015, 5, 8)]}>
<Chart id={1}
yExtents={[d => [d.high, d.low], ema20.accessor(), ema50.accessor()]}
padding={{ top: 10, bottom: 20 }}>
<XAxis axisAt="bottom" orient="bottom"/>
<YAxis axisAt="right" orient="right" ticks={5} />
<CandlestickSeries />
<LineSeries yAccessor={ema20.accessor()} stroke={ema20.stroke()}/>
<LineSeries yAccessor={ema50.accessor()} stroke={ema50.stroke()}/>
<EdgeIndicator itemType="last"
orient="right"
edgeAt="right"
yAccessor={d => d.close}
fill={d => d.close > d.open ? "#6BA583" : "#FF0000"}/>
</Chart>
<Chart id={2}
yExtents={[d => d.volume]}
height={150} origin={(w, h) => [0, h - 150]}>
<YAxis axisAt="left" orient="left" ticks={5} tickFormat={d3.format(".0s")}/>
<BarSeries yAccessor={d => d.volume} fill={d => d.close > d.open ? "#6BA583" : "#FF0000"} />
</Chart>
<HoverTooltip
chartId={1}
yAccessor={ema50.accessor()}
tooltipContent={tooltipContent([ema20, ema50])}
fontSize={15} />
</ChartCanvas>
);
}
}
CandleStickChartWithHoverTooltip.propTypes = {
data: React.PropTypes.array.isRequired,
width: React.PropTypes.number.isRequired,
ratio: React.PropTypes.number.isRequired,
type: React.PropTypes.oneOf(["svg", "hybrid"]).isRequired,
};
CandleStickChartWithHoverTooltip.defaultProps = {
type: "svg",
};
CandleStickChartWithHoverTooltip = fitWidth(CandleStickChartWithHoverTooltip);
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 => <CandleStickChartWithHoverTooltip data={data} type={type} />}</TypeChooser>, document.getElementById("chart"));
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React Stockcharts - CandleStickChartWithHoverTooltip 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("./CandleStickChartWithHoverTooltip.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