Built with blockbuilder.org
Last active
September 11, 2018 02:44
-
-
Save trasherdk/08fd3de47bdecd95247ea986eaaf7e14 to your computer and use it in GitHub Desktop.
D3 OHLC Chart with Random data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DAY_MS = 1000 * 60 * 60 * 24; | |
const skipbox = document.getElementById('skip'); | |
const updatebox = document.getElementById('update'); | |
// create some test data that skips weekends | |
const generator = fc.randomFinancial() | |
.filter(fc.randomSkipWeekends); | |
const data = generator(50); | |
const xTickFilter = d3.timeDay.filter((d) => d.getDay() === 1); | |
// use the date to determine the x extent, padding by one day at each end | |
const xExtent = fc.extentDate() | |
.accessors([d => d.date]) | |
.padUnit('domain') | |
.pad([DAY_MS, DAY_MS]) | |
// compute the y extent from the high / low values, padding by 10% | |
const yExtent = fc.extentLinear() | |
.accessors([d => d.high, d => d.low]) | |
.pad([0.1, 0.1]) | |
// Create the gridlines and series | |
const gridlines = fc.annotationSvgGridline() | |
.xTicks(xTickFilter); | |
const candlestick = fc.seriesSvgCandlestick(); | |
// add them to the chart via a multi-series | |
const multi = fc.seriesSvgMulti() | |
.series([gridlines, candlestick]); | |
// adapt the d3 time scale in a discontinuous scale that skips weekends | |
const skipWeekendScale = fc.scaleDiscontinuous(d3.scaleTime()) | |
.discontinuityProvider(fc.discontinuitySkipWeekends()); | |
function renderChart() { | |
// create a chart | |
const chart = fc.chartSvgCartesian( | |
skipbox.checked ? skipWeekendScale : d3.scaleTime(), | |
d3.scaleLinear() | |
) | |
.xDomain(xExtent(data)) | |
.yDomain(yExtent(data)) | |
.xTicks(xTickFilter) | |
.plotArea(multi); | |
// render the chart | |
d3.select('#streaming-chart') | |
.datum(data) | |
.call(chart); | |
} | |
function autoUpdate(){ | |
} | |
renderChart(); | |
skipbox.addEventListener('click', renderChart); | |
updatebox.addEventListener('click', renderChart); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!-- include polyfills for custom event, Symbol and Custom Elements --> | |
<script src="//unpkg.com/[email protected]/dist/polyfill.js"></script> | |
<script src="//unpkg.com/[email protected]/custom-event-polyfill.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/document-register-element/1.8.0/document-register-element.js"></script> | |
<!-- use babel so that we can use arrow functions and other goodness in this block! --> | |
<script src="//unpkg.com/babel-standalone@6/babel.min.js"></script> | |
<script src="//unpkg.com/[email protected]"></script> | |
<script src="//unpkg.com/[email protected]"></script> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 18px sans-serif; | |
} | |
.chart { | |
height: 480px; | |
} | |
</style> | |
<label><input type='checkbox' id='skip'/>Skip Weekends</label> | |
<label><input type='checkbox' id='update'/>Auto Update</label> | |
<div id='streaming-chart' class='chart'></div> | |
<script src='chart.js' type='text/babel'></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment