Related
I am using chartjs to render a barchart. For this I need to pass two lists in the format like [1, 2, 3] & [3, 2, 1]. I am making an AJAX call to Django which returns the two lists (I have not added the code to get the data from the database yet). The graph works fine for one list but not sure how to pass the second list.
I tried to pass the two lists as json and tried to use each of the lists in the success function of the ajax call but the graph does not render properly. With one list the graph is working fine
below is the code for the ChartJs AJAX call
$.ajax({
async: pasys,
type: "GET",
url: purl,
data: pdata,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(ldata) {
var barData = {
labels: ["Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec", "Jan", "Feb", "Mar"],
datasets: [
{
label: "DL1",
backgroundColor: 'rgba(220, 220, 220, 0.5)',
pointBorderColor: "#fff",
data: ldata.data1
},
{
label: "Non-DL1",
backgroundColor: 'rgba(100, 200, 300, 0.5)',
pointBorderColor: "#aaa",
data: ldata.data2
}
]
};
var barOptions = {
responsive: true
};
var ctx2 =
document.getElementById("opendemandtrend").getContext("2d");
new Chart(ctx2, {type: 'bar', data: barData, options:barOptions});
* below is the code for the django view *
def gldh_productivitymetric_opendemandtrend_get(request):
lcompanyid = request.GET.get("pcompanyid")
lpmid = request.GET.get("ppmid")
data = json.dumps({"data1": "[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]",
"data2": "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]"})
return HttpResponse(data, content_type="application/json")
You're passing in strings as the values, instead of lists. Don't do that.
data = json.dumps({"data1": [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
"data2": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]})
<html>
<head>
<title>Dashboard Evelien</title>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load the Visualization API and the controls package.
google.charts.load('current', {'packages':['corechart', 'controls', 'table']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawDashboard);
// Callback that creates and populates a data table,
// instantiates a dashboard, a range slider and a pie chart,
// passes in the data and draws it.
function drawDashboard() {
// Create our data table.
var data = google.visualization.arrayToDataTable([
['geo_Stad', 'Name', 'Leningdelen', 'Hypotheek', 'Straat', 'Woonplaats', 'Datum_start', 'Datum_eind', 'Mv', 'looptijd'],
['Netherlands, Amsterdam', 'Aap' , 5, 500000, 'Kalverstraat', 'Amsterdam', new Date(2014, 3, 22), new Date(2016, 2, 28), 'm', 123],
['Netherlands, Rotterdam', 'Noot', 1, 70000, 'Beursplein', 'Rotterdam', new Date(2014, 10, 11), new Date(2017, 3, 20), 'm', 234],
['Netherlands, Rotterdam', 'Mies', 3, 300000, 'Stationsplein', 'Rotterdam', new Date(2013, 10, 1), new Date(2013, 12, 23), 'v', 564],
['Netherlands, Amsterdam', 'Wim' , 2, 222222, 'Dorpsstraat', 'Amsterdam', new Date(2010, 1, 2), new Date(2016, 10, 23), 'm', 456],
['Netherlands, Amsterdam', 'Zus' , 7, 600000, 'Stationsplein', 'Amsterdam', new Date(2007, 5, 22), new Date(2009, 2, 2), 'v', 385],
['Netherlands, Rotterdam', 'Jet' , 2, 100000, 'Dorpsstraat', 'Rotterdam', new Date(2014, 7, 7), new Date(2015, 2, 16), 'v', 964],
['Netherlands, Rotterdam', 'Teun', 1, 85670, 'Kerkstraat', 'Rotterdam', new Date(2014, 3, 22), new Date(2016, 11, 12), 'm', 356],
['Netherlands, Utrecht', 'Gijs', 1, 53400, 'Stationsstraat', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 6, 18), 'm', 356],
['Netherlands, Utrecht', 'Does', 1, 77200, 'Vreeburg', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 8, 8), 'm', 768],
['Netherlands', 'Does1', 0, 0, 'Amsterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does2', 0, 0, 'Rotterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 563],
['Netherlands', 'Does3', 0, 0, 'Utrecht', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does4', 0, 0, 'Nederland', null, new Date(2014, 3, 22), new Date(2016, 8, 8), '', 963]
]);
// Create a dashboard.
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));
// Create a range slider, passing some options
var donutRangeSlider = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'filter_div',
'options': {
'filterColumnLabel': 'Leningdelen'
}
});
var tableE = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'tableE_div',
'view': {'rows' : data.getFilteredRows([{column: 2, minValue: 1}])},
'options': {
'showRowNumber': 'true',
'width': '100%',
'height': '33%',
}
});
// Create a pie chart, passing some options
var pieChart = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'chart_div',
'view': {'columns': [1, 2],
'rows' : data.getFilteredRows([{column: 2, minValue: 1}])},
'options': {
'width': 400,
'height': 300,
'pieSliceText': 'value',
'legend': 'right'
}
});
// Create a bar chart, passing some options
var barChart = new google.visualization.ChartWrapper({
'chartType': 'BarChart',
'containerId': 'chartB_div',
'view': {'columns': [1, 2],
'rows' : data.getFilteredRows([{column: 2, minValue: 1}])},
'options': {
'width': 500,
'height': 300,
'legend': 'right'
}
});
// Establish dependencies, declaring that 'filter' drives 'pieChart',
// so that the pie chart will only display entries that are let through
// given the chosen slider range.
dashboard.bind(donutRangeSlider, [tableE, pieChart, barChart]);
// Draw the dashboard.
dashboard.draw(data);
}
</script>
</head>
<body>
<!--Div that will hold the dashboard-->
<div id="dashboard_div">
<table class="columns">
<tr>
<td><div id="filter_div" style="border: 1px solid #ccc"></div></td>
</tr>
<tr>
<td><div id="chart_div" style="border: 1px solid #ccc"></div></td>
<td><div id="chartB_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
<table>
<tr>
<td><div id="tableE_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
</div>
</body>
</html>
Hi,
I am trying to make a dashboard with many differend graphs, but I cannot solve the issue 'Invalid row index 5. Should be in the range [0-4].'
Now I have a very simple dashboard, it works when I use only the data columns the 2 simple graphs need. But I want more graphs with other columns. When I add more columns to the data, the error pops up. I think I need a ready Listener somewhere in the code, but I tried so many times using the answeres for others, but without success.
So will you please complete my code with a ready listener so the error will be solved? Thanks !
a 'ready' listener will not resolve the problem
the problem stems from...
'rows' : data.getFilteredRows([{column: 2, minValue: 1}])}
once the dashboard is drawn and the filter value changes,
there are rows that exist in the data, that do not exist in the dashboard
so getFilteredRows is returning more rows than the dashboard has access to
thus, the error is thrown --> Invalid row index
to correct, just use an overall DataView to draw the dashboard
var view = new google.visualization.DataView(data);
view.setRows(data.getFilteredRows([{column: 2, minValue: 1}]));
dashboard.draw(view);
see following working snippet...
google.charts.load('current', {'packages':['corechart', 'controls', 'table']});
google.charts.setOnLoadCallback(drawDashboard);
function drawDashboard() {
var data = google.visualization.arrayToDataTable([
['geo_Stad', 'Name', 'Leningdelen', 'Hypotheek', 'Straat', 'Woonplaats', 'Datum_start', 'Datum_eind', 'Mv', 'looptijd'],
['Netherlands, Amsterdam', 'Aap' , 5, 500000, 'Kalverstraat', 'Amsterdam', new Date(2014, 3, 22), new Date(2016, 2, 28), 'm', 123],
['Netherlands, Rotterdam', 'Noot', 1, 70000, 'Beursplein', 'Rotterdam', new Date(2014, 10, 11), new Date(2017, 3, 20), 'm', 234],
['Netherlands, Rotterdam', 'Mies', 3, 300000, 'Stationsplein', 'Rotterdam', new Date(2013, 10, 1), new Date(2013, 12, 23), 'v', 564],
['Netherlands, Amsterdam', 'Wim' , 2, 222222, 'Dorpsstraat', 'Amsterdam', new Date(2010, 1, 2), new Date(2016, 10, 23), 'm', 456],
['Netherlands, Amsterdam', 'Zus' , 7, 600000, 'Stationsplein', 'Amsterdam', new Date(2007, 5, 22), new Date(2009, 2, 2), 'v', 385],
['Netherlands, Rotterdam', 'Jet' , 2, 100000, 'Dorpsstraat', 'Rotterdam', new Date(2014, 7, 7), new Date(2015, 2, 16), 'v', 964],
['Netherlands, Rotterdam', 'Teun', 1, 85670, 'Kerkstraat', 'Rotterdam', new Date(2014, 3, 22), new Date(2016, 11, 12), 'm', 356],
['Netherlands, Utrecht', 'Gijs', 1, 53400, 'Stationsstraat', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 6, 18), 'm', 356],
['Netherlands, Utrecht', 'Does', 1, 77200, 'Vreeburg', 'Utrecht', new Date(2014, 3, 22), new Date(2016, 8, 8), 'm', 768],
['Netherlands', 'Does1', 0, 0, 'Amsterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does2', 0, 0, 'Rotterdam', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 563],
['Netherlands', 'Does3', 0, 0, 'Utrecht', 'Nederland', new Date(2014, 3, 22), new Date(2016, 8, 8), '', 467],
['Netherlands', 'Does4', 0, 0, 'Nederland', null, new Date(2014, 3, 22), new Date(2016, 8, 8), '', 963]
]);
var view = new google.visualization.DataView(data);
view.setRows(data.getFilteredRows([{column: 2, minValue: 1}]));
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));
var donutRangeSlider = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'filter_div',
'options': {
'filterColumnLabel': 'Leningdelen'
}
});
var tableE = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'tableE_div',
'options': {
'showRowNumber': 'true',
'width': '100%',
'height': '33%',
}
});
var pieChart = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'chart_div',
'view': {
'columns': [1, 2]
},
'options': {
'width': 400,
'height': 300,
'pieSliceText': 'value',
'legend': 'right'
}
});
var barChart = new google.visualization.ChartWrapper({
'chartType': 'BarChart',
'containerId': 'chartB_div',
'view': {
'columns': [1, 2]
},
'options': {
'width': 500,
'height': 300,
'legend': 'right'
}
});
dashboard.bind(donutRangeSlider, [tableE, pieChart, barChart]);
dashboard.draw(view);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="dashboard_div">
<table class="columns">
<tr>
<td><div id="filter_div" style="border: 1px solid #ccc"></div></td>
</tr>
<tr>
<td><div id="chart_div" style="border: 1px solid #ccc"></div></td>
<td><div id="chartB_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
<table>
<tr>
<td><div id="tableE_div" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
</div>
I can't get my multi-line chart to animate on start up -
While I'm fetching data points for my multi line graph from the DB, I've added a sample of what the points would be in my code below:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['line']});
google.setOnLoadCallback(drawChart);
function drawChart()
{
var data = new google.visualization.DataTable();
data.addColumn('timeofday', 'Time');
data.addColumn('number', 'Count of Users');
data.addColumn('number', 'Count of Items');
data.addColumn('number', 'Count of Locations');
data.addRows([
[[8,07,06],1,181,181],
[[8,08,52],1,73,73],
[[8,09,43],1,204,204],
[[8,10,06],1,209,209],
[[8,10,27],1,334,334],
[[8,10,47],1,345,345],
[[8,12,24],1,852,852],
[[8,13,52],1,317,317],
[[8,43,01],1,538,538],
[[8,51,53],1,50,50],
[[8,55,01],1,50,50],
[[8,55,30],1,99,99],
[[8,56,27],1,51,51],
[[8,57,11],1,50,50],
[[8,57,58],1,50,50],
[[8,58,32],1,50,50],
[[8,59,42],1,16,16],
[[9,08,54],1,16,16],
[[9,09,41],1,2,2],
[[9,10,07],1,9,9],
[[9,10,53],1,35,35],
[[9,11,31],1,4,4],
[[9,11,45],1,3,3]
]);
var options = {
title: 'Updated every 3 Hours',
chart: {
animation: {
duration: 2000,
easing: 'linear',
startup: true
},
},
width: 1000,
height: 500
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
As of now a static multi line graph is rendered:
enter image description here
There are multiple issues with your code(your console should have logged them)
08 and 09 are not valid Number-values(should be 8 and 9)
you load the wrong package(you must load corechart)
animation is not a property of chart
fixed chart:
google.load('visualization', '1.1', {
packages: ['corechart']
});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('timeofday', 'Time');
data.addColumn('number', 'Count of Users');
data.addColumn('number', 'Count of Items');
data.addColumn('number', 'Count of Locations');
data.addRows([
[
[8, 7, 06], 1, 181, 181
],
[
[8, 8, 52], 1, 73, 73
],
[
[8, 9, 43], 1, 204, 204
],
[
[8, 10, 06], 1, 209, 209
],
[
[8, 10, 27], 1, 334, 334
],
[
[8, 10, 47], 1, 345, 345
],
[
[8, 12, 24], 1, 852, 852
],
[
[8, 13, 52], 1, 317, 317
],
[
[8, 43, 01], 1, 538, 538
],
[
[8, 51, 53], 1, 50, 50
],
[
[8, 55, 01], 1, 50, 50
],
[
[8, 55, 30], 1, 99, 99
],
[
[8, 56, 27], 1, 51, 51
],
[
[8, 57, 11], 1, 50, 50
],
[
[8, 57, 58], 1, 50, 50
],
[
[8, 58, 32], 1, 50, 50
],
[
[8, 59, 42], 1, 16, 16
],
[
[9, 8, 54], 1, 16, 16
],
[
[9, 9, 41], 1, 2, 2
],
[
[9, 10, 07], 1, 9, 9
],
[
[9, 10, 53], 1, 35, 35
],
[
[9, 11, 31], 1, 4, 4
],
[
[9, 11, 45], 1, 3, 3
]
]);
var options = {
title: 'Updated every 3 Hours',
animation: {
duration: 2000,
easing: 'linear',
startup: true
},
//width: 1000,
//height: 500
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<div id="chart_div"></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
I have this geochart: https://jsfiddle.net/y2dkrpg1/
Is it possible to add some adition information (text) about each region (for example by clicking on the region)?
Thank you!
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawMarkersMap);
function drawMarkersMap() {
var data = google.visualization.arrayToDataTable([
['State', 'NS'],
['Blagoevgrad', 17],
['Burgas', 15],
['Varna', 27],
['Veliko Tarnovo', 24],
['Vidin', 30],
['Vratsa', 35],
['Gabrovo', 30],
['Dobrich', 37],
['Kardzhali', 23],
['Kyustendil', 17],
['Lovech', 20],
['Montana', 49],
['Pazardjik', 31],
['Pernik', 26],
['Pleven', 24],
['Plovdiv', 28],
['Razgrad', 25],
['Ruse', 28],
['Silistra', 49],
['Sliven', 17],
['Smolyan', 25],
['Sofia', 28],
['Stara Zagora', 20],
['Targovishte', 30],
['Haskovo', 40],
['Shumen', 40],
['Yambol', 30],
['Pazardzhik', 31],
['Sofia-grad', 12],
]);
var formatter = new google.visualization.NumberFormat({
fractionDigits: 0,
suffix: '%'
});
formatter.format(data, 1);
var options = {
region: 'BG',
displayMode: 'regions',
resolution: 'provinces',
};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
Write something in stead of 'Tadam' and the values.
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawMarkersMap);
function drawMarkersMap() {
var data = google.visualization.arrayToDataTable([
['State', 'NEETs','TADAM'],
['Blagoevgrad', 17, 100],
['Burgas', 15, 100],
['Varna', 27, 6],
['Veliko Tarnovo', 24, 34],
['Vidin', 30, 324],
['Vratsa', 35, 234],
['Gabrovo', 30, 32423],
['Dobrich', 37, 5],
['Kardzhali', 23, 5],
['Kyustendil', 17, 5],
['Lovech', 20, 345],
['Montana', 49, 43534],
['Pazardjik', 31, 34534],
['Pernik', 26, 34534],
['Pleven', 24, 3443],
['Plovdiv', 28, 34],
['Razgrad', 25, 100],
['Ruse', 28, 10],
['Silistra', 49, 1],
['Sliven', 17, 2],
['Smolyan', 25, 3],
['Sofia', 28, 4],
['Stara Zagora', 20, 5],
['Targovishte', 30, 6],
['Haskovo', 40, 7],
['Shumen', 40, 8],
['Yambol', 30, 9],
['Pazardzhik', 31, 10],
['Sofia-grad', 12, 11],
]);
var formatter = new google.visualization.NumberFormat({
fractionDigits: 0,
suffix: '%'
});
formatter.format(data, 1);
var options = {
region: 'BG',
displayMode: 'regions',
resolution: 'provinces',
};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
https://jsfiddle.net/y2dkrpg1/
I am currently testing google charts and having difficulty in specifying my own custom tooltips.
my data is declared as below
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addColumn('number', 'Cats');
data.addColumn({type: 'string', role: 'tooltip', p: {'html':true}});
data.addRows([0, 0, 0, 'hello'], [1, 10, 5, 'hello'],
[2, 23, 15, 'hello'],[3, 17, 9, 'hello'], [4, 18, 10, 'hello'],
[5, 9, 5, 'hello'],
see this jsFiddle http://jsfiddle.net/SecretSquirrel/rbwyhx1q/
It appears the custom tooltip is only affecting the first data column?
I thought it was pretty limited to only allow 1 tooltip per row, but if this is only 1 tooltip for the first column this is really quite useless.
Well you have to add another tooltip column for your second graft.
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addColumn({type: 'string', role: 'tooltip', p: {'html':true}});
data.addColumn('number', 'Cats');
data.addColumn({type: 'string', role: 'tooltip', p: {'html':true}});
data.addRows([
[0, 0,'custom', 0, 'hello'], [1, 10,'custom', 5, 'hello'], [2, 23,'custom', 15, 'hello'],
[3, 17,'custom', 9, 'hello'], [4, 18,'custom', 10, 'hello'], [5, 9,'custom', 5, 'hello'],
[6, 11,'custom', 3, 'hello'], [7, 27,'custom', 19, 'hello'], [8, 33,'custom', 25, 'hello'],
[9, 40,'custom', 32, 'hello'], [10, 32,'custom', 24, 'hello'], [11, 35,'custom', 27, 'hello'],
[12, 30,'custom', 22, 'hello'], [13, 40,'custom', 32, 'hello'], [14, 42,'custom', 34, 'hello'],
[15, 47,'custom', 39,'hello'], [16, 44,'custom', 36,'hello'], [17, 48,'custom', 40, 'hello'],
[18, 52,'custom', 44,'hello'], [19, 54,'custom', 46,'hello'], [20, 42,'custom', 34, 'hello'],
[21, 55,'custom', 47,'hello'], [22, 56,'custom', 48,'hello'], [23, 57,'custom', 49,'hello']
]);
var options = {
width: 1000,
height: 563,
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Popularity'
},
series: {
1: {curveType: 'function'}
}
};
var chart = new google.visualization.LineChart(document.getElementById('ex2'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<div id="ex2"></div>