javascript - Chartjs 2.7.3 : Set Y data at the correct X position axis

标签 javascript chart.js chartjs-2.6.0

enter image description here

data: {
            labels: ['29 Oct, 18', '30 Oct, 18', '02 Nov, 18', '14 Nov, 18', '15 Nov, 18', '19 Nov, 18', '20 Nov, 18', '28 Nov, 18'],
            datasets: [{
                pointRadius: 0,
                label: 'Positive',
                lineTension: 0, 
                data: [{'x': '15 Nov, 18', 'y': 18636}],
                borderWidth: 1,
                backgroundColor: 'rgba(0, 255, 0, 0.5)', 
            },{
                pointRadius: 0,
                label: 'Negative',
                lineTension: 0, 
                data: [{'x': '29 Oct, 18', 'y': -20480}, {'x': '30 Oct, 18', 'y': -284}, {'x': '02 Nov, 18', 'y': -1625}, {'x': '14 Nov, 18', 'y': -6622}, {'x': '15 Nov, 18', 'y': -12991}, {'x': '19 Nov, 18', 'y': -1645}, {'x': '20 Nov, 18', 'y': -1230}, {'x': '28 Nov, 18', 'y': -39612}],
                borderWidth: 1,
                backgroundColor: 'rgba(255, 0, 0, 0.5)', 
            }]
        },

问题是绿色条在错误的 x 位置。目前是“29 okt”,但我用“15 nov”标记了它

如何将这些数据集设置到正确的 x 位置

最佳答案

由于您指定了数据集的 x/y 坐标,因此您必须xAxes 比例类型设置为 time 在您的图表选项中。

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

参见 official docs对于所有可能的配置或检查下面的工作示例中使用的选项。


重要:您必须将数据集中的日期格式更改为类似'YYYY-MM-DD' 的格式,否则moment 会扔这个warning .

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions.

var data = {
  labels: [
    "2018-10-29",
    "2018-10-30",
    "2018-11-02",
    "2018-11-14",
    "2018-11-15",
    "2018-11-19",
    "2018-11-20",
    "2018-11-28"
  ],
  datasets: [{
      pointRadius: 0,
      label: "Positive",
      lineTension: 0,
      data: [{
        x: "2018-11-15",
        y: 18636
      }],
      borderWidth: 1,
      backgroundColor: "rgba(0, 255, 0, 0.5)"
    },
    {
      pointRadius: 0,
      label: "Negative",
      lineTension: 0,
      data: [{
          x: "2018-10-29",
          y: -20480
        },
        {
          x: "2018-10-30",
          y: -284
        },
        {
          x: "2018-11-02",
          y: -1625
        },
        {
          x: "2018-11-14",
          y: -6622
        },
        {
          x: "2018-11-15",
          y: -12991
        },
        {
          x: "2018-11-19",
          y: -1645
        },
        {
          x: "2018-11-20",
          y: -1230
        },
        {
          x: "2018-11-28",
          y: -39612
        }
      ],
      borderWidth: 1,
      backgroundColor: "rgba(255, 0, 0, 0.5)"
    }
  ]
};
var options = {
  scales: {
    xAxes: [{
      type: "time",
      distribution: 'series',
      time: {
        displayFormats: {
          day: 'D MMM, YY'
        }
      },
      ticks: {
        source: "labels"
      },
      gridLines: {
        display: false
      }
    }]
  }
};

var ctx = document.getElementById("myChart").getContext("2d");

var myBarChart = new Chart(ctx, {
  type: "bar",
  data: data,
  options: options
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<canvas id="myChart" width="300" height="100"></canvas>

关于javascript - Chartjs 2.7.3 : Set Y data at the correct X position axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53934008/

相关文章:

javascript - 如何在 div 中显示选项值而不重复它(javascript 或 jquery)

javascript - 如何在 Chartjs 2.x 中使用图表插件 (Chart.pluginService.register) 访问标签数组?

javascript - 使用 JSON 文件中的数据动态创建元素

javascript - map 平铺算法

javascript - 使用 p5js 创建多个可点击的 Div

chart.js - Chartjs 中的重置缩放

javascript - 如何从图表中删除 y 轴标签?

javascript - 让 Chartjs 显示数据标签的键而不是值

javascript - Chart.js:如何在鼠标悬停时改变线的颜色和粗细?

chart.js 2 - 是否可以使用 HTML 格式化刻度标签?