Google Pie Chart php mysql - php

**
I used Google Pie chart using PHP MySQL. If I put my mouse on the
chart it only shows 2 column data from the row. But there are three
columns in that table & I want to show all the columns from the row. I
have written this code to show 3rd column but it doesn't work.
**
var data = google.visualization.arrayToDataTable([
['Problems', 'Sufferer', 'Solutions'],
<?php
while($row = $res->fetch_assoc()){
echo "['".$row['Problems']."',".$row['sum(Sufferer)'].", '".$row['Solutions']."'],";
}
?>
]);
How can I solve this problem? picture of my Pie Chart output example

Google Pie chart only supports 2 columns refer Google Pie Chart
The first one is the slice label and the second one is the slice value.
If you wish to add more information to your chart, you can make use of the tooltip which is displayed on hover.
For more information on columns and roles, refer Column Roles
By default only the slice label and slice value with percentage will be shown in the tooltip of the chart.
This can be customized by passing data in the below format
Data format
var data = google.visualization.arrayToDataTable([
['Pizza', 'Popularity', {type:'string', role:'tooltip'}],
['Pepperoni', 33, "Most popular"],
]);
If you wish to customize the rendering of the tooltip, it can be achieved by passing HTML data as the content of the tooltip.
For more information on tooltips and customizing HTML content, refer Tooltips
Example
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Pizza', 'Popularity', {type:'string', role:'tooltip'}],
['Pepperoni', 33, "Most popular"],
['Hawaiian', 26, "Popular"],
['Mushroom', 22, "Somewhat popular"],
['Sausage', 10, "Less popular"]
]);
var options = {
title: 'Pizza Popularity'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="piechart" style="width: 900px; height: 500px;"></div>

Related

getImageURI() returning a big url that won't open in browser

I'm currently converting my google column chart into a png, and then getting the url using chart_div.innerHTML, paste it into browser (Firefox).
My error is : 413. That’s an error. Your client issued a request that was too large
Is there any way to reduce the size of the url ? or any possible solution around it ? my goal is to be able to convert all of my 4 charts to pngs and print each, or at least 1.
send the image to a new window,
this will allow you to right click and save as, print, etc...
window.open(chart.getImageURI(), '_blank');
the above creates a "pop-up" and may be blocked,
you can also change the location of the current page...
location.href = chart.getImageURI();
see following fiddle for an example...
https://jsfiddle.net/7qtuhwaw/
EDIT
an option for chrome is to create a download link,
see following working snippet...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', '2015');
data.addColumn('number', '2016');
data.addRows([
[new Date('01/01/2016'), 200, 210],
[new Date('02/01/2016'), 190, 220],
[new Date('03/01/2016'), 205, 200],
[new Date('04/01/2016'), 220, 230],
[new Date('05/01/2016'), 212, 210],
[new Date('06/01/2016'), 185, 193],
[new Date('07/01/2016'), 196, 207]
]);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'ready', function () {
downloadLink = document.createElement('a');
downloadLink.href = chart.getImageURI();
downloadLink.download = 'chart.png';
downloadLink.click();
});
chart.draw(data, {
hAxis: {
format: 'MMM'
},
height: 400
});
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
<div id="image_div"></div>
Example solution from google developpers as seen on the issues page of google charts.
Links:
Tutorial : https://developers.google.com/chart/interactive/docs/printing
Unless you have access to Google Servers, you will need to grab the charts in chunks. HTTP 413 means that the server declined your request, because it's just too big. Try splitting your chart into multiple charts and then either put it back together locally or just display the chunks.

remove pagination on google pie chart

i have following code to display pie chart.
<script>
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
//google.charts.setOnLoadCallback(drawChartTwo);
//google.charts.setOnLoadCallback(drawChartThree);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = google.visualization.arrayToDataTable([
['Type', 'Duration'],
<?php
foreach($val as $data){
echo "['".$data['type']."', ".$data['duration']."],";
}
?>
]);
// Set chart options
var options = {'title':'Duration Analysis',
'width':830,
'height':300,
'is3D': true,
pieSliceText : 'none',
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
</script>
and its working fine. but, now when i see chart on my browser i see it like.
in the image i see list of values in pagination with 3 page. but i want it to display right side of my chart without pagination. any possibility to display 3 colomns instead of 3 pagination page??? Please help me.

Grouping rows by column value/category in a Google pie chart

I have a table that looks like the following:
sub_note | item_amount | sub_cat |
---------------------------------------|
Spotify | $5.99 | entertainment |
Vodafone | $35 | phone |
Netflix | $5.99 | entertainment |
I want to display a Google pie chart that tells me how much is being spent per category i.e. Entertainment, Phone etc.
I have used the following adaptation of the Google Quick Start example, but all I'm seeing is a blank chart with 'No data'.
<script type="text/javascript">
// Load the Visualization API and the corechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Category');
data.addColumn('number', 'Total');
data.addRows([
<?php
$query = "SELECT SUM(sub_amount) AS sub_cat, subs_total FROM subscriptions WHERE sub_user_id ='".$user_id."' GROUP BY sub_cat ORDER BY sub_cat";
$exec = mysqli_query($connect,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".$row['sub_cat']."',".$row['subs_total']."],";
}
?>]);
// Set chart options
var options = {'title':'Spending by Category',
'width':600,
'height':400};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
It's clear that there's something wrong with the query code and how it's being grouped, but I'm scratching my head trying to figure this out.
Any pointers would be great!
UPDATE: Thanks to #WhiteHat for the SQL correction, here's the functioning code:
<script type="text/javascript">
// Load the Visualization API and the corechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Category');
data.addColumn('number', 'Total');
data.addRows([
<?php
$query = "SELECT sub_cat, SUM(sub_amount) as subs_total FROM subscriptions WHERE sub_user_id ='".$user_id."' GROUP BY sub_cat ORDER BY sub_cat";
$exec = mysqli_query($connect,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".$row['sub_cat']."',".$row['subs_total']."],";
}
?>]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':600,
'height':400};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
try this sql...
$query = "SELECT sub_cat, SUM(sub_amount) as subs_total FROM subscriptions WHERE sub_user_id ='".$user_id."' GROUP BY sub_cat ORDER BY sub_cat";
The category should be the first column and subtotal second.
Please can you test if your sql code return something?
I would help you if you give to me the correct dataset in your SQL.
Try to do an echo or vardump outside your javascript to know if your SQL is Good.

Create PDF of the datastored in Javascript of HTML file

I want to create pdf files of pie charts and graphs which are dynamically generated using javascript.
I am using pdfdom for creating the pdf files using php.
But the Problem is that pdfdom is unable to create pdf of the data stored in the javascript here is the sample of my HTML file..
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
Is there any other way to do it please help..its urgent..!!
Alternative:have you tried pchart http://pchart.sourceforge.net/ ? You can use jquery to generate the chart. You can access the script via AJAX(generating the image and making the PDF) and and the response of that script you can get the link of the PDF file.

hide javascript content for google visualization api

I am using google visualization api javascript for some images on my website. So when i am using the javascript and if the user clicks on view source, it is revealing all the information in the javascript source code which i do not want it to happen.
So i have tried to save the javascript into piechart.js file and call it like this
<script type="text/javascript" src="piechart.js"></script>
and then call the ID of the chart in the div like this on the same page. But the chart is not being displayed on the webpage.
Here is the code of index.php
<script type="text/javascript" src="piechart.js">
<div id="piechart"></div>
I am sure my code is correct for the chart to display because when i place the code instead of the piechart.js i can see the chart and along with code, the values are also being shown. Please help me here. How and where should i call the div?
piechart.js code (would appreciate if someone could align this javascript code correctly.)
<script type="text/javascript">
`
google.load('visualization', '1', {packages: ['corechart']});
<script type="text/javascript">
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Browser');
data.addColumn('number', 'Visits');
data.addRows(5);
data.setValue(0, 0, 'Internet Explorer (50.8%)');
data.setValue(0, 1, 50.81);
data.setValue(1, 0, 'Firefox (25.3%)');
data.setValue(1, 1, 25.30);
data.setValue(2, 0, 'Safari (16.09%)');
data.setValue(2, 1, 16.09);
data.setValue(3, 0, 'Chrome (7.53%)');
data.setValue(3, 1, 7.53);
data.setValue(4, 0, 'Opera (0.25%)');
data.setValue(4, 1, 0.25);
// Create and draw the visualization.
new google.visualization.PieChart(document.getElementById('piechart')).
draw(data, {title:"Visits by Browser",width:420, height:300, backgroundColor:'cccccc'});
}
google.setOnLoadCallback(drawVisualization);
</script> `
So when i am using the javascript and
if the user clicks on view source, it
is revealing all the information in
the javascript source code which i do
not want it to happen.
No need to do that just to hide your source code; and that is not possible because the source code will still be available/visible even if you put your javascript code in some other file.
You may want to obfuscate your code, have a look at:
Obfuscated Code
Or you may want to copyright your code with appropriate license.

Categories