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