score:1

Accepted answer

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>

Related Query

More Query from same tag