README

Tue, 01 Dec 2009 13:55:43 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 01 Dec 2009 13:55:43 +0000
changeset 12
96b91845c3a9
parent 9
2712d960f70a
permissions
-rw-r--r--

Add example URL in 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());
12
96b91845c3a9 Add example URL in README
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
18 --> Chart: http://chart.apis.google.com/chart?cht=bvs&chco=00CC00,CC0000&&chd=e:DwS0Dww7&chs=320x200
8
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.
9
2712d960f70a Minor fixes for the README
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
35
8
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 ### Charts
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 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
39
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 chart:set_type(type): Set the type of chart.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 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
42
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 chart:add_series(series): Add a new data series
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 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
45
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 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
47 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
48 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
49 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
50 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
51 and max).
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 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
54 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
55
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 chart:set_title(title): Set the title of the chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 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
58 will start a new line (use for sub-titles).
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 chart:set_legend(entries): Add a legend to a chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 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
62 { "Temperature", "Rainfall", "Humidity" }.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 chart:set_legend_position(position, layout): Set the position of the legend
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 position may be one of "top", "bottom", "left" or "right". Default is "right".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 layout may be one of "vertical" or "horizontal". Default is "vertical". When using
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 the "horizontal" layout, the only valid positions are "top" and "bottom".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 chart:add_axis(location, options): Add an axis to the chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 location may be one of "top", "bottom", "left" or "right".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 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
72 the 'Axes' section below.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 chart:add_marker(marker): Add a marker to one or more datapoints
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 '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
76
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 chart:set_color(color): Set the colour of the graph lines
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 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
79 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
80 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
81 to the chart type. For example "0000FFCC,FF0000CC".
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 chart:url(): Return the URL to render the chart
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 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
85
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 ## Axes
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 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
89 table may contain any of the following options:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 options.range: Specify the range of the axis markers
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 For example: options.range = { min = 0, max = 500 }
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 options.labels: Specify text labels along the axis instead of numbers
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 For example: options.labels = { "Jan", "Feb", "Mar", "Apr" }
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 or with positions: options.labels = { Jan = 10, Feb = 20, Mar = 30, May = 50 }
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 options.style: Specify the style of an axis
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 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
100 Only the colour is required, and further parameters are optional.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 For example, a green axis: options.style = "00CC00"
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 and with 12pt text: options.style = "00CC00,12"
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 options.ticklength: Specify the length of the tickmarks on the axis
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 Must be a number. From the Google documentation:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 "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
107 "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
108
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 ## Markers
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 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
113 where 'marker' is a table of marker options, as described below:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 marker.label: Text label for the marker. Default is to use marker.index.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 marker.color: Colour for the marker. A simple hexadecimal value like 00FF00.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118
9
2712d960f70a Minor fixes for the README
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
119 marker.series: Which data series the marker should be on. Default is 0 (the first series)
8
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 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
122
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 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
124
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 marker.priority: When to draw the marker
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 From the Google documentation:
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 -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
128 hidden if another chart element is drawn in the same place.
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 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
131
05052cfcefab Add README
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 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
133 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
134

mercurial