Google Charts: Why am I getting two select events when I have animation turned on - google-visualization

I attach a select event to charts -- a column chart and a pie chart
When I turn on animation, I get two select events for a single column select action from the column chart (but just one, correctly, from the pie chart, which doesn't animate).
For the column chart, the first event returns the correct selection data [{column:1, row:1}], but the second event returns an empty array.
With animation turned off (commented out) I get the correct response, namely only one event when a user selects a column.
Has anyone seen this? What do I do? I haven't found any value that lets me even differentiate whether the event is occurring in the presence of animation, or any other logical way to at least filter out the rogue second event. Obviously I would like to suppress the incorrect second event when animation is turned on.
Here's the animation spec:
let options = {
animation:{
startup: true,
duration: 500,
easing: 'out',
},

the 'select' event on a ColumnChart seems to be working fine with animation here,
see following working snippet...
is there more you can share?
are you creating the charts directly or using the ChartWrapper class?
which packages are you using, 'corechart'?
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('timeofday', 'Time of Day');
data.addColumn('number', 'Motivation Level');
data.addColumn('number', 'Energy Level');
data.addRows([
[{v: [8, 0, 0], f: '8 am'}, 1, .25],
[{v: [9, 0, 0], f: '9 am'}, 2, .5],
[{v: [10, 0, 0], f:'10 am'}, 3, 1],
[{v: [11, 0, 0], f: '11 am'}, 4, 2.25],
[{v: [12, 0, 0], f: '12 pm'}, 5, 2.25],
[{v: [13, 0, 0], f: '1 pm'}, 6, 3],
[{v: [14, 0, 0], f: '2 pm'}, 7, 4],
[{v: [15, 0, 0], f: '3 pm'}, 8, 5.25],
[{v: [16, 0, 0], f: '4 pm'}, 9, 7.5],
[{v: [17, 0, 0], f: '5 pm'}, 10, 10],
]);
var view = new google.visualization.DataView(data);
view.setColumns([{sourceColumn:0, type: 'string', calc: 'stringify'}, 1]);
var options = {
animation:{
startup: true,
duration: 500,
easing: 'out',
},
hAxis: {
title: 'Time of Day',
format: 'h:mm a',
viewWindow: {
min: [7, 30, 0],
max: [17, 30, 0]
}
},
isStacked: true,
title: 'Motivation and Energy Level Throughout the Day',
vAxis: {
title: 'Rating (scale of 1-10)'
}
};
var chartCol = new google.visualization.ColumnChart(document.getElementById('column_div'));
var chartPie = new google.visualization.PieChart(document.getElementById('pie_div'));
google.visualization.events.addListener(chartCol, 'select', function () {
showSelection(chartCol);
});
google.visualization.events.addListener(chartPie, 'select', function () {
showSelection(chartPie);
});
function showSelection(sender) {
document.getElementById('msg_div').innerHTML += (new Date()).getTime() + ' -- ' + JSON.stringify(sender.getSelection()) + '<br/>';
}
chartCol.draw(data, options);
chartPie.draw(view, {
legend: 'none',
theme: 'maximized'
});
},
packages: ['corechart']
});
div {
padding: 6px 6px 6px 6px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="column_div"></div>
<div id="pie_div"></div>
<div id="msg_div"></div>

Related

Google column charts X-axis label different from value

I'd like to create column chart with X axis numeric values 1, 2, 3, 4 ... N and Y value of course different on every column.
I can't find out how to change labels on X line under bars, to string. For example - 1 could be marked as Elephant, 2 as Horse etc.
I could use string as X values, but then there is no way to get zoom working. At least, I didn't find any way to get it working.
simple example with strings, I'd like to achieve same appearance as this one, but with numeric values on X axis.
google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawBasic);
function drawBasic() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'animal');
data.addColumn('number', 'count');
data.addRows([
['Elephant', 5],
['Horse', 2],
['Dog', 7],
['Cat', 4],
]);
var options = {
explorer: {
axis: 'horizontal',
keepInBounds: true,
},
title: 'Testing',
hAxis: {
title: 'Animal',
},
vAxis: {
title: 'number'
}
};
var chart = new google.visualization.ColumnChart(
document.getElementById('chart_div'));
chart.draw(data, options);
}
Chart should look like this, but with working zoom:
Chart example
to use string labels on a continuous axis,
you will need to provide your own ticks
using object notation, provide the value (v:) and formatted value (f:)
{v: 1, f: 'Elephant'}
see following working snippet...
google.charts.load('current', {
callback: drawBasic,
packages: ['corechart']
});
function drawBasic() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'animal');
data.addColumn('number', 'count');
data.addRows([
[1, 5],
[2, 2],
[3, 7],
[4, 4]
]);
var options = {
explorer: {
axis: 'horizontal'
},
title: 'Testing',
hAxis: {
ticks: [
{v: 1, f: 'Elephant'},
{v: 2, f: 'Horse'},
{v: 3, f: 'Dog'},
{v: 4, f: 'Cat'}
],
title: 'Animal',
},
vAxis: {
title: 'number'
}
};
var chart = new google.visualization.ColumnChart(
document.getElementById('chart_div')
);
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

GeoChart: markers load very slowly

google.charts.load('current', {'packages':['geochart']});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var query = new google['visualization'].Query('https://docs.google.com/spreadsheets/d/____&headers=1&range=A1:B')
query.send(handleQueryResponseTR);
}
function handleQueryResponseTR(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var options = {
region: 'world',
displayMode: 'markers',
sizeAxis: { minValue: 0, maxValue: 5 },
colorAxis: {
colors: ['#fff', '#000']
},
legend: 'none'
};
var data = response.getDataTable();
var chart = new google.visualization.GeoChart(document.getElementById('geochart-colors'));
chart.draw(data, options);
};
The spreadsheet has the format:
Country Value
XX XX
XX XX
There are approximately 40 entries (40 different countries).
Everything works, but the map loads the markers one by one and it takes about 30 seconds to load all of them. Why is it so slow? It's not the fact that it's loading them from a Google Sheets, even when hard coded the load time is the same.
JSFIDDLE
when in markers mode format, using latitude and longitude seems to work best...
loads instantly...
use a data view to add the country name to the tooltip,
see following working snippet...
google.charts.load('current', {
callback: drawRegionsMap,
packages: ['geochart']
});
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Lat','Lng','Value','Country'],
[41.153332,20.168331,58,'Albania'],
[-25.274398,133.775136,28,'Australia'],
[40.143105,47.576927,47,'Azerbaijan'],
[23.684994,90.356331,52,'Bangladesh'],
[-3.373056,29.918886,76,'Burundi'],
[12.565679,104.990963,28,'Cambodia'],
[56.130366,-106.346771,79,'Canada'],
[-35.675147,-71.542969,48,'Chile'],
[26.820553,30.802498,7,'Egypt'],
[-16.578193,179.414413,127,'Fiji'],
[46.227638,2.213749,25,'France'],
[51.165691,10.451526,29,'Germany'],
[22.396428,114.109497,9,'Hong Kong'],
[20.593684,78.96288,119,'India'],
[-0.789275,113.921327,35,'Indonesia'],
[31.046051,34.851612,41,'Israel'],
[41.87194,12.56738,4,'Italy'],
[30.585164,36.238414,102,'Jordan'],
[-0.023559,37.906193,121,'Kenya'],
[29.31166,47.481766,59,'Kuwait'],
[33.854721,35.862285,127,'Lebanon'],
[55.169438,23.881275,3,'Lithuania'],
[23.634501,-102.552784,10,'Mexico'],
[9.081999,8.675277,48,'Nigeria'],
[30.375321,69.345116,91,'Pakistan'],
[31.952162,35.233154,66,'Palestinian Territories'],
[12.879721,121.774017,80,'Philippines'],
[51.919438,19.145136,242,'Poland'],
[25.354826,51.183884,86,'Qatar'],
[45.943161,24.96676,35,'Romania'],
[61.52401,105.318756,133,'Russia'],
[23.885942,45.079162,15,'Saudi Arabia'],
[1.352083,103.819836,2,'Singapore'],
[48.669026,19.699024,9,'Slovakia'],
[35.907757,127.766922,41,'South Korea'],
[40.463667,-3.74922,111,'Spain'],
[7.873054,80.771797,97,'Sri Lanka'],
[-6.369028,34.888822,34,'Tanzania'],
[13.443182,-15.310139,129,'Gambia'],
[38.963745,35.243322,2,'Turkey'],
[23.424076,53.847818,85,'United Arab Emirates'],
[55.378051,-3.435973,56,'United Kingdom'],
[48.379433,31.16558,97,'Ukraine'],
[37.09024,-95.712891,130,'United States'],
[6.42375,-66.58973,120,'Venezuela'],
[45.1,15.2,20,'Croatia']
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, 2, {
calc: function (dt, row) {
return dt.getValue(row, 3) + ': ' + dt.getFormattedValue(row, 2);
},
role: 'tooltip',
type: 'string'
}]);
var options = {
displayMode: 'markers',
};
var chart = new google.visualization.GeoChart(document.getElementById('geochart-colors'));
chart.draw(view, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="geochart-colors"></div>
note: found country lat / lng list here -->
countries.csv
see following snippet for comparing load time with country name only...
google.charts.load('current', {
callback: drawRegionsMap,
packages: ['geochart']
});
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Value'],
[{v: 'Albania'}, {v: 58}],
[{v: 'Australia'}, {v: 28}],
[{v: 'Azerbaijan'}, {v: 47}],
[{v: 'Bangladesh'}, {v: 52}],
[{v: 'Burundi'}, {v: 76}],
[{v: 'Cambodia'}, {v: 28}],
[{v: 'Canada'}, {v: 79}],
[{v: 'Chile'}, {v: 48}],
[{v: 'Egypt'}, {v: 7}],
[{v: 'Fiji'}, {v: 127}],
[{v: 'France'}, {v: 25}],
[{v: 'Germany'}, {v: 29}],
[{v: 'Hong Kong'}, {v: 9}],
[{v: 'India'}, {v: 119}],
[{v: 'Indonesia'}, {v: 35}],
[{v: 'Israel'}, {v: 41}],
[{v: 'Italy'}, {v: 4}],
[{v: 'Jordan'}, {v: 102}],
[{v: 'Kenya'}, {v: 121}],
[{v: 'Kuwait'}, {v: 59}],
[{v: 'Lebanon'}, {v: 127}],
[{v: 'Lithuania'}, {v: 3}],
[{v: 'Mexico'}, {v: 10}],
[{v: 'Nigeria'}, {v: 48}],
[{v: 'Pakistan'}, {v: 91}],
[{v: 'Pakistan Balochistan'}, {v: 55}],
[{v: 'Palestine, State of'}, {v: 66}],
[{v: 'Philippines'}, {v: 80}],
[{v: 'Poland'}, {v: 242}],
[{v: 'Qatar'}, {v: 86}],
[{v: 'Romania'}, {v: 35}],
[{v: 'Russian Federation'}, {v: 133}],
[{v: 'Saudi Arabia'}, {v: 15}],
[{v: 'Singapore'}, {v: 2}],
[{v: 'Slovakia'}, {v: 9}],
[{v: 'Korea, Republic of'}, {v: 41}],
[{v: 'Spain'}, {v: 111}],
[{v: 'Sri Lanka'}, {v: 97}],
[{v: 'Tanzania, United Republic of'}, {v: 34}],
[{v: 'Gambia'}, {v: 129}],
[{v: 'Turkey'}, {v: 2}],
[{v: 'United Arab Emirates'}, {v: 85}],
[{v: 'United Kingdom'}, {v: 56}],
[{v: 'Ukraine'}, {v: 97}],
[{v: 'United States'}, {v: 130}],
[{v: 'Venezuela, Bolivarian Republic of'}, {v: 120}],
[{v: 'Croatia'}, {v: 20}]
]);
var options = {
displayMode: 'markers',
};
var chart = new google.visualization.GeoChart(document.getElementById('geochart-colors'));
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="geochart-colors"></div>
EDIT
remove the coordinates from the tooltip by using option --> tooltip.isHtml: true
tooltip: {
isHtml: true
}
and the following css...
.google-visualization-tooltip-item:first-child {
display: none;
visibility: hidden;
}
the tooltip column must have a property for --> p: {html: true}
and for some reason, column properties are not recognized
when using a data view to draw the chart
to correct, convert the view to a data table before drawing...
view.toDataTable()
see following working snippet...
google.charts.load('current', {
callback: drawRegionsMap,
packages: ['geochart']
});
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Lat','Lng','Value','Country'],
[41.153332,20.168331,58,'Albania'],
[-25.274398,133.775136,28,'Australia'],
[40.143105,47.576927,47,'Azerbaijan'],
[23.684994,90.356331,52,'Bangladesh'],
[-3.373056,29.918886,76,'Burundi'],
[12.565679,104.990963,28,'Cambodia'],
[56.130366,-106.346771,79,'Canada'],
[-35.675147,-71.542969,48,'Chile'],
[26.820553,30.802498,7,'Egypt'],
[-16.578193,179.414413,127,'Fiji'],
[46.227638,2.213749,25,'France'],
[51.165691,10.451526,29,'Germany'],
[22.396428,114.109497,9,'Hong Kong'],
[20.593684,78.96288,119,'India'],
[-0.789275,113.921327,35,'Indonesia'],
[31.046051,34.851612,41,'Israel'],
[41.87194,12.56738,4,'Italy'],
[30.585164,36.238414,102,'Jordan'],
[-0.023559,37.906193,121,'Kenya'],
[29.31166,47.481766,59,'Kuwait'],
[33.854721,35.862285,127,'Lebanon'],
[55.169438,23.881275,3,'Lithuania'],
[23.634501,-102.552784,10,'Mexico'],
[9.081999,8.675277,48,'Nigeria'],
[30.375321,69.345116,91,'Pakistan'],
[31.952162,35.233154,66,'Palestinian Territories'],
[12.879721,121.774017,80,'Philippines'],
[51.919438,19.145136,242,'Poland'],
[25.354826,51.183884,86,'Qatar'],
[45.943161,24.96676,35,'Romania'],
[61.52401,105.318756,133,'Russia'],
[23.885942,45.079162,15,'Saudi Arabia'],
[1.352083,103.819836,2,'Singapore'],
[48.669026,19.699024,9,'Slovakia'],
[35.907757,127.766922,41,'South Korea'],
[40.463667,-3.74922,111,'Spain'],
[7.873054,80.771797,97,'Sri Lanka'],
[-6.369028,34.888822,34,'Tanzania'],
[13.443182,-15.310139,129,'Gambia'],
[38.963745,35.243322,2,'Turkey'],
[23.424076,53.847818,85,'United Arab Emirates'],
[55.378051,-3.435973,56,'United Kingdom'],
[48.379433,31.16558,97,'Ukraine'],
[37.09024,-95.712891,130,'United States'],
[6.42375,-66.58973,120,'Venezuela'],
[45.1,15.2,20,'Croatia']
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, 2, {
calc: function (dt, row) {
return '<div><span>' + dt.getValue(row, 3) + '</span>: ' + dt.getFormattedValue(row, 2) + '</div>';
},
role: 'tooltip',
type: 'string',
p: {html: true}
}]);
var options = {
displayMode: 'markers',
tooltip: {
isHtml: true
}
};
var chart = new google.visualization.GeoChart(document.getElementById('geochart-colors'));
chart.draw(view.toDataTable(), options);
}
.google-visualization-tooltip-item:first-child {
display: none;
visibility: hidden;
}
.google-visualization-tooltip-item span {
font-weight: bold;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="geochart-colors"></div>

How to change selected column border color & width in Google Charts?

I am using Google Charts. I want to change border color & width of selected column. By default stroke color is white and width is 1. I want to change border color to black and width to 2.
Code :
var data = google.visualization.arrayToDataTable(mydata);
var options = {
width: 600,
height: 400,
legend: {position: 'top', maxLines: 4},
bar: {groupWidth: '50%'},
isStacked: true
};
var chart = new google.visualization.ColumnChart(document.getElementById('mydiv'));
chart.draw(data, options);
There is no build-in option to set this style, but you may ovveride these settings for stroke-color/width via CSS:
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawStacked);
function drawStacked() {
var data = new google.visualization.DataTable();
data.addColumn('timeofday', 'Time of Day');
data.addColumn('number', 'Motivation Level');
data.addColumn('number', 'Energy Level');
data.addRows([
[{v: [8, 0, 0], f: '8 am'}, 1, .25],
[{v: [9, 0, 0], f: '9 am'}, 2, .5],
[{v: [10, 0, 0], f:'10 am'}, 3, 1],
[{v: [11, 0, 0], f: '11 am'}, 4, 2.25],
[{v: [12, 0, 0], f: '12 pm'}, 5, 2.25],
[{v: [13, 0, 0], f: '1 pm'}, 6, 3],
[{v: [14, 0, 0], f: '2 pm'}, 7, 4],
[{v: [15, 0, 0], f: '3 pm'}, 8, 5.25],
[{v: [16, 0, 0], f: '4 pm'}, 9, 7.5],
[{v: [17, 0, 0], f: '5 pm'}, 10, 10],
]);
var options = {
legend: {position: 'top', maxLines: 4},
isStacked: true};
var chart = new google.visualization.ColumnChart(document.getElementById('mydiv'));
chart.draw(data, options);
}
#mydiv svg>g>g>g>g>rect[stroke="#ffffff"][stroke-width="1"] {
stroke: black !important;
stroke-width: 2px !important;
}
<div id="mydiv"></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
You could set column style (border color & width) by applying Column styles on the selected column using select event as demonstrated below:
google.load("visualization", '1.1', { packages: ['corechart'] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General','Western', 'Literature'],
['2010', 10, 24, 20, 32, 18, 5],
['2020', 16, 22, 23, 30, 16, 9],
['2030', 28, 19, 29, 30, 12, 13]
]);
var options = {
width: 600,
height: 400,
legend: { position: 'top', maxLines: 3 },
bar: { groupWidth: '75%' },
isStacked: true,
};
var view = new google.visualization.DataView(data);
var chart = new google.visualization.ColumnChart(document.getElementById('columnchart_stacked'));
google.visualization.events.addListener(chart, 'select', function () {
highlightBar(chart,options,view);
});
chart.draw(data, options);
}
function highlightBar(chart,options,view) {
var selection = chart.getSelection();
if (selection.length) {
var row = selection[0].row;
var column = selection[0].column;
//1.insert style role column to highlight selected column
var styleRole = {
type: 'string',
role: 'style',
calc: function(dt, i) {
return (i == row) ? 'stroke-color: #000000; stroke-width: 2' : null;
}
};
var indexes = [0, 1, 2, 3, 4, 5, 6];
var styleColumn = findStyleRoleColumn(view)
if (styleColumn != -1 && column > styleColumn)
indexes.splice(column, 0, styleRole);
else
indexes.splice(column+1, 0, styleRole);
view.setColumns(indexes);
//2.redraw the chart
chart.draw(view, options);
}
}
function findStyleRoleColumn(view) {
for (var i = 0; i < view.getNumberOfColumns() ; i++) {
if (view.getColumnRole(i) == "style") {
return i;
}
}
return -1;
}
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script>
<div id="columnchart_stacked" style="width: 600px; height: 420px;"></div>
JSFiddle

Google charts floating min / max / average column chart

I'm trying to create a Google chart that looks like the following:
http://chart.googleapis.com/chart?cht=bvs&chs=200x125&chd=t2:10,50,60,80,40%7C50,60,100,40,20%7C30,70,90,95,45&chco=4d89f900,c6d9fd&chbh=20&chds=0,160&chm=H,336699,2,-1,1:22
Basically, I just want to represent the max, min, and average all on one chart, but I can't seem to figure out how to do this. I know it's possible using markers with the old URL-based charts, but they're being deprecated and it doesn't look like the new API supports markers yet.
I tried using candlesticks, but the only way I got it working was with a skinny line and a horizontal line in the middle, so it looked like a bunch of plus signs rather than floating columns with line markers. I know I could also technically stack a column chart with a stepped area chart, but then the line is continuous across all entries, which I don't want.
Thanks.
EDIT: Using jmac's method and intervals, I came up with this:
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'label');
data.addColumn('number', 'filler');
data.addColumn('number', 'range');
data.addColumn({type:'number', role:'interval'});
data.addRows([
['A', 3, 4, 2],
['B', 2, 5, 4],
['C', 4, 4, 1],
['D', 5, 2, 1],
['E', 1, 8, 4],
]);
// Create and draw the visualization.
var ac = new google.visualization.ColumnChart(document.getElementById('visualization'));
ac.draw(data, {
width: 600,
isStacked: true,
series: [{color:'transparent'},{color:'silver'},{color:'silver'}],
vAxis: {gridlines: {color: 'transparent'}, textPosition: 'none'},
focusTarget: 'category',
intervals: { 'style': 'bars', 'barWidth': 1.3, 'lineWidth': 2 },
});
}
I don't have enough reputation to post an image of what it looks like yet, but if you paste it in here you can see it: https://code.google.com/apis/ajax/playground/?type=visualization#column_chart
Also, since it still highlights the filler area when you mouse over it, I found a css hack to hide the highlighting on mouse over:
#chart-div {
svg g g g g rect {
stroke-width:0px;
}
}
You can use "box" style intervals to accomplish what you want:
function drawChart () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Category');
data.addColumn('number', 'Min');
data.addColumn('number', 'Average');
data.addColumn('number', 'Max');
data.addRows([
['Foo', 3, 5, 7],
['Bar', 5, 8, 10],
['Baz', 0, 2, 6],
['Bat', 1, 2, 4]
]);
var view = new google.visualization.DataView(data);
// duplicate 1 column as a dummy data series, and add intervals to it
view.setColumns([0, 1, {
id: 'min',
type: 'number',
role: 'interval',
calc: function (dt, row) {
return dt.getValue(row, 1);
}
}, {
id: 'avg',
type: 'number',
role: 'interval',
calc: function (dt, row) {
return dt.getValue(row, 2);
}
}, {
id: 'max',
type: 'number',
role: 'interval',
calc: function (dt, row) {
return dt.getValue(row, 3);
}
}, 1, 2, 3]);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(view, {
height: 400,
width: 600,
lineWidth: 0,
intervals: {
style: 'boxes'
},
legend: {
position: 'none'
},
series: {
0: {
// dummy data series, controls color of intervals
visibleInLegend: false,
color: 'blue',
enableInteractivity: false
},
1: {
// min series options
},
2: {
// average series options
},
3: {
// max series options
}
}
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
See working example: http://jsfiddle.net/asgallant/pvJpx/
If all you care about is how it looks visually, you can recreate this with a bit of finagling to have it look like this:
This is the code:
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['label', 'filler', 'bot half', 'top half'],
['A', 3, 2, 2],
['B', 2, 4, 1],
['C', 4, 1, 3],
['D', 5, 1, 1],
['E', 1, 4, 4],
]);
// Create and draw the visualization.
var ac = new google.visualization.ColumnChart(document.getElementById('visualization'));
ac.draw(data, {
width: 600,
isStacked: true,
series: [{color:'transparent'},{color:'silver'},{color:'silver'}],
vAxis: {gridlines: {color: 'transparent'}, textPosition: 'none'},
focusTarget: 'category',
});
}
This is a dumb workaround, but here are the steps given a min value, a max value, and an avg value:
Create a dummy (transparent) series equal to min
Create a second series for the bottom half of the bar equal to avg - min
Create a third series for the top half of the bar equal to max - avg
Although it looks right, the issue is that interaction with the chart will be real funky, in the sense that it won't show you what you would expect from the chart (you would have separate values that aren't showing min, max, and average, but only two values for the size of points 2) and 3) above). You can get around this with creative use of focusTarget, but that will still get you odd stuff like this:
Now you could theoretically rename your series, and use the {v:, f:} trick to make it look nicer, and that may be a good workaround, but it is very kludgy depending on your application. If you finagle it all nice and right, you would get something like this:
This is done with the following code:
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Series Name');
data.addColumn('number', 'Average');
data.addColumn('number', 'Minimum');
data.addColumn('number', 'Maximum');
data.addRows([
['A', {v:3, f:'5'}, {v:2, f:'3'}, {v:2, f:'7'}],
['B', {v:2, f:'6'}, {v:4, f:'2'}, {v:1, f:'7'}],
['C', {v:4, f:'5'}, {v:1, f:'4'}, {v:3, f:'8'}],
['D', {v:5, f:'6'}, {v:1, f:'5'}, {v:1, f:'8'}],
['E', {v:1, f:'5'}, {v:4, f:'1'}, {v:4, f:'9'}],
]);
// Create and draw the visualization.
var ac = new google.visualization.ColumnChart(document.getElementById('visualization'));
ac.draw(data, {
width: 600,
isStacked: true,
series: [{color:'transparent'},{color:'silver'},{color:'silver'}],
vAxis: {gridlines: {color: 'transparent'}, textPosition: 'none'},
focusTarget: 'category',
});
}
Again, this is kludgy and not perfect (see the grey box around the filler series, that can't be helped), but it will display the info, and it can be automated using some fancy javascript and/or formatters with dataviews depending on how often the charts need to be changed and what format you get your data in.

Google Line Charts, place circle on annotation

i am new to google charts i want to make a graph for cricket rate rate and wicket that should look something like this
i have searched google and found out that i might do it with the help of annotations and i have written this code:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawVisualization);
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Overs');
data.addColumn('number', 'Run-rate');
data.addColumn({type: 'string', role:'annotation'});
data.addColumn({type: 'string', role:'annotationText'});
data.addRows([
[1, 6, null, null],
[2, 6, null, null],
[10, 2, null, null],
[20, 3.2, null, 'Shoaib Malik'],
[21, 3, '2', 'Shahid Afridi'],
[30, 4, null, null],
[40, 5, 'B', 'This is Point B'],
[50, 6, null, null],
]);
var options = {
title: 'Run Rate',
pointSize:0,
hAxis: {
gridlines: {
color: 'transparent'
}
},
};
new google.visualization.LineChart(document.getElementById('chart_div')).
draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
and this is the output of the code:
now the problem is that i want to show circle like the first image instead of text 2,B
i cant do it using pointSize because i want circle where wicket falls, not where the over ends...
can any1 tell me how to do this ? either i can replace text with circle or any other way out
You can't replace the text if you want to use the annotation functionality (as the text is what is generated by the annotations). You could use an overlapping data series to show only certain points. Here's an example that shows an overlapping series (I removed the annotations for simplicity, but you can still use them if you want to):
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Overs');
data.addColumn('number', 'Run-rate');
data.addColumn('boolean', 'Wicket falls');
data.addRows([
[1, 6, false],
[2, 6, false],
[10, 2, true],
[20, 3.2, false],
[21, 3, true],
[30, 4, true],
[40, 5, false],
[50, 6, false]
]);
// create a DataView that duplicates points on the "Run Rate" series where "Wicket falls" is true
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
type: 'number',
label: data.getColumnLabel(2),
calc: function (dt, row) {
// return the value in column 1 when column 2 is true
return (dt.getValue(row, 2)) ? dt.getValue(row, 1) : null;
}
}]);
var options = {
title: 'Run Rate',
pointSize:0,
hAxis: {
gridlines: {
color: 'transparent'
}
},
series: {
0: {
// put any options pertaining to series 0 ("Run-rate") here
},
1: {
// put any options pertaining to series 1 ("Wicket Falls") here
pointSize: 6,
lineWidth: 0
}
}
};
new google.visualization.LineChart(document.getElementById('chart_div')).
// use the view instead of the DataTable to draw the chart
draw(view, options);
}
google.load('visualization', '1', {packages:['corechart'], callback: drawVisualization});
See working example here: http://jsfiddle.net/asgallant/saTWj/