README

Wed, 24 Jun 2009 20:39:55 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 24 Jun 2009 20:39:55 +0100
changeset 8
05052cfcefab
child 9
2712d960f70a
permissions
-rw-r--r--

Add README

8
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 gchart.lua - Create charts using the Google Charts API
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 Google Charts API works on a very simple principle. You encode all the data about the chart you want
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 to render into a URL, and when you request this URL you will receive a PNG image of your chart.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 The API is very fussy about the format of the data you pass it, so this library abstracts all that away,
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 and lets you create charts, manipulate them, and then build the URL when you are ready.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 ## Example
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 gchart = require "gchart"
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 gchart.default:set_color("C6D9FD");
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 mychart = gchart.new("bar")
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 mychart:add_series({ 100, 200, 100, 400 })
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 print("Chart:", chart:url());
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 ## Reference
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 ### Defaults
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 gchart.lua exposes a default chart object. All charts will 'inherit' the properties from this chart unless you override them. For example:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 gchart.default:set_color("C6D9FD") -- All charts will now be this colour by default
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 ### Core
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 gchart.set_base_url(url): Set the base URL of the API. Default: "http://chart.apis.google.com/chart"
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 For example if another provider supports an API compatible with Google Charts, you could use this to switch gchart.lua to using their service instead.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 gchart.new(type): Creates a new chart. Default type is "line".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 Returns the new chart object.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 ### Charts
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 All these functions assume that 'chart' is a chart object returned from gchart.new().
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 chart:set_type(type): Set the type of chart.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 May be one of "line", "sparkline", "plot", "bar" or a type code straight from the Google API, like "bhs".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 chart:add_series(series): Add a new data series
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 Takes an array of numbers, each represinting a data point. For example { 10, 20, 30 }.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 chart:set_scale(min, max): Set the minimum and maximum values for the chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 The Google Chart API does not automatically scale input data. By default gchart.lua will attempt
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 to scale the graph so that the base line is 25% lower than the lowest value on the graph, and the
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 top axis 25% higher than the highest value on the graph. Supply custom numbers for either or both of
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 min and max, or set to true for gchart.lua to auto-scale that value (default is to auto-scale both min
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 and max).
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 chart:set_size(width, height): Set the width and height of the generated chart in pixels
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 Google Chart API limits the maximum size of generated charts, the default size is 320x200.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 chart:set_title(title): Set the title of the chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 The title will be displayed at the top of the chart. Both the '|' (pipe) and \n (newline) characters
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 will start a new line (use for sub-titles).
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 chart:set_legend(entries): Add a legend to a chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 Set entries to a list of legend entries, in the same order as your data series were added. For example
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 { "Temperature", "Rainfall", "Humidity" }.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 chart:set_legend_position(position, layout): Set the position of the legend
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 position may be one of "top", "bottom", "left" or "right". Default is "right".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 layout may be one of "vertical" or "horizontal". Default is "vertical". When using
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 the "horizontal" layout, the only valid positions are "top" and "bottom".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 chart:add_axis(location, options): Add an axis to the chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 location may be one of "top", "bottom", "left" or "right".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 options is an (optional) table allowing you to configure how the axis is displayed. For more info see
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 the 'Axes' section below.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 chart:add_marker(marker): Add a marker to one or more datapoints
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 'marker' is a table of options. For more info see the 'Markers' section below.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 chart:set_color(color): Set the colour of the graph lines
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 Colours are specified as hexidecimal values of red, green and blue, just as in HTML. An optional addition
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 is appending 2 further digits to specify the opacity. For example 50% transparent blue would be: 0000FFCC.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 It is possible to specify multiple colours, separated by commas, which will be used differently according
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 to the chart type. For example "0000FFCC,FF0000CC".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 chart:url(): Return the URL to render the chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 Last but not least, call this function to retrieve the URL to generate the graph with all properties applied.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 ## Axes
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 Axes are extremely flexible in Google Charts API. Add an axis with chart:add_axis(location, options) as described above. The 'options'
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 table may contain any of the following options:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 options.range: Specify the range of the axis markers
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 For example: options.range = { min = 0, max = 500 }
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 options.labels: Specify text labels along the axis instead of numbers
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 For example: options.labels = { "Jan", "Feb", "Mar", "Apr" }
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 or with positions: options.labels = { Jan = 10, Feb = 20, Mar = 30, May = 50 }
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 options.style: Specify the style of an axis
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 Currently a string of the form: <axis color>,<font size>,<alignment>,<drawing control>,<tick mark color>
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 Only the colour is required, and further parameters are optional.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 For example, a green axis: options.style = "00CC00"
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 and with 12pt text: options.style = "00CC00,12"
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 options.ticklength: Specify the length of the tickmarks on the axis
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 Must be a number. From the Google documentation:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 "Positive values are drawn outside the chart area. The maximum positive value is 25."
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 "Negative values are drawn inside the chart area. Use this feature to draw gridlines on a chart."
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 ## Markers
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 Markers can be used to draw attention to specific points on the chart. They can be added using chart:add_marker(marker),
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 where 'marker' is a table of marker options, as described below:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 marker.label: Text label for the marker. Default is to use marker.index.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 marker.color: Colour for the marker. A simple hexadecimal value like 00FF00.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 marker.series: Which data series the marker should be on. Use default is 0 (the first series)
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 marker.index: The index of the data point to add the marker at. Default is 0 (the start of the series)
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 marker.size: Text size of the marker's label. Default is 11 (for 11pt text)
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 marker.priority: When to draw the marker
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 From the Google documentation:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 -1 specifies that the label is drawn before all other parts of the chart. The label will be
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 hidden if another chart element is drawn in the same place.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 0 specifies that the label is drawn after bars or lines, but before other labels. This is the default.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 1 specifies that the label is drawn after all other parts of the chart. If more than one label has this
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 value, the first one will be drawn first, the second one drawn second, and so on.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133

mercurial