javascript - Chart.js 折线图,具有正确间隔的 x 标签

标签 javascript chart.js

默认情况下,chart.js 似乎均匀地间隔开其 x 标签,而不是在空间中绘制 (x,y) 对。

var options = {
  type: 'line',
  data: {
  labels: [0, 1, 10],
  datasets: [{
      data: [1, 2, 1]
    }]
  },
}

这会将“1”标签放置在 x 轴上 0 和 10 之间的中间 ( here's a fiddle )。如何让它定位到 10 的 1/10?

最佳答案

根据 Chart.js xAxis linear scale : strange behavior您需要一个线性 x 轴,并以 x,y 对形式提供数据。

所以,它需要是:

var ctx = document.getElementById('chartJSContainer').getContext('2d');
var scatterChart = new Chart(ctx, {
            type: 'line',
            data: {
                datasets: [{
                    label: 'Linear Dataset',
                    data: [{ x: 0, y: 1}
                          ,{x: 1, y: 2 }
                          ,{ x: 10, y: 1} ]
                          }]
                  },
            options: {
                scales: {
                    xAxes: [{
                        type: 'linear'
                        ,position: 'bottom'
                    }]
                }
            }
        });

关于javascript - Chart.js 折线图,具有正确间隔的 x 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55622348/

相关文章:

javascript - 什么库在 Javascript 中模拟 OpenGL

javascript - 客户端修改JS

javascript - 图表JS为图例添加最大值

javascript - 条形图中的链接 (charts.js)

javascript - 同时使用 chart.js 版本 1.0.2 和 2.0

chart.js - 无法在 Charts.js v2.1.4 上使 y 轴从零开始

chart.js - Chartjs : Is it possible to hide the data labels on the axis but show up on the graph on hover?

javascript - 如何在事件阶段更改时间线圆圈的颜色?

javascript - 使用 jQuery 根据搜索关键字过滤表数据

javascript - 如何为 imgProgress.js 插件创建动画?