score:5

Accepted answer

you can achieve this using chart.js plugins. they let you handle specific events triggered during the chart creation (beforeinit, afterupdate, afterdraw ...) and are also easy to implement :

chart.pluginservice.register({
    afterupdate: function(chart) {
        // this will be triggered after every update of the chart
    }
});

now you just have to loop through your dataset data model (the property used to draw charts) and add the offset you want :

chart.pluginservice.register({
    afterupdate: function(chart) {
        // we get the dataset and set the offset here
        var dataset = chart.config.data.datasets[0];
        var offset = 20;

        // for every data in the dataset ...
        for (var i = 0; i < dataset._meta[0].data.length; i++) {
            // we get the model linked to this data
            var model = dataset._meta[0].data[i]._model;

            // and add the offset to the `x` property
            model.x += offset;

            // .. and also to these two properties
            // to make the bezier curve fits the new graph
            model.controlpointnextx += offset;
            model.controlpointpreviousx += offset;
        }
    }
});

you can see your example working on this jsfiddle and here is its result :

enter image description here

score:2

leading on from the answer given by 'tektiv' i needed a similar solution but one that works for responsive charts.

so instead of using fixed measurements for the given offset shown in tektiv's plugin, we first count the number of objects in the dataset array. we then divide the chart.width by the number of objects in the array to give us equal segments, then in order to define the half way point between each grid line, we divide that sum by a factor of 2.

note 1: you could also replace the factor of 2 to a variable so the user could chose the portion of offset needed.

note 2: i've placed the plugin code within the chart script given i don't want this as a global affect by registering a global plugin.

note 3: this is second re-edit of my solution given the plugin code i partially copied from the answer given by 'tektiv' above was only firing successfully for the first time, but then when re-loading a new instance of the chart i experienced some null errors on the dataset._meta (worth also seeing answer here on this particular topic as this helped me fix and finalize my answer: dataset._meta[0].dataset is null in chartjs code example:

<script>

    var mychart;

    function drawchart() {

        var ctx = document.getelementbyid('mychart').getcontext('2d');
        ctx.innerhtml = '';

        if (mychart != null) {
            mychart.destroy();
        }

        var datasetarray = [5, 10.5, 18.2, 33.9, 121.2, 184.9, 179.9, 196.1, 158.3, 166.3, 66.4, 20.6, null];

        mychart = new chart(ctx, {
            type: 'line',
            data: {
                labels: ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec", ""],
                datasets: [{
                    data: datasetarray,
                    borderwidth: 2,
                    bordercolor: "#f37029",
                    pointbordercolor: "#f37029",
                    pointbackgroundcolor: "#f37029",
                    pointhitradius: 10,
                    spangaps: false,
                }]
            },
            plugins: [{
                afterupdate: function (chart, options) {
                    //..
                    var dataset = chart.config.data.datasets[0];

                    // get the number of objects in the dataset array.
                    var nodatapoints = datasetarray.length;

                    //alert(nodatapoints); // testing only, you'll notice that this 
                    // alert would fire each time the responsive chart is resized.
                    var xoffset = (chart.width / nodatapoints) / 2;

                    for (var i = 0; i < dataset.data.length; i++) {
                        for (var key in dataset._meta) {
                            var model = dataset._meta[key].data[i]._model;
                            model.x += xoffset;
                            model.controlpointnextx += xoffset;
                            model.controlpointpreviousx += xoffset;
                        }
                    }
                }
            }],
            options: {
                scales: {
                    xaxes: [{
                        gridlines: {
                            offsetgridlines: false,
                            display: true,
                        },
                        ticks: {
                            fontsize: 8,
                            maxrotation: 0
                        }
                    }],
                    yaxes: [{
                        gridlines: {
                            display: true
                        },
                        ticks: {
                            beginatzero: true,
                        }
                    }]
                },
                legend: {
                    display: false
                },
                responsive: true,
                maintainaspectratio: true
            }
        });
    }

</script>

first screenshot below shows the responsive chart stretched to a wide screen view:

wide screen

second screenshot shows the responsive chart resized to a smaller and more conventional window size:

narrow screen

score:10

check out - http://www.chartjs.org/docs/latest/axes/cartesian/ .

in chapter "common configuration",there is a boolean attribute offset. default value is false (except in case of bar chart)

if true, extra space is added to the both edges and the axis is scaled to fit into the chart area. this is set to true in the bar chart by default.

so you can just set it to true, and it should work.


Related Query

More Query from same tag