chart.js - 带有 {x, y} 点数据的折线图仅显示 2 个值

标签 chart.js

https://codepen.io/shaz1234/pen/PEKjOV

代码笔有我的代码

new Chart(document.getElementById("chartjs-0"),                    {
    "type":"line",
    "data": {
              "datasets": [
                 { 
                   "label":"My First Dataset",
                   "data": [
                     {x: 0, y: 65}, 
                     {x: 4, y: 59}, 
                     {x: 100, y: 80}, 
                     {x: 110, y: 81}, 
                     {x: 125, y: 56}
                    ],
                   "fill":false,
                   "borderColor":"rgb(75, 192, 192)",
                   "lineTension":0.1
                 }
              ]
             },
             "options":{
             }
          }
     );

非常简单的示例,但图表仅显示前两个点。我本来希望图表可以缩放到提供的最小值和最大值 x值。我是否误解了点折线图的设计原理,还是我有错误?

提前感谢您的观看。

最佳答案

为了帮助那些在 2019 年(Chart.js 2.0)陷入困境的人,请参见下文:

对于那些喜欢深入细节并形成自己的推论的人,您可以引用 https://www.chartjs.org/samples/latest/scales/time/line-point-data.html 中的 chartjs 示例。 (去查看它的来源)。

对于那些想要快速回答的人:简而言之,您可以维护现有代码,但添加以下选项:(为了完整性,我重新附加了 Shaz 的代码)

new Chart(document.getElementById("chartjs-0"), {
    "type":"line",
    "data": {
              "datasets": [
                 { 
                   "label":"My First Dataset",
                   "data": [
                     {x: 0, y: 65}, 
                     {x: 4, y: 59}, 
                     {x: 100, y: 80}, 
                     {x: 110, y: 81}, 
                     {x: 125, y: 56}
                    ],
                   "fill":false,
                   "borderColor":"rgb(75, 192, 192)",
                   "lineTension":0.1
                 }
              ]
             },
             // this is the part that will make it work... see .. xAxes type:'linear'
             "options":{
                 responsive: true, 
                 title: {
                     // optional: your title here
                 },
                 scales: {
                     xAxes: [{
                           type: 'linear', // MANDATORY TO SHOW YOUR POINTS! (THIS IS THE IMPORTANT BIT) 
                           display: true, // mandatory
                           scaleLabel: {
                                display: true, // mandatory
                                labelString: 'Your label' // optional 
                           },
                      }], 
                     yAxes: [{ // and your y axis customization as you see fit...
                        display: true,
                        scaleLabel: {
                             display: true,
                             labelString: 'Count'
                        }
                    }],
                }
            }
        }
     ); 

关于chart.js - 带有 {x, y} 点数据的折线图仅显示 2 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48081521/

相关文章:

chart.js - 当在任意值之上/之下绘制时,如何更改 Chart.js 线条的颜色?

javascript - 从数组向 Chart.JS 添加标签和颜色

javascript - React 渲染函数阻止每次调用时创建新的 Canvas 元素 - Charts.js

python - 如何用scrapy抓取交互式图表?

javascript - 在堆叠水平条形图中如何删除 Chart.js 中的垂直线?

javascript - ChartJS 无法在 Vue 中工作

javascript - Chartjs scaleLabel位置

javascript - 循环遍历 Chart.js 输入的数组

javascript - 如何使用 {x,y} 对象数组来制作图表?

angular - 在 PrimeNG 中隐藏图例