D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
EmbraceLife
Full window
Github gist
candleBars
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis--x path { display: none; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } </style> <svg width="960" height="500"></svg> <script src="//d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"); var margin = {top: 20, right: 80, bottom: 30, left: 50}, width = svg.attr("width") - margin.left - margin.right, height = svg.attr("height") - margin.top - margin.bottom; var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // ---------- step 1 above: create svg and g -------------------------- var parseTime = d3.timeParse("%Y%m%d"); var x = d3.scaleTime().rangeRound([0, width]), // var x = d3.scaleBand().rangeRound([0, width]).padding(0.2), y = d3.scaleLinear().range([height, 0]); // ------------ step 3.a above: define scale functions with their ranges ---------- function type(d, _, columns) { d.Date = parseTime(+d.Date); for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c]; return d; } d3.csv("data.csv", type, function(error, data) { if (error) throw error; // console.log(data.columns); // console.log(data); // console.log(data[0].Date); // ---------------- step 2 above: get row-data and column-data ready ------------- x.domain(d3.extent(data, function(d){return d.Date;})); // x.domain(data.map(function(d){return d.Date})); // console.log(x.domain()); y.domain( [d3.min(data.map(function(d){return d.Low;})), d3.max(data.map(function(d){return d.High;}))] ); // console.log(y.domain()); // ------------------ step 3.b above: set scale-func domain ---------------------- g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y)) .append("text") .attr("x",20) .attr("y", 6) .attr("dy", "0.71em") .attr("text-anchor", "start") .attr("fill", "#000") .text("up-red, down-green"); var barWidth = width/data.length; // console.log(barWidth); var candlebar = g.selectAll(".bar") .data(data) .enter().append("g") .attr("class", ".bar") .attr("transform", function(d, i) { return "translate(" + i * barWidth + ",0)"; }); ; candlebar.append("line") .attr("x1", barWidth/2) .attr("x2", barWidth/2) .attr("y1", function(d){ return y(d.High);}) .attr("y2", function(d){ return y(d.Low);}) .attr("stroke-width", 0.01) .attr("stroke", "gray"); candlebar.append("rect") .attr("y", function(d) { if (d.Open > d.Close) { return y(d.Open) } else { return y(d.Close); }}) .attr("height", function(d) { if (d.Open > d.Close) { return (height - y(d.Open)) - (height - y(d.Close)); } else { if (d.Open < d.Close) { return (height - y(d.Close)) - (height - y(d.Open)); }}}) .attr("fill", function(d){ if (d.Open > d.Close) { return "green" ; } else { if (d.Open < d.Close) { return "red" ; }}}) .attr("width", barWidth); // ---------- step 4 above: draw x-y-axis, lines, ... ----------------------------- }); </script>
https://d3js.org/d3.v4.min.js