Showing posts with label Google Chart. Show all posts
Showing posts with label Google Chart. Show all posts

Tuesday, August 20, 2013

[Google Chart] Some examples of using Google Chart API in Python

Here are some example codes of using Google Chart APIs written in Python that I wrote several years ago. And some output image for reference.

  • To generate multi-line chart:
def createMultiLineChart(rows, max_value):
urls = "http://chart.apis.google.com/chart?" + \
"chs=" + str(300+24*rows.__len__()) + "x200" + \
"&chd=t:" + ",".join([str(i[1]) for i in rows]) + "|" + ",".join([str(i[2]) for i in rows]) + \
"&cht=lc" + \
"&chls=2,1,0|2,1,0" + \
"&chco=0000ff,ff0000" + \
"&chtt=SMT%20Scrap%20Rate" + \
"&chxt=x,y" + \
"&chg=20,20" + \
"&chds=0," + str(max_value * 1.4) + \
"&chxl=0:|" + "|".join([str(i[0]) for i in rows]) + "|1:|" + "|".join(
[str(i * max_value * 1.4 / 10.0) + '%' for i in range(0,11)]) + \
"&chdl=Day%20S/R(%)|Night%20S/R(%)" + \
"&chm=s,0000ff,0,-1,8|s,ff0000,1,-1,8|N*f3*%,0000ff,0,-1,10|N*f3*%,ff0000,1,-1,10"
return urls


  • To generate multi-line chart:

def createLineChart(rows, max_value):
urls = "http://chart.apis.google.com/chart?" + \
"chs=" + str(300+24*rows.__len__()) + "x200" + \
"&chd=t:" + ",".join([str(i[1]) for i in rows]) + \
"&cht=lc" + \
"&chls=2,1,0" + \
"&chco=8080ff" + \
"&chtt=SMT%20Scrap%20Rate" + \
"&chxt=x,y" + \
"&chg=20,20" + \
"&chds=0," + str(max_value * 1.4) + \
"&chxl=0:|" + "|".join([str(i[0]) for i in rows]) + "|1:|" + "|".join(
[str(i * max_value * 1.4 / 10.0) + '%' for i in range(0,11)]) +\
"&chdl=S/R(%)" + \
"&chm=s,80C65A,0,-1,10|N*f3*%,8080ff,0,-1,10"
return urls
P.S: It is almost the same as multi-line chart so that I don't provide the image.

  • To generate bar chart:
def createBarChart(rows, max_value):
urls = "http://chart.apis.google.com/chart?" + \
"chs=" + str(300+24*rows.__len__()) + "x200" + \
"&chd=t:" + ",".join([str(i[1]) for i in rows]) + \
"&cht=bvg" + \
"&chco=1d89f9,c6d9fd" + \
"&chtt=SMT%20Scrap%20Rate" + \
"&chxt=x,y" + \
"&chg=20,20" + \
"&chds=0," + str(max_value * 1.4) + \
"&chxl=0:|" + "|".join([str(i[0]) for i in rows]) + "|1:|" + "|".join(
[str(i * max_value * 1.4 / 10.0) + '%' for i in range(0,11)]) +\
"&chdl=S/R(%)" + \
"&chbh=14,9,15"
#"&chf=bg,s,ffffef"
return urls

  • To generate pie chart:
def createPieChart(rows, legend):
urls = "|".join([i[0] for i in rows])
urls = urls.replace(' ','_')
urls=urls.encode('utf8')
urls=urllib.quote(urls,'&=')
return "http://chart.apis.google.com/chart?" + \
"chs=600x150" + \
"&chd=t:" + ",".join([str(i[1]) for i in rows]) + \
"&cht=p3" + \
"&chtt=" + legend + \
"&chl=" + urls