| 9 |
lars |
1 |
Title: Options Tutorial
|
|
|
2 |
|
|
|
3 |
This document will help you understand how jqPlot's options
|
|
|
4 |
relate to the API documentation and the jqPlot object
|
|
|
5 |
itself. For a listing of options available to jqPlot,
|
|
|
6 |
see <jqPlot Options> in the jqPlotOptions.txt file.
|
|
|
7 |
|
|
|
8 |
The key to effectively using jqPlot is understanding jqPlot's
|
|
|
9 |
options. The online documentation is API documentation. While
|
|
|
10 |
it explains what attributes and methods various objects possess,
|
|
|
11 |
it doesn't explain how to use or set those attributes through
|
|
|
12 |
options. This tutorial will help explain that.
|
|
|
13 |
|
|
|
14 |
Let's assume you are creating a plot
|
|
|
15 |
like this:
|
|
|
16 |
|
|
|
17 |
> chart = $.jqplot('chart', dataSeries, optionsObj);
|
|
|
18 |
|
|
|
19 |
First, note that you shouldn't try to directly set attributes on the
|
|
|
20 |
"chart" object (like chart.grid.shadow) after your call to $.jqplot().
|
|
|
21 |
At best this won't do anything **(see below). You should pass options in via
|
|
|
22 |
the "optionsObj".
|
|
|
23 |
|
|
|
24 |
The optionsObj really represents the plot object (jqPlot object, not
|
|
|
25 |
to be confused with the $.jqplot function which will create a jqPlot
|
|
|
26 |
object). Attributes you specify on that object will be merged with
|
|
|
27 |
attributes in the jqPlot object. The axes, legend, series, etc. are
|
|
|
28 |
attributes on the jqPlot object. The jqPlot/optionsObj object looks
|
|
|
29 |
something like (only some attributes shown):
|
|
|
30 |
|
|
|
31 |
> jqPlot-|
|
|
|
32 |
> |-seriesColors
|
|
|
33 |
> |-textColor
|
|
|
34 |
> |-fontFamily
|
|
|
35 |
> |-fontSize
|
|
|
36 |
> |-stackSeries
|
|
|
37 |
> |-series(Array)-|
|
|
|
38 |
> | |-Series1-|
|
|
|
39 |
> | | |-lineWidth
|
|
|
40 |
> | | |-linePattern
|
|
|
41 |
> | | |-shadow
|
|
|
42 |
> | | |-showLine
|
|
|
43 |
> | | |-showMarker
|
|
|
44 |
> | | |-color
|
|
|
45 |
> | |-Series2...
|
|
|
46 |
> | |-...
|
|
|
47 |
> | |-SeriesN
|
|
|
48 |
> |
|
|
|
49 |
> |-grid(Object)-|
|
|
|
50 |
> | |-drawGridLines
|
|
|
51 |
> | |-background
|
|
|
52 |
> | |-borderColor
|
|
|
53 |
> | |-borderWidth
|
|
|
54 |
> | |-shadow
|
|
|
55 |
> |
|
|
|
56 |
> |-title(Object)-|
|
|
|
57 |
> | |-text
|
|
|
58 |
> | |-show
|
|
|
59 |
> | |-fontFamily
|
|
|
60 |
> | |-fontSize
|
|
|
61 |
> | |-textAlign
|
|
|
62 |
> | |-textColor
|
|
|
63 |
> |
|
|
|
64 |
> |-axes(Object)-|
|
|
|
65 |
> | |-xais-|
|
|
|
66 |
> | | |-min
|
|
|
67 |
> | | |-max
|
|
|
68 |
> | | |-numberTicks
|
|
|
69 |
> | | |-showTicks
|
|
|
70 |
> | | |-showTickMarks
|
|
|
71 |
> | | |-pad
|
|
|
72 |
> |
|
|
|
73 |
> | ... and so on
|
|
|
74 |
|
|
|
75 |
The optionsObj should follow the same construction as if it were a
|
|
|
76 |
jqPlot object (with some exceptions/shortcuts I'll mention in a
|
|
|
77 |
moment). So generally, when you see something like
|
|
|
78 |
"this.drawGridLines" in the grid properties in the docs, just replace
|
|
|
79 |
"this" with "grid" in your options object. So it becomes
|
|
|
80 |
optionsObj.grid.drawGridLines. Do likewise with the other objects in
|
|
|
81 |
the plot, replacing "this", with the respective attribute on the plot
|
|
|
82 |
like "legend" or "title". Series and Axes are handled a little
|
|
|
83 |
differently, because series is an array and axes has 4 distinct children
|
|
|
84 |
"xaxis", "yaxis", "x2axis" and "y2axis".
|
|
|
85 |
|
|
|
86 |
So, to remove the shadow from the grid and change the grid border size
|
|
|
87 |
you would do:
|
|
|
88 |
|
|
|
89 |
> optionObj = {grid:{shadow:false, borderWidth:9.0}};
|
|
|
90 |
|
|
|
91 |
To do the same as above but also make all the text in the plot red you
|
|
|
92 |
would do:
|
|
|
93 |
|
|
|
94 |
> optionObj = {
|
|
|
95 |
> textColor:"#ff0000",
|
|
|
96 |
> grid:{shadow:false, borderWidth:9.0}
|
|
|
97 |
> }
|
|
|
98 |
|
|
|
99 |
Here is a more deeply nested example. Say you want to specify a min
|
|
|
100 |
and max on your y axis and use a specific color for your second
|
|
|
101 |
series. That would look like:
|
|
|
102 |
|
|
|
103 |
> optionsObj = {
|
|
|
104 |
> axes:{yaxis:{min:5, max:230}},
|
|
|
105 |
> series:[{},{color:"#33ff66"}]
|
|
|
106 |
> }
|
|
|
107 |
|
|
|
108 |
Note that series options are an array in order of the series data you
|
|
|
109 |
sent in to your plot. To get to the second series, you have to put an
|
|
|
110 |
object (even if empty) in place of the first series.
|
|
|
111 |
|
|
|
112 |
There is a handy shortcut to assign options to all axes or all series
|
|
|
113 |
at one go. Use axesDefaults and seriesDefaults. So, if you wanted
|
|
|
114 |
both x and y axes to start at 0 and you wanted all series to not show
|
|
|
115 |
markers, you could do:
|
|
|
116 |
|
|
|
117 |
> optionsObj = {axesDefaults:{min:0}, seriesDefaults:{showMarker:false}}
|
|
|
118 |
|
|
|
119 |
Another shortcut is for the plot title. Normally, you would assign
|
|
|
120 |
options to the title as an object. If you specify a title option as a
|
|
|
121 |
string, it will assign that to the title.text property automatically.
|
|
|
122 |
So these two are equivalent:
|
|
|
123 |
|
|
|
124 |
> optionsObj = {title:{text:"My Plot"}}
|
|
|
125 |
|
|
|
126 |
and
|
|
|
127 |
|
|
|
128 |
> optionsObj = {title:"My Plot"}
|
|
|
129 |
|
|
|
130 |
Where things need more explanation is with renderers, plugins and
|
|
|
131 |
their options. Briefly, what's the difference between a renderer and
|
|
|
132 |
a plugin.
|
|
|
133 |
|
|
|
134 |
A renderer is an object that is used to draw something and gets
|
|
|
135 |
attached to an existing object in the plot in order to draw it. A
|
|
|
136 |
plugin does more than just provide drawing functionality to an
|
|
|
137 |
object; it can calculate a trend line, change the
|
|
|
138 |
cursor, provide event driven functionality, etc. I consider renderers
|
|
|
139 |
plugins, but plugins don't have to be renderers.
|
|
|
140 |
|
|
|
141 |
So, how do you use renderers and plugins, and specify their options?
|
|
|
142 |
Some common renderers are for bar charts and category axes. If you
|
|
|
143 |
want to render your series as a bar chart with each set of bars
|
|
|
144 |
showing up in a category on the x axis, you do:
|
|
|
145 |
|
|
|
146 |
> optionsObj = {
|
|
|
147 |
> seriesDefaults:{renderer:$.jqplot.BarRenderer},
|
|
|
148 |
> axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer}}
|
|
|
149 |
> }
|
|
|
150 |
|
|
|
151 |
This replaces the default renderer used for all series in the plot
|
|
|
152 |
with a bar renderer and the x axis default renderer (but not any other
|
|
|
153 |
axis) with a category renderer.
|
|
|
154 |
|
|
|
155 |
Now, how would I assign options to those renderers? The renderer's
|
|
|
156 |
attributes may not be present in the pre-existing jqPlot object, they
|
|
|
157 |
may be specific to the renderer. This is done through the
|
|
|
158 |
"rendererOptions" option on the appropriate object. So, if I wanted my
|
|
|
159 |
bars to be 25 pixels wide, I would do:
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
> optionsObj = {
|
|
|
163 |
> seriesDefaults:{
|
|
|
164 |
> renderer:$.jqplot.BarRenderer},
|
|
|
165 |
> rendererOptions:{
|
|
|
166 |
> barWidth:25
|
|
|
167 |
> },
|
|
|
168 |
> axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer}}
|
|
|
169 |
> }
|
|
|
170 |
|
|
|
171 |
Again, this is using the "seriesDefaults" option, which will apply
|
|
|
172 |
options to all series in the plot. You could do the same on any
|
|
|
173 |
particular series in the plot through the "series" options array.
|
|
|
174 |
|
|
|
175 |
Plugins are free to add their own options. For example, the
|
|
|
176 |
highlighter plugin has its own set of options that are unique to it.
|
|
|
177 |
As a result, it responds to options placed in the "highlighter"
|
|
|
178 |
attribute of your options object. So, if I wanted to change the
|
|
|
179 |
highlighter tooltip to fade in and out slowly and be positioned
|
|
|
180 |
directly above the point I'm highlighting:
|
|
|
181 |
|
|
|
182 |
> optionsObj = {
|
|
|
183 |
> highlighter:{tooltipFadeSpeed:'slow', tooltipLocation:'n'}
|
|
|
184 |
> }
|
|
|
185 |
|
|
|
186 |
Other plugins, like dragable and trendlines, add their options in with
|
|
|
187 |
the series. (Yes, that's the correct name for the dragable plugin; it
|
|
|
188 |
doesn't use the correct spelling of "draggable".)
|
|
|
189 |
This is because both of those plugins can have different
|
|
|
190 |
options for different series in the plot. So, if you wanted to specify the
|
|
|
191 |
color for the dragable plugin and constrain it to drag only on the x axis as well
|
|
|
192 |
as specify the color of the trend line you could do:
|
|
|
193 |
|
|
|
194 |
> series:[{
|
|
|
195 |
> dragable: {
|
|
|
196 |
> color: '#ff3366',
|
|
|
197 |
> constrainTo: 'x'
|
|
|
198 |
> },
|
|
|
199 |
> trendline: {
|
|
|
200 |
> color: '#cccccc'
|
|
|
201 |
> }
|
|
|
202 |
> }]
|
|
|
203 |
|
|
|
204 |
This would apply those options to the first series only. If you had 2 series
|
|
|
205 |
and wanted to turn off dragging and trend lines on the second series, you could do:
|
|
|
206 |
|
|
|
207 |
> series:[{
|
|
|
208 |
> dragable: {
|
|
|
209 |
> color: '#ff3366',
|
|
|
210 |
> constrainTo: 'x'
|
|
|
211 |
> },
|
|
|
212 |
> trendline: {
|
|
|
213 |
> color: '#cccccc'
|
|
|
214 |
> }
|
|
|
215 |
> }, {
|
|
|
216 |
> isDragable: false,
|
|
|
217 |
> trendline:{
|
|
|
218 |
> show: false
|
|
|
219 |
> }
|
|
|
220 |
> }]
|
|
|
221 |
|
|
|
222 |
Note, series draggability is turned off with the "isDragable" option directly on
|
|
|
223 |
the series itself, not with a suboption of "dragable". This may be improved
|
|
|
224 |
in the future.
|
|
|
225 |
|
|
|
226 |
I hope this is helpful.
|
|
|
227 |
A few key points to remember:
|
|
|
228 |
|
|
|
229 |
- When you see "this" in the api docs, you generally replace it with
|
|
|
230 |
the name of the object (in lowercase) you are looking at in your
|
|
|
231 |
options object.
|
|
|
232 |
- seriesDefaults and axesDefaults are convenient shortcuts.
|
|
|
233 |
- to assign options to a renderer, generally use the "rendererOptions"
|
|
|
234 |
- plugins may add their own options attribute, like "highlighter" or
|
|
|
235 |
"cursor".
|
|
|
236 |
|
|
|
237 |
** Note: you can set attributes after the plot is created (like
|
|
|
238 |
plot.grid.shadow = false), but you'll have to issue the appropriate
|
|
|
239 |
calls to possibly reinitialize and redraw the plot. jqPlot can
|
|
|
240 |
definitely handle this to change the plot after creation (this is how
|
|
|
241 |
the dragable plugin updates the plot data and the trend line plugin
|
|
|
242 |
recomputes itself when data changes). This hasn't been documented
|
|
|
243 |
yet, however.
|