使用chart.js

时间:2016-08-30 06:14:28

标签: chart.js

我需要使用chart.js创建图表,但是我使用chart.js文档创建了一个基本图表。但现在我希望数据源是HTML Table而不是Array对象。

这是我的代码:

<!DOCTYPE HTML>
<html>

<head></head>

<body>
  <canvas id="c" width="500" height="500"></canvas>
  <script src="chart.js"></script>
  <script>
    var ctx = document.getElementById("c").getContext("2d");

    var data = {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [{
        label: "My First dataset",
        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: [65, 59, 80, 81, 56, 55, 40]
      }, {
        label: "My Second dataset",
        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: [28, 48, 40, 19, 86, 27, 90]
      }]
    };
    var MyNewChart = new Chart(ctx).Line(data);
  </script>

  <table id="datatable">
    <thead>
        <tr>
            <th></th>
            <th>Jane</th>

            <th>Jordan</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>Apples</th>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <th>Pears</th>
            <td>2</td>
            <td>0</td>
        </tr>
        <tr>
            <th>Plums</th>
            <td>5</td>
            <td>11</td>
        </tr>
        <tr>
            <th>Bananas</th>
            <td>1</td>
            <td>1</td>
        </tr>
        <tr>
            <th>Oranges</th>
            <td>2</td>
            <td>4</td>
        </tr>
        <tr>
            <th>Carret</th>
            <td>6</td>
            <td>4</td>
        </tr>
    </tbody>
</table>
</body>

</html>

===================

任何帮助将不胜感激。 谢谢和问候

1 个答案:

答案 0 :(得分:0)

这个问题Looping through table data with jquery有答案讨论如何使用jquery从表中提取数据。理想情况下,如果您的表是从服务器端生成的,则可以将该数据集传递到JSON数据集中,以便在图表和表格的页面上使用。