I am trying to refresh a Google Chart Dashboard, with data from the server. but I am getting an error with the ControlWrapper when I refresh the data. The initial load is working fine - but when I get another set of Data from the server I can't seem to use the Slider - I get an Error Message:
JSFIDDLE: https://jsfiddle.net/9aktz4s3/4/
Note: In the JSFiddle it doesn't work on Initial load because I have not set any default data - but the issue in JSFiddle is the same I get when I refresh data
This is my HTML:
<!--Div that will hold the dashboard-->
<div id="scorecard_dashboard_div" class="row">
<!--Divs that will hold each control and chart-->
<div id="scorecard_chart_div"></div>
<div id="scorecard_filter_div"></div>
</div>
And this is how I initialize the dashboard:
var scorecardChartData;
var scorecardChart;
var scorecardDashboard;
var scoreCardRangeSelector;
var scorecardChartOptions = {
legend: { position: "top" },
chartArea: { width: "90%" },
xptColumns: [{
id: "dateid", label: "Date", type: "date" }, {
id: "brakingid", label: "Brakings", type: "number" }, {
id: "distanceTravelledid", label: "Distance Travelled", type: "number" }, ]
};
function initViolationChart() {
scorecardChartData = new google.visualization.DataTable();
// add columns to the chart
for (let i = 0; i < scorecardChartOptions.xptColumns.length; i++) {
var column = scorecardChartOptions.xptColumns[i];
scorecardChartData.addColumn(column.type, column.label, column.id);
}
// Create a dashboard.
scorecardDashboard = new google.visualization.Dashboard(document.getElementById('scorecard_dashboard_div'));
// Create a range slider, passing some options
scoreCardRangeSelector = new google.visualization.ControlWrapper({
controlType: "ChartRangeFilter",
containerId: "scorecard_filter_div",
options: {
filterColumnIndex: 0,
ui: { chartOptions: {legend: {position: "top" },height: 50, chartArea: {width: '90%'}}} }
});
// Create the Chart Wrapper
scorecardChart = new google.visualization.ChartWrapper({
chartType: 'ColumnChart',
containerId: 'scorecard_chart_div',
options: {legend: {position: "top"},
chartArea: {width: "90%"},
}
});
// Bind the both with Dashboard
scorecardDashboard.bind(scoreCardRangeSelector, scorecardChart);
// Draw it
scorecardDashboard.draw(scorecardChartData);
}
Here is how I refresh the graph data:
refreshData function(){
var rows = [];
//NORMALLY HERE THERE WILL BE AN AJAX REQUEST TO GET DATA FROM SERVER
var items = jsonData.driverScorecardItems;
for (var i = 0; i < items.length; i++) {
var item = items[i];
//
var thisRow = [{
v: new Date(item.dateFormatted),
f: item.dateFormatted
}, {
v: item.harshBrakings
}, {
v: item.distanceTravelledKM
}];
rows.push(thisRow);
}
// Add the rows to my Data Table
scorecardChartData.addRows(rows);
// Redraw the Chart
scorecardDashboard.draw(scorecardChartData);
}
