简体   繁体   English

Chart.js 在折线图中绘制数字 X 和 Y 的问题

[英]Chart.js issue in plotting numeric X and Y in line chart

I have bellow Code to display a Chart using Chart.js.我有波纹管代码来使用 Chart.js 显示图表。

<canvas id="canvasline" width="200" height="200"></canvas>

<script>
var ctx = document.getElementById('canvasline').getContext('2d');
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['0', '30', '50'],
    datasets: [{
      data: [
        { x: "0", y: 0 },
        { x: "10", y: 10 },
        { x: "20", y: 20 },
        { x: "30", y: 30 },
        { x: "40", y: 40 },
        { x: "50", y: 50 },
      ],
      borderWidth: 1
    }]
  },
  options: {
      legend: {
        display: false
      },
      scales: {
      yAxes: [{
        ticks: {
          display: true,
          suggestedMin: 0,
          suggestedMax: 100,
          maxTicksLimit: 10
        },        
        gridLines: {
          display: true
        }
      }],
      xAxes: [{
        ticks: {
          display: true,
          suggestedMin: 0,
          suggestedMax: 100,
          maxTicksLimit: 3
        },        
        gridLines: {
          display: true
        }
      }]
    }
  }
});
</script>

Working Code Example:工作代码示例:
https://jsfiddle.net/2vcrsq6n/ https://jsfiddle.net/2vcrsq6n/

I am facing the below issues:我面临以下问题:

  1. For the X-Axis label "30", I see X data "10" is getting displayed对于 X 轴标签“30”,我看到 X 数据“10”正在显示
  2. At the runtime I get an JSON array with X and Y values as an array, I want to plot this X and Y numbers on the graph how should I implement it.在运行时,我得到一个 JSON 数组,其中包含 X 和 Y 值作为数组,我想在图表上绘制这个 X 和 Y 数字,我应该如何实现它。

When the data is specified as individual points through objects that contain an x and an y property, you should not define data.labels .当数据通过包含xy属性的对象指定为单个点时,您不应定义data.labels

Also make sure, the x and y values are numbers but not strings.还要确保xy值是数字而不是字符串。

Please take a look at your amended code below that uses the most recent stable version of Chart.js (2.9.3).请查看下面使用最新稳定版本 Chart.js (2.9.3) 的修改后的代码。

 var ctx = document.getElementById('myChart').getContext('2d'); var myChart = new Chart(ctx, { type: 'scatter', data: { datasets: [{ data: [ { x: 0, y: 0 }, { x: 10, y: 10 }, { x: 20, y: 20 }, { x: 30, y: 30 }, { x: 40, y: 40 }, { x: 50, y: 50 } ], showLine: true, fill: false, borderWidth: 1 }] }, options: { legend: { display: false }, scales: { yAxes: [{ ticks: { suggestedMin: 0, suggestedMax: 100, maxTicksLimit: 10 } }], xAxes: [{ ticks: { suggestedMin: 0, suggestedMax: 100, maxTicksLimit: 3 } }] } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script> <canvas id="myChart"></canvas>

To update your chart with new data, simply add or replace the data in the dataset and invoke chart.update() afterwards.要使用新数据更新图表,只需添加或替换数据集中的数据,然后调用chart.update()

myChart.data.datasets[0].data = <new data>;
myChart.update();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM