Chart.js 折线图未显示

Posted

技术标签:

【中文标题】Chart.js 折线图未显示【英文标题】:Chart.js line chart is not displaying 【发布时间】:2015-04-08 09:24:13 【问题描述】:

我用chart.js 构建了一个折线图。但是图表没有显示出来。这是我的 html

    <canvas id="myChart" width"600" height:"600"></canvas>

这是我的 javascript。这种方法我没有收到任何错误,但没有任何显示。

var c = $('#myChart');
var ct = c.get(0).getContext('2d');
var ctx = document.getElementById("myChart").getContext("2d").Line(data);


var data = 
labels: ["January", "February", "March", "April", "May", "June", "July","August", "November", "December"],
datasets: [
    
        label: "Sodium intake",
        fillColor: "rgba(220,220,220,0.2)",
        strokeColor: "rgba(220,220,220,1)",
        pointColor: "rgba(220,220,220,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(220,220,220,1)",
        data: [165, 159, 180, 181, 156, 155, 140]
    ,
    
        label: "Sugar intake",
        fillColor: "rgba(151,187,205,0.2)",
        strokeColor: "rgba(151,187,205,1)",
        pointColor: "rgba(151,187,205,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(151,187,205,1)",
        data: [128, 148, 140, 119, 186, 127, 190]
    
]

;

我使用的另一种方法与文档类似。我实例化了一个新图表。但是,这种方法为我的折线图返回 undefined。

var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).line(data,
belzierCurve : false

);

【问题讨论】:

【参考方案1】:

在您的 html 上:

<canvas id="myChart" width"600" height:"600"></canvas>

应该是:

<canvas id="myChart"  ></canvas>

然后,替换您的代码

var c = $('#myChart');
var ct = c.get(0).getContext('2d');
var ctx = document.getElementById("myChart").getContext("2d").Line(data);

使用此代码

var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);

所以在你的 js 中

var data = 
    //your data here

enter code here

然后在你的数据之后添加这一行

var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);

所以你的代码应该是这样的:

var data = 
  labels: ["January", "February", "March", "April", "May", "June", "July","August", "November", "December"],
  datasets: [
      
          label: "Sodium intake",
          fillColor: "rgba(220,220,220,0.2)",
          strokeColor: "rgba(220,220,220,1)",
          pointColor: "rgba(220,220,220,1)",
          pointStrokeColor: "#fff",
          pointHighlightFill: "#fff",
          pointHighlightStroke: "rgba(220,220,220,1)",
          data: [165, 159, 180, 181, 156, 155, 140]
      ,
      
          label: "Sugar intake",
          fillColor: "rgba(151,187,205,0.2)",
          strokeColor: "rgba(151,187,205,1)",
          pointColor: "rgba(151,187,205,1)",
          pointStrokeColor: "#fff",
          pointHighlightFill: "#fff",
          pointHighlightStroke: "rgba(151,187,205,1)",
          data: [128, 148, 140, 119, 186, 127, 190]
      
  ]


var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);

【讨论】:

【参考方案2】:

好的,试试这个:

首先更正您的 html:

<canvas id="myChart"  ></canvas>

然后为你的 javascript 试试这个:

var data = 
    "labels": ["January", "February", "March", "April", "May", "June", "July", "August", "November", "December"],
    "datasets": [
        label: "Sodium intake",
        fillColor: "rgba(220,220,220,0.2)",
        strokeColor: "rgba(220,220,220,1)",
        pointColor: "rgba(220,220,220,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(220,220,220,1)",
        data: [165, 159, 180, 181, 156, 155, 140]
    , 
        label: "Sugar intake",
        fillColor: "rgba(151,187,205,0.2)",
        strokeColor: "rgba(151,187,205,1)",
        pointColor: "rgba(151,187,205,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(151,187,205,1)",
        data: [128, 148, 140, 119, 186, 127, 190]
    ]


var chartDisplay = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);

应该没问题。请参阅jsfiddle of it。

【讨论】:

【参考方案3】:

原因可能是您使用的是最新的 chart.min.js,但使用的是旧代码。如需正确参考,请遵循 chartjs.org 文档。

代码配置结构在最新版本中发生了变化。 (我猜是从 2.3 开始)

所以,而不是

var countries= document.getElementById("countries").getContext("2d");
var chart = new Chart(countries).Pie(pieData,pieOptions);

我们的结构如下:

var countries= document.getElementById("countries").getContext("2d");
var chart = new Chart(countries,
  type: 'pie',
  data: 
  
    labels: ['India', 'Germany', 'Netherland'],
    datasets: 
        [
        backgroundColor: '#337ab7',
        data: ['100', '99' ,'98'],
        borderWidth:1,
        ]
  ,
  options:
  
      responsive: true,
      maintainAspectRatio:false,
      legend: 
        display: false,
        position: 'top'
      
  
);

var countries= document.getElementById("countries").getContext("2d");
var pieData = 

    labels: ['India', 'Germany', 'Netherland'],
    datasets: [
      backgroundColor: '#337ab7',
      data: ['100', '99' ,'98'],
      borderWidth:1,
      ]
;
var pieOptions = 

    responsive: true,
    maintainAspectRatio:false,
    legend: 
      display: false,
      position: 'top'
    
;

var chart = new Chart(countries,    
type: 'pie',
data: pieData,
options: pieOptions
);

【讨论】:

以上是关于Chart.js 折线图未显示的主要内容,如果未能解决你的问题,请参考以下文章

Chart.js - 如何为随时间发生的大量数据创建自动缩放折线图

Chart.js - 折线图格式数据到 Lacs/Crres

web页面 chart.js 怎样在折线图的点上显示对应数值?

如何使用 Chart.js 向我的折线图添加点击事件

折线图不适用于 time chart.js 类型

向 Chart.js 折线图添加标题