2

I would like to know how to view chart data as html table on button click in chartjs. I have implemented the line chart using chartjs , xaxis represents the color yaxis represents votes and points

Working fiddle:https://jsfiddle.net/miyavv/ehqrL20o/ I want to display chart as table

Color | # of Votes | # of Points
Red     12           7
Blue    19           11
... and so on
var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [
        {
          label: '# of Votes',
          data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
        },  
            {
                label: '# of Points',
                data: [7, 11, 5, 8, 3, 7],
                borderWidth: 1
            }
        ]
  },
  options: {
    scales: {
        yAxes: [{
        ticks: {
                    reverse: false
        }
      }]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
//html
<body>
    <canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>

4
  • Unfortunately, your question is not clear, please provide more info or sandbox sample. Commented Jan 11, 2020 at 12:26
  • @Alex thanks for reply, I want to display chart data as html table, Commented Jan 13, 2020 at 1:53
  • @Alex I have updated my question and fiddle Commented Jan 15, 2020 at 15:16
  • What's the problem with @SciFiThief answer? what do you mean by "chart as table"?please draw your desired result and attach to question. could help to clarify exactly what do you intend Commented Jan 15, 2020 at 21:36

1 Answer 1

1

You need to identify what in your chart data could be represented as columns and rows in the table. If you named your x-axis, you could represent the chart data like this:

Color | # of Votes | # of Points
Red     12           7
Blue    19           11
... and so on

Also, chart data can be represented as a crosstab which is a more direct representation (since datapoint lies where x and y intersects):

             | Red | Blue | ... and so on
# of Votes     12    19
# of Points    7     11

Here's the example of how you can convert the chart data to crosstab: https://jsfiddle.net/tara5/cjvuphkL/

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

2 Comments

thanks for reply, I have updated my question and added fiddle link
I see that you provided a fiddle link, but still, it doesn't have any code where you try to convert chart data to the table. Also, you could easily modify my example with the crosstab to a table (it is basically a table). You just need to swap rows and columns, and name the first column 'Colors'. The example has all the necessary code.

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.