How to edit one of two Google charts on the one page? - charts

I've added Google chart to my page. This returns an images of a charts. It is ok. But I want to edit only one of them and this is problem. It edit only the first one, but I want to edit for example only the second one. Can anybody help me? Here is my code:
//draw charts
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Value');
data.addColumn('number', 'Total count');
data.addRows([
["A", 4],
["B", 1],
]);
var options = {
title: "Choose something"
};
var chart = new google.visualization.PieChart(document.getElementById(304));
chart.draw(data, options);
var data305 = new google.visualization.DataTable();
data305.addColumn('string', 'Value');
data305.addColumn('number', 'Total count');
data305.addRows([
["Male", 2],
["Female", 2],
]);
var options305 = {
title: "Gender"
};
var chart305 = new google.visualization.PieChart(document.getElementById(305));
chart305.draw(data305, options305);
}
//edit charts
google.load('visualization', '1.0', {packages: ['charteditor']});
var chartEditor = null;
function loadEditor() {
// Create the chart to edit.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Value');
data.addColumn('number', 'Total count');
data.addRows([
["A", 4],
["B", 1],
]);
var wrapper = new google.visualization.ChartWrapper({
chartType: 'PieChart',
dataTable: data,
options: {'title': 'Pohlavi'},
containerId: '304'
});
chartEditor = new google.visualization.ChartEditor();
google.visualization.events.addListener(chartEditor, 'ok', redrawChart);
chartEditor.openDialog(wrapper, {});
//---------------------------------------
// Create the chart to edit.
var data305 = new google.visualization.DataTable();
data305.addColumn('string', 'Value');
data305.addColumn('number', 'Total count');
data305.addRows([
["Male", 2],
["Female", 2],
]);
var wrapper305 = new google.visualization.ChartWrapper({
chartType: 'PieChart',
dataTable: data,
options: {'title': 'Pohlavi'},
containerId: '305'
});
chartEditor305 = new google.visualization.ChartEditor();
google.visualization.events.addListener(chartEditor305, 'ok', redrawChart);
chartEditor.openDialog(wrapper305, {});
}
// On "OK" save the chart to a <div> on the page.
function redrawChart(inputId){
chartEditor.getChartWrapper().draw(document.getElementById(304));
}
function redrawChart305(inputId){
chartEditor305.getChartWrapper().draw(document.getElementById(305));
}
and here ienter code heres my html:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<button onclick="loadEditor()">Click me</button>
<div id="304" style="height: 400px; width: 600px;"></div>
<button onclick="loadEditor()">Click me</button>
<div id="305" style="height: 400px; width: 600px;"></div>
<div id="vis_div" style="height: 400px; width: 600px;"></div>
Where is mistake?
Thanks

Related

Google line chart remove background color

I can't find the answer for this one on SO or in the google docs.
Considering the following configuration how can I remove this super annoying background? Desperately trying to find the right property in the docs without success: https://developers.google.com/chart/interactive/docs/gallery/linechart
chart: {
type: 'LINE',
options: {
width: '100%',
colors: [colors.primary.fresh],
// what property to target here? I've tried most...
}
}
areaOpacity: 0 is the option I think you are looking for...
HOWEVER, this option only works on Area charts, not Line charts.
not sure it will work for you.
see following Area chart, with the fill color removed.
google.charts.load('current', {
packages: ['controls']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'y');
data.addRows([
[0, 4000],
[1, 3800],
[2, 5600],
[3, 5800],
[4, 5400],
[5, 5000]
]);
var chart = new google.visualization.ChartWrapper({
chartType: 'AreaChart',
containerId: 'chart_div',
dataTable: data,
options: {
areaOpacity: 0
}
});
chart.draw();
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
see following Line chart, with the fill color added (doesn't work).
google.charts.load('current', {
packages: ['controls']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'y');
data.addRows([
[0, 4000],
[1, 3800],
[2, 5600],
[3, 5800],
[4, 5400],
[5, 5000]
]);
var chart = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'chart_div',
dataTable: data,
options: {
areaOpacity: 0.3
}
});
chart.draw();
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
and just for reference, see following Area chart, with the fill color added.
google.charts.load('current', {
packages: ['controls']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'x');
data.addColumn('number', 'y');
data.addRows([
[0, 4000],
[1, 3800],
[2, 5600],
[3, 5800],
[4, 5400],
[5, 5000]
]);
var chart = new google.visualization.ChartWrapper({
chartType: 'AreaChart',
containerId: 'chart_div',
dataTable: data,
options: {
areaOpacity: 0.3
}
});
chart.draw();
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

how to fix `visualization.ComboChart` when using string,timeofday,number together on google chart?

I'm trying to create a ComboChart including three columns types string, timeofday, number but I Facing an error "All series on a given axis must be of the same data type" even the column types is there, how to fix that ?
please check the link: https://jsfiddle.net/minamagdy666/2jfn1uec/3/
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Time of Day');
data.addColumn('timeofday', 'Time of Day');
data.addColumn('number', 'Emails Received');
data.addRows([
['eee',[8, 30, 45], 5],
['eee',[9, 0, 0], 10],
]);
var options = {
title: 'Total Emails Received Throughout the Day',
height: 450,
series: {0:{type: 'line'}}
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
first, need to define the seriesType for the chart.
'line', 'area', 'bars', 'candlesticks', or 'steppedArea'
seriesType: 'bars'
next, if each series is not the same type,
(in this case one is number, the other timeofday)
then each series must be on its own y-axis...
1: {targetAxisIndex: 1}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Time of Day');
data.addColumn('timeofday', 'Time of Day');
data.addColumn('number', 'Emails Received');
data.addRows([
['eee',[8, 30, 45], 5],
['eee',[9, 0, 0], 10],
]);
var options = {
title: 'Total Emails Received Throughout the Day',
height: 450,
seriesType: 'bars',
series: {
0:{
type: 'line'
},
1: {
targetAxisIndex: 1
}
}
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Google Line Chart - Show Data Values on Points by default

I want to show data values on the line chart, i.e. which appear on mouse over, i need them all displayed by default. I tried the solution here Google Charts API: Always show the Data Point Values in Graph
data.addColumn({type: 'number', role: 'annotation'});
I added this to my code (I'm not sure where to put it exactly) but It didn't make any change.
Here is part of my code:
<!DOCTYPE html>
<html>
<head>
<title>Week/Orders</title>
<script type="text/javascript" src="loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart(){
var data = new google.visualization.DataTable();
var data = google.visualization.arrayToDataTable([
['Week','Orders'],
<?php
while($row = mysqli_fetch_assoc($aresult)){
echo "['".$row["Week"]."', ".$row["Orders"]."],";
}
?>
]);
var options = {
title: 'Week Vs Orders',
'width':1700,
'height':600,
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curvechart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="curvechart" style="width: 900px; height: 400px"></div>
</body>
</html>
I have two values; Week and Orders. I need only the "Order" value displayed on the graph by default. How can I do this? Thanks.
you can use a DataView to add annotations to the chart
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
calc: 'stringify',
sourceColumn: 1,
type: 'string',
role: 'annotation'
}]);
see following working snippet...
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart(){
var data = google.visualization.arrayToDataTable([
['Week','Orders'],
['1', 5],
['2', 6],
['3', 4]
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
calc: 'stringify',
sourceColumn: 1,
type: 'string',
role: 'annotation'
}]);
var options = {
title: 'Week Vs Orders',
width: 1700,
height: 600,
legend: {position: 'bottom'}
};
var chart = new google.visualization.LineChart(document.getElementById('curvechart'));
chart.draw(view, options); // <-- use data view
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="curvechart"></div>

GeoCharts how to show country names instead of codes

I would like to make a visualization with the google GeoChart. I put in country codes. I would like to display the full country names but only the country code pops up.
google.charts.load('current', {
'packages': ['geochart'],
'mapsApiKey': 'AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY'
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Members'],
['LB', 3],
['JO', 2],
['IT', 24],
['PS', 3],
['LY', 1],
['TN', 6],
['LB', 3],
['EG', 2],
]);
var options = {
colorAxis: {
colors: ['#c1c1cd', '#53556e']
},
legend: 'none'
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="regions_div" style="width: 900px; height: 500px;"></div>
using object notation,
you can provide the value (v:) and the formatted value (f:)
{v: 'IT', f: 'Italy'}
the tooltip will display the formatted value by default
update the data using object notation and provide the name as the formatted value
see following working snippet,
hover over Italy to see the result...
google.charts.load('current', {
'packages': ['geochart'],
'mapsApiKey': 'AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY'
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Members'],
['LB', 3],
['JO', 2],
[{v: 'IT', f: 'Italy'}, 24],
['PS', 3],
['LY', 1],
['TN', 6],
['LB', 3],
['EG', 2],
]);
var options = {
colorAxis: {
colors: ['#c1c1cd', '#53556e']
},
legend: 'none'
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="regions_div" style="width: 900px; height: 500px;"></div>
Based on the WhiteHat's answer I added some code to automatize the introduction of the formatted values.
// iso to name conversion
// https://gist.github.com/maephisto/9228207
var isoCountries = {
'IT': 'Italy',
'JO': 'Jordan',
'LB': 'Lebanon',
};
function getCountryName(countryCode) {
if (isoCountries.hasOwnProperty(countryCode)) {
return isoCountries[countryCode];
} else {
return countryCode;
}
};
// Load the Visualization API and the corechart package.
google.charts.load('current', {
'packages': ['geochart'],
'mapsApiKey': 'AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY'
});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawRegionsMap);
// Callback that creates and populates a data table,
// instantiates the chart, passes in the data and
// draws it.
function drawRegionsMap() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Country');
data.addColumn('number', 'Members');
data.addRows([
['LB', 3],
['JO', 2],
['IT', 24],
]);
// Set chart options
var options = {
colorAxis: {
colors: ['#c1c1cd', '#53556e']
},
legend: 'none',
backgroundColor: '#efefef'
};
// Create new data table for iso and full country name
var dataMod = new google.visualization.DataTable();
dataMod.addColumn('string', 'Country');
dataMod.addColumn('number', 'Members');
// Adding rows with iso and full country name
var numRows = data.getNumberOfRows();
for (var i = 0; i < numRows; i++) {
dataMod.addRows([
[{
v: String(data.getValue(i, 0)),
f: String(getCountryName(data.getValue(i, 0)))
}, data.getValue(i, 1)],
]);
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(dataMod, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="regions_div" style="width: 900px; height: 500px;"></div>

Display Single point on google material line chart

If I have a single data row like this:
google.charts.load('current', {'packages':['line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'User');
data.addRows([
[new Date(), 41],
]);
var options = {
chart: {
title: 'Active Users',
},
width: 900,
height: 500
};
var chart = new google.charts.Line(document.getElementById('linechart_material'));
chart.draw(data, options);
}
How to display a point in such kind of situation? Right now, the point is there but it is not highlighted.This problem is only in material chart not in classic.
the option for pointSize needs to be set for a single row of data
however, it is among the several options that do not work on Material charts
see this issue --> Tracking Issue for Material Chart Feature Parity #2143
instead, recommend using Core chart, with following option...
theme: 'material'
see following working snippet...
google.charts.load('current', {
callback: drawChart,
packages:['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'User');
data.addRows([
[new Date(), 41],
]);
var options = {
height: 500,
theme: 'material',
title: 'Active Users',
width: 900
};
if (data.getNumberOfRows() === 1) {
options.pointSize = 5;
}
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>