Chartjs-ChartJS – Set baseline above zero

1👍

The best way to do this (and obviously more work) would be to create a custom axes. Chart.js explains how to do this here:

https://www.chartjs.org/docs/latest/developers/axes.html

I’ve also create a fiddle where the datavalues are modified by a set baseline. I’ve modified the tooltip and y-axis label as well so that everything shows based on the baseline. It’s not as elegant as a custom axis, but it’s quick and gets the job done (assuming the baseline is known). You can check out the fiddle here:

https://jsfiddle.net/tkngc02h/1/

And, in case you just want to see the code, here it is:

<!doctype html>
<html>

<head>
    <title>Line Styles</title>
    <script src="https://www.chartjs.org/dist/2.9.3/Chart.min.js"></script>
    <script src="https://www.chartjs.org/samples/latest/utils.js"></script>
    <style>
    canvas{
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
    }
    </style>
</head>

<body>
    <div style="width:75%;">
        <canvas id="canvas"></canvas>
    </div>
    <script>
    var baseline = 1;

        var config = {
            type: 'bar',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
                datasets: [{
                    label: 'TestData',
                    fill: false,
                    backgroundColor: window.chartColors.blue,
                    borderColor: window.chartColors.blue,
                    data: [
                        0 - baseline, 
            1.8 - baseline, 
            2.1 - baseline, 
            -0.2 - baseline, 
            1.3 - baseline, 
            -0.5 - baseline, 
            -0.23 - baseline, 
            0 - baseline
                    ],
                }]
            },
            options: {
                responsive: true,
                title: {
                    display: true,
                    text: 'Chart.js Line Chart',
                },
                tooltips: {
                    mode: 'index',
                    intersect: false,
                    callbacks: {
                        // Use the footer callback to display the sum of the items showing in the tooltip
                        label: function(tooltipItem, data) {
                                        var label = data.datasets[tooltipItem.datasetIndex].label || '';

                    if (label) {
                        label += ': ';
                    }
                    label += tooltipItem.yLabel + baseline;
                    return label;                       
                    },
                    },          
                },
                hover: {
                    mode: 'nearest',
                    intersect: true
                },
                scales: {
                    xAxes: [{
                        display: true,
                        scaleLabel: {
                            display: true,
                            labelString: 'Month'
                        }
                    }],
                    yAxes: [{
                        display: true,
                        scaleLabel: {
                            display: true,
                            labelString: 'Value',
                        },

                ticks: {
                    // Include a dollar sign in the ticks
                    callback: function(value, index, values) {
                        return value + baseline;
                    }
                }            

                    }]
                }
            }
        };

        window.onload = function() {
            var ctx = document.getElementById('canvas').getContext('2d');
            window.myLine = new Chart(ctx, config);
        };
    </script>
</body>

</html>

Leave a comment