javascript - Chartjs, plot data based with unequal time intervals - Stack Overflow

I'm plotting a graph with chartjs where the x-axis represents time and the y-axis the correspondin

I'm plotting a graph with chartjs where the x-axis represents time and the y-axis the corresponding data.

Now i got data for today, last week, last month and last year.

var myLineChart = new Chart(ctx).Line(data, options);
var data = {
    labels: ["last year", "last month", "last week", "today"],
    datasets: [
        {
            label: "My First dataset",
            data: [80, 81, 56, 40]
        }
    ]

};

When i plot the graph, the distance between every point is the same. But this is not correct because the time intervals are not the same.

The distance between 'last year' and 'last month' should be bigger than the distance between 'last week' and 'last month'.

Anyone an idea how i can achieve a this with chartjs, when i look at the documentation i don't see any options for this.

I'm plotting a graph with chartjs where the x-axis represents time and the y-axis the corresponding data.

Now i got data for today, last week, last month and last year.

var myLineChart = new Chart(ctx).Line(data, options);
var data = {
    labels: ["last year", "last month", "last week", "today"],
    datasets: [
        {
            label: "My First dataset",
            data: [80, 81, 56, 40]
        }
    ]

};

When i plot the graph, the distance between every point is the same. But this is not correct because the time intervals are not the same.

The distance between 'last year' and 'last month' should be bigger than the distance between 'last week' and 'last month'.

Anyone an idea how i can achieve a this with chartjs, when i look at the documentation i don't see any options for this.

Share Improve this question asked May 13, 2015 at 13:22 mgoubertmgoubert 3231 gold badge5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You have to use an extension to chart.js that has data for both x and y values in the data structure. One that I found is Chart.Scatter.

The longer explanation is that by default, the labels you use in chart.js are just labels; they don't get parsed into any kind of x values.

You can use the Chart.js scatter chart type (https://www.chartjs/docs/latest/charts/scatter.html). Also remember to set showLine: true in the datasets options.

I found that this resulted in gaps at the start and end of the x axis. You can feed in the min and max ticks for x axis as follows:

scales: {
  xAxes: [
    {
      display: false,
      ticks: { max: maxX, min: minX }
    }
  ],
  yAxes: [
    {
      display: false
    }
  ]
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741647663a4358479.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信