Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js xAxis linear scale : strange behavior

I'm trying to use linear scale on x-axis in my chart.js chart. I add some code beause stackoverflow makes it obligatory when adding a jsfiddle url, but I don't see the point :

var options={
    scales:{
    xAxes:[{ type: "linear"}]
  }
};

I'm getting a very strange chart (2nd one) : http://jsfiddle.net/t0krmau8/

In the first chart, I'd like to get more space between 2 and 4 (2 times more space than between 1 and 2), that's why I'm using a linear scale.

Am I using the linear scale wrong? Or should I use something else?

Thanks

like image 457
jaudo Avatar asked Apr 19 '26 18:04

jaudo


1 Answers

You're not providing the data in correct format for the scatter line plot.

The correct format to provide the data is described by the following example from Chart.js Docs.

var scatterChart = new Chart(ctx/* your canvas context*/, {
 type: 'line',
    data: {
        datasets: [{
            label: 'Scatter Dataset',
            data: [{
                x: -10,
                y: 0
            }, {
                x: 0,
                y: 10
            }, {
                x: 10,
                y: 5
            }]
        }]
    },
    options: {
        scales: {
            xAxes: [{
                type: 'linear',
                position: 'bottom'
            }]
        }
    }
});

source

I think the x and y should be separable into different arrays, but you can always do a combination step and combine them into objects.

like image 184
Minato Avatar answered Apr 21 '26 07:04

Minato