hide javascript content for google visualization api - php

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.

Related

Google Pie Chart php mysql

**
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>

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.

Google scatter chart for two arrays

Hello People here is my Google scattered chart code what I am using:
require '/lib/GoogleChart.php';
require '/lib/markers/GoogleChartShapeMarker.php';
require '/lib/markers/GoogleChartTextMarker.php';
$Variance=array();
$Emp_RecFactor=array();
$Emp_Id=array();
//$Emp_FirstName=array();
$EquityGraph=new EquityGraph();
$EquityGraph->Graph();
$DrawGraph=$EquityGraph->DrawGraph;
foreach($DrawGraph as $key=>$value)
{
$Variance[]=$value["Variance"];//for multiple values ,array
$Emp_RecFactor[]=$value["Emp_RecFactor"];//single value
$Emp_Id[]=$value["Emp_Id"];//single value
}
$_GET['Variance']=$Variance;
$_GET['Emp_RecFactor']=$Emp_RecFactor;
print_r($Emp_RecFactor);
$chart = new GoogleChart('lc', 500, 200);
// manually forcing the scale to [0,100]
$chart->setScale(0,100);
// add one line
$data = new GoogleChartData($Variance);
$chart->addData($data);
// customize y axis
$y_axis = new GoogleChartAxis('y');
$y_axis->setDrawTickMarks(false)->setLabels(array(-5,0,5));
$chart->addAxis($y_axis);
// customize x axis
$x_axis = new GoogleChartAxis('x');
$x_axis->setTickMarks(5);
$chart->addAxis($x_axis);
// add a shape marker with a border
$shape_marker = new GoogleChartShapeMarker(GoogleChartShapeMarker::CIRCLE);
$shape_marker->setSize(6);
$shape_marker->setBorder(2);
$shape_marker->setData($data);
$chart->addMarker($shape_marker);
// add a value marker
$value_marker = new GoogleChartTextMarker(GoogleChartTextMarker::VALUE);
$value_marker->setData($data);
$chart->addMarker($value_marker);
//~ header('Content-Type: image/png');
echo $chart->toHtml();
As you can see in the code I have used $Variance array passing to $data now I need to use one more array $Emp_RecFactor and I need to draw a graph between those two...
I also want to add mouse over feature to this so that if someone hovers over the selected point it should display different things for different selected points -how do I do that?
To draw a Google scattered chart between two arrays you have to use code as below
var data = google.visualization.arrayToDataTable([
['Age', 'Array1', 'Array2'],
[8, 12, 15],
[4, 5.5, 0],
[11, 0, 14],
[4, 9, 5],
[3, 3.5, 9],
[6.5, 7, 13]
]);
And it has a default tooltip to show the data while hovering the selected point. We can also customize Tooltip content by using html tags.
To view a working sample for this go to the site jqfaq.com(Sample) and a customization of Tooltip content go to the site jqfaq.com(customizing Tooltip)

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.

Categories