0

I need to create charts using chart.js , however I have created a basic one using the chart.js documentation. But now I want the data source to be HTML Table instead of Array object.

Here is my code:

<!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>

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

Any help would be appreciated. Thanks and Regards

1 Answer 1

0

This question Looping through table data with jquery has answers that discuss how to pull data from a table using jquery. Ideally if your table is be being generated from server side you could pass that data set into a JSON data set to be used on the page by both chart and table.

Sign up to request clarification or add additional context in comments.

1 Comment

this should be a comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.