How can I make a stepline or stepped chart in chart.js or D3?
Asked Answered
R

4

14

I'm able to accomplish this in Google Spreadsheets, below is a screenshot:

Google Spreadsheet Screenshot

Here's the small dataset in CSV

Buy PPU,Sell PPU,Net PPU
0.023,0.019,-0.000725
0.026,0.0165,-0.003725
0.021,0.021,0.00735
0.015,0.0165,0.0147
0.021,0.028,0.0168
0.018,0.028,0.0198

Any help is appreciated. I'm not seeing an example in either library of this specific kind of chart (called I believe "Stepped" or "Step line"), but I believe they are flexible enough to accomplish this?

Thank you

Rotten answered 9/10, 2014 at 20:32 Comment(0)
H
17

Nobody seem to have answered this, but you can do that with chart.js by setting steppedLine: true in the dataset configuration.

var config = {
        type: 'line',
        data: {
            datasets: [{
                label: "My dataset",
                data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
                steppedLine: true,
            }
   }
Hara answered 18/10, 2016 at 17:23 Comment(1)
In chart.js 3.5.1 the property is steppedAce
S
11

@Baz's answer is perfect for D3 version 3, but for anyone struggling to find similar resources for version 4 (the API changed pretty dramatically)..

The process now is to use d3.curveStepAfter instead.

documentation

example


An alternative approach is using a line and manually setting the interpolator, eg, d3.line().curve(d3.curveStepAfter)

Situation answered 24/10, 2017 at 19:53 Comment(1)
This is the correct answer for v4 - thank you. Too bad there can't be multiple accepted answers - for different contexts.Benefactress
R
2

You can use a d3.svg.line and set the line.interpolate to either step-before or step-after.

Here is the documentation:

https://github.com/mbostock/d3/wiki/SVG-Shapes#line_interpolate

Here is an example:

http://bl.ocks.org/mbostock/4342190

Rusch answered 9/10, 2014 at 20:47 Comment(0)
D
0

Chartjs provides the stepped line chart with the stepped property for various charts. For example, in the HTML document, add the canvas and with the id:

[html]

<canvas id="myChart" width="400" height="400"></canvas>
<script src="index.js"></script>

[index.js]

const ctx = document.getElementById('myChart').getContext('2d');

const data = {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
        label: '# of Votes',
        data: [5, 4, 3, 6, 2, 3],
        borderColor: '#FF0000',
        borderWidth: 2,
        stepped: 'after',
    }]
}

const myChart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
});

The stepped could be any of false, true, before, after, middle. Please see the docs for more information.

Danielledaniels answered 20/2, 2022 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.