“A picture is worth a thousand words.” To explain a set of data, the better way is create a chart. In PHP there a lot of chart designing tools/application. Today I spent some time with a charting tool provided by Google Code. I find this tool is really useful and easy to use. Like other Google codes it provides a lots of customization options. There are 2 kind of charting type supported by 2 different APIs. One produces Image chart and another produces Interactive Charts.
To explore with Google Chart API for Image chart I chose the data set of browser usages by visitors on my blog for the month of January 2011. Here is a snapshot from Awstats from my blog cPanel.
Here is the chart produced by the Google Chart API.
Let me explain the process I have used for drawing this chart. If you send a request URL with parameters in GET or POST then The Google Chart API returns a chart image as response. The request URL is https://chart.googleapis.com/chart. Here I have used GET method to send the parameters. Here is my URL for drawing this chart
Here are the list of parameters with brief description :-
This is the dimension for the chart. The syntax is chs=<width>x<height>. For both width & height max value is 1000 pixcels. However widthxheight should not exceed more than 300000.
The chart type. The syntax is cht=<chart_type>. I have used p3(for 3D pie chart). You can use values like p(pie chart), lc(line chart), v(venn chart), s(scatter chart), bhs/bvs(bar chart) etc. Check the chart gallery for complete listing of available chart types.
The data set for drawing the chart Basic syntax is chd=t:n1,n2,n3,n4…. n1, n2, n3 are floating point numbers with a value range of 0 – 100. Numbers below zero will be dropped and above 100 will be truncated to 100.
This is the chart legend text/label for each data/data series. The syntax is chdl=<data_1_label>|…|<data_n_label>. If you have multiple words as label then they must be used in encoded format(URL encoding).
The color specification for each elements/series. Syntax for this parameter is chco=element_1_color|element_2_color|…|element_n_color. The color code is a string of RRGGBB format hexadecimal number format. With this each 6 character color code you can use optional 2 character hexadecimal number from 00 to AA. Hence the color code format used here is RRGGBB[AA]. The last AA is used as transparency value , where 00 means complete transparent and FF means complete opaque.
This parameter is for chart title. Syntax is chtt=<chart_title>. To provide a line break you can use “|“. However the chart title also should be in encoded format.
This is used to mention the font size and the color of the chart title. Syntax is chts=<color>,<font_size>. Color is as RRGGBB format hexadecimal format and font size is in point value.
This parameter is used for background fill. Syntax is chf=<fill_type>,s,<color>. Here I have used bg as fill_type which means background fill. s means solid color. Color code is in RRGGBB hexadecimal format.
Beyond these parameters there are more which can be used to draw charts as per requirement. You can draw more complex charts using this API. You can read Google Chart Tools. To use this chart API you don’t require any API key from Google. Currently there is no limit to the number of requests per day you can make to the Google Chart API, but Google have reserved the right to block any use that they regard as abusive.
There is Image Chart Editor which you can use to draw your chart. So happy charting then.