Java Reference
In-Depth Information
ing_charts.html
)
,whichisalsoknownastheChartAPI,letsyoudynamicallycre-
ate and return images of bar, pie, and other kinds of charts.
GoogleChartsisaccessedviathe
https://chart.googleapis.com/chart
URI. You append a query string to this URI that identifies the chart type, size, data,
labels, and any other needed information. For example, query string
“
?cht=p3&chs=450x200&chd=t:60,40&chl=Q1%20(60%)|Q2%20(40%)
”
describes the following chart type, size, data, and label parameters:
•
cht=p3
specifies the chart type as a three-dimensional pie chart.
•
chs=450x200
specifiesthechartsizeas450pixelswideby200pixelshigh
—achartshouldbeatleasttwo-and-one-half timesaswideasitistallsothat
all labels are fully visible.
•
chd=t:60,40
specifies the chart data in a simple text format —this format
consists of a single series of comma-separated values; multiple series are spe-
cified byusingavertical bartoseparate oneseries fromthenext—wherethe
first data item (for the first pie chart slice) is 60 and the second data item (for
the second slice) is 40.
•
chl=Q1%20(60%)|Q2%20(40%)
specifiesthechartlabelsforthepiechart
slices as Q1 (60%) and Q2 (40%) —labels are separated by vertical bars and
must be URL-encoded (which is why each space character is replaced with
%20
).
Google Charts defaults to returning the chart as a PNG image. You can return a
GIF image instead by including the
chof=gif
parameter in the query string, or
even return a JavaScript Object Notation (JSON)-formatted document (see
ht-
er.
I've created a
ViewChart
application that passes the aforementioned URI with
querystringtoGoogleCharts,obtainsthegeneratedPNGimageofthe3Dpiechart,and
displays this image.
Listing 11-15
presents this application's source code.
Listing 11-15.
A client for accessing the Google Charts web service
import java.io.InputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;