Alex
Alex

Reputation: 45

Javascript: Passing variable to Google Charts API

I am working with Google Pie Charts API in Javascript. I am using Javascript within PHP. I am passing array elements to API url. But, somehow, I am not getting the required Pie Chart. I think I am messing with the syntax of passing variables to the API url.

This is the code I am using:

function pie(){
?>
<SCRIPT LANGUAGE='Javascript'><!--

piechart();
function piechart() {
var chtdata = new Array(50,50,100,25);       // Array containing values to be mapped

var doc1 =  "<img src='http://chart.apis.google.com/chart?cht=p3&chs=450x200&chd=t:'+chtdata[0]+',50,100,20&chl='r'|'s'|'g'|'h'&chtt='Visitor Details'&chco=ff0000' name='piechart' />";
document.write(doc1);
document.write('Done.');

}
</SCRIPT>
<?php

It would be great if you could point out my mistake. Any help will be appreciated.

Upvotes: 1

Views: 983

Answers (1)

Pointy
Pointy

Reputation: 413702

Because the outer quotes on your JavaScript string are double-quotes, the reference to the variable isn't actually a reference to the variable at all. The color-coding of the text right there in your question should show that clearly.

Try:

var doc1 =  "<img src='http://chart.apis.google.com/chart?cht=p3&chs=450x200&chd=t:"+val1+",50,100,20&chl='r'|'s'|'g'|'h'&chtt='Visitor Details'&chco=ff0000' name='piechart' />";

Upvotes: 2

Related Questions