Skip to content

Instantly share code, notes, and snippets.

@maxim75
Created May 17, 2011 03:36
Show Gist options
  • Save maxim75/975893 to your computer and use it in GitHub Desktop.
Save maxim75/975893 to your computer and use it in GitHub Desktop.
Google Chart API - Bar chart
<html>
<head>
<title> Vertical bar chart </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<img id="chart" width="380" height="220" alt="H" />
<script>
function get_url_params_string(params)
{
var result = [];
for(var name in params)
result.push(encodeURI(name) + "=" + encodeURI(params[name]));
return result.join("&");
}
var data = [
{ "title": "A", "value": 1 },
{ "title": "B", "value": 2 },
{ "title": "C", "value": 3 }
]
var titles = [];
var values = [];
var max_value = 0;
for(var index in data)
{
var item = data[index];
titles.push(item["title"]);
values.push(item["value"]);
if(item["value"] > max_value)
max_value = item["value"];
}
var chart_url = "http://chart.apis.google.com/chart?" + get_url_params_string({
"chxl": "1:|" + titles.join("|"),
"chxr": "0,0," + max_value,
"chxt": "x,y",
"chbh": "a",
"chs": "380x220",
"cht": "bhs",
"chco": "4D89F9",
"chds": "0,"+ max_value,
"chd": "t:" + values.join(","),
"chtt": "Тестирование",
"": "",
"": "",
});
$("#chart").attr("src", chart_url)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment