9951 explained code solutions for 126 technologies


javascript-d3How do I create a candlestick chart in d3.js?


Creating a candlestick chart in d3.js is quite simple.

First, you need to set up the data that will be used to create the chart. This data should represent the open, close, high, and low values of the data points. For example:

var data = [
  {"open": 20, "close": 30, "high": 40, "low": 10},
  {"open": 10, "close": 25, "high": 35, "low": 5},
  {"open": 15, "close": 30, "high": 45, "low": 5},
];

Next, you need to set up the scales for the chart. These will be used to map the data points to the chart. For example:

var xScale = d3.scaleLinear()
    .domain([0, data.length])
    .range([0, width]);

var yScale = d3.scaleLinear()
    .domain([d3.min(data, d => d.low), d3.max(data, d => d.high)])
    .range([height, 0]);

Finally, you need to create the candlestick elements. This can be done using the rect element in d3. For example:

var candles = svg.selectAll("rect")
    .data(data)
    .enter()
    .append("rect")
    .attr("x", (d, i) => xScale(i))
    .attr("y", d => yScale(d.high))
    .attr("width", xScale(1) - xScale(0) - barPadding)
    .attr("height", d => yScale(d.low) - yScale(d.high))
    .attr("fill", d => d.open < d.close ? "green" : "red");

The code above will create a candlestick chart with each candlestick having a width of xScale(1) - xScale(0) - barPadding, a height of yScale(d.low) - yScale(d.high), and a color of green or red depending on whether the open value is less than the close value.

Helpful links

Edit this code on GitHub