you can extract values ββfrom a table using the jquery text () function, having built the correct data structure for chart.js, you can dynamically generate chart data from the table.
First of all, you need to extract the shortcuts from the table. you can select rows and, iterating over each row, you can get label data from the first columns.
function generateLabelsFromTable() { var labels = []; var rows = jQuery("tr"); rows.each(function(index){ if (index != 0)
Similarly, you can generate chart data, iterate the html table.
function generateDataSetsFromTable() { var data; var datasets = []; var rows = jQuery("tr"); rows.each(function(index){ if (index != 0) // we dont need first row of table { var cols = $(this).find("td"); var data = []; cols.each(function(innerIndex){ if (innerIndex!=0) // we dont need first columns of the row data.push($(this).text()); }); var dataset = { fillColor : colors[index%3].fillColor, strokeColor : colors[index%3].strokeColor, highlightFill: colors[index%3].highlightFill, highlightStroke: colors[index%3].highlightStroke, data : data } datasets.push(dataset); } }); return datasets; }
After writing these functions you can change your barChartData like this
var barChartData = { labels : generateLabelsFromTable(), datasets : generateDataSetsFromTable() };
You must also define an array of colors at the beginning to preserve the current style of the chart. This array is used above when assigning a chart dataset.
var colors = []; colors.push( { fillColor : "rgba(95,137,250,0.5)", strokeColor : "rgba(95,137,250,0.9)", highlightFill: "rgba(95,137,250,0.75)", highlightStroke: "rgba(95,137,250,1)" }); colors.push( { fillColor : "rgba(245,75,75,0.5)", strokeColor : "rgba(245,75,75,0.8)", highlightFill : "rgba(245,75,75,0.75)", highlightStroke : "rgba(245,75,75,1)" }); colors.push( { fillColor : "rgba(98,223,114,0.5)", strokeColor : "rgba(98,223,114,0.8)", highlightFill : "rgba(98,223,114,0.75)", highlightStroke : "rgba(98,223,114,1)", });
Remember to enable jquery.
Fiddle