You are here

optionsTutorial.txt in Visitors 8.2

Title: Options Tutorial

This document will help you understand how jqPlot's options
relate to the API documentation and the jqPlot object
itself.  For a listing of options available to jqPlot,
see <jqPlot Options> in the jqPlotOptions.txt file.

The key to effectively using jqPlot is understanding jqPlot's
options.  The online documentation is API documentation.  While
it explains what attributes and methods various objects possess,
it doesn't explain how to use or set those attributes through 
options.  This tutorial will help explain that.

Let's assume you are creating a plot 
like this:

> chart = $.jqplot('chart', dataSeries, optionsObj);

First, note that you shouldn't try to directly set attributes on the
"chart" object (like chart.grid.shadow) after your call to $.jqplot().  
At best this won't do anything **(see below). You should pass options in via
the "optionsObj".

The optionsObj really represents the plot object (jqPlot object, not
to be confused with the $.jqplot function which will create a jqPlot
object).  Attributes you specify on that object will be merged with
attributes in the jqPlot object.  The axes, legend, series, etc. are
attributes on the jqPlot object.  The jqPlot/optionsObj object looks
something like (only some attributes shown):

> jqPlot-|
>        |-seriesColors
>        |-textColor
>        |-fontFamily
>        |-fontSize
>        |-stackSeries
>        |-series(Array)-|
>        |               |-Series1-|
>        |               |         |-lineWidth
>        |               |         |-linePattern
>        |               |         |-shadow
>        |               |         |-showLine
>        |               |         |-showMarker
>        |               |         |-color
>        |               |-Series2...
>        |               |-...
>        |               |-SeriesN
>        |
>        |-grid(Object)-|
>        |              |-drawGridLines
>        |              |-background
>        |              |-borderColor
>        |              |-borderWidth
>        |              |-shadow
>        |
>        |-title(Object)-|
>        |               |-text
>        |               |-show
>        |               |-fontFamily
>        |               |-fontSize
>        |               |-textAlign
>        |               |-textColor
>        |
>        |-axes(Object)-|
>        |              |-xais-|
>        |              |      |-min
>        |              |      |-max
>        |              |      |-numberTicks
>        |              |      |-showTicks
>        |              |      |-showTickMarks
>        |              |      |-pad
>        |
>        | ... and so on
 
The optionsObj should follow the same construction as if it were a
jqPlot object (with some exceptions/shortcuts I'll mention in a
moment).  So generally, when you see something like
"this.drawGridLines" in the grid properties in the docs, just replace
"this" with "grid" in your options object.  So it becomes
optionsObj.grid.drawGridLines.  Do likewise with the other objects in
the plot, replacing "this", with the respective attribute on the plot
like "legend" or "title".  Series and Axes are handled a little
differently, because series is an array and axes has 4 distinct children
"xaxis", "yaxis", "x2axis" and "y2axis".

So, to remove the shadow from the grid and change the grid border size
you would do:

> optionObj = {grid:{shadow:false, borderWidth:9.0}};

To do the same as above but also make all the text in the plot red you
would do:

> optionObj = {
>    textColor:"#ff0000",
>    grid:{shadow:false, borderWidth:9.0}
> }

Here is a more deeply nested example. Say you want to specify a min
and max on your y axis and use a specific color for your second
series.  That would look like:

> optionsObj = {
>    axes:{yaxis:{min:5, max:230}},
>    series:[{},{color:"#33ff66"}]
> }

Note that series options are an array in order of the series data you
sent in to your plot.  To get to the second series, you have to put an
object (even if empty) in place of the first series.

There is a handy shortcut to assign options to all axes or all series
at one go.  Use axesDefaults and seriesDefaults.  So, if you wanted
both x and y axes to start at 0 and you wanted all series to not show
markers, you could do:

> optionsObj = {axesDefaults:{min:0}, seriesDefaults:{showMarker:false}}

Another shortcut is for the plot title.  Normally, you would assign
options to the title as an object.  If you specify a title option as a
string, it will assign that to the title.text property automatically.
So these two are equivalent:

> optionsObj = {title:{text:"My Plot"}}

and

> optionsObj = {title:"My Plot"}

Where things need more explanation is with renderers, plugins and
their options.  Briefly, what's the difference between a renderer and
a plugin.

A renderer is an object that is used to draw something and gets
attached to an existing object in the plot in order to draw it.  A
plugin does more than just provide drawing functionality to an
object; it can calculate a trend line, change the
cursor, provide event driven functionality, etc.  I consider renderers
plugins, but plugins don't have to be renderers.

So, how do you use renderers and plugins, and specify their options?
Some common renderers are for bar charts and category axes.  If you
want to render your series as a bar chart with each set of bars
showing up in a category on the x axis, you do:

> optionsObj = {
>    seriesDefaults:{renderer:$.jqplot.BarRenderer},
>    axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer}}
> }

This replaces the default renderer used for all series in the plot
with a bar renderer and the x axis default renderer (but not any other
axis) with a category renderer.

Now, how would I assign options to those renderers?  The renderer's
attributes may not be present in the pre-existing jqPlot object, they
may be specific to the renderer.  This is done through the
"rendererOptions" option on the appropriate object. So, if I wanted my
bars to be 25 pixels wide, I would do:


> optionsObj = {
>    seriesDefaults:{
>        renderer:$.jqplot.BarRenderer},
>        rendererOptions:{
>            barWidth:25
>        },
>    axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer}}
> }

Again, this is using the "seriesDefaults" option, which will apply
options to all series in the plot.  You could do the same on any
particular series in the plot through the "series" options array.

Plugins are free to add their own options.  For example, the
highlighter plugin has its own set of options that are unique to it.
As a result, it responds to options placed in the "highlighter"
attribute of your options object.  So, if I wanted to change the
highlighter tooltip to fade in and out slowly and be positioned
directly above the point I'm highlighting:

> optionsObj = {
>     highlighter:{tooltipFadeSpeed:'slow', tooltipLocation:'n'}
> }

Other plugins, like dragable and trendlines, add their options in with
the series.   (Yes, that's the correct name for the dragable plugin; it
doesn't use the correct spelling of "draggable".)
This is because both of those plugins can have different
options for different series in the plot.  So, if you wanted to specify the
color for the dragable plugin and constrain it to drag only on the x axis as well
as specify the color of the trend line you could do:

> series:[{
>     dragable: {
>         color: '#ff3366',
>         constrainTo: 'x'
>     },
>     trendline: {
>         color: '#cccccc'
>     }
> }]

This would apply those options to the first series only.  If you had 2 series
and wanted to turn off dragging and trend lines on the second series, you could do:

> series:[{
>     dragable: {
>         color: '#ff3366',
>         constrainTo: 'x'
>     },
>     trendline: {
>         color: '#cccccc'
>     }
> }, {
>    isDragable: false,
>    trendline:{
>        show: false
>    }
> }]

Note, series draggability is turned off with the "isDragable" option directly on 
the series itself, not with a suboption of "dragable".  This may be improved 
in the future.

I hope this is helpful. 
A few key points to remember:

- When you see "this" in the api docs, you generally replace it with
the name of the object (in lowercase) you are looking at in your
options object.
- seriesDefaults and axesDefaults are convenient shortcuts.
- to assign options to a renderer, generally use the "rendererOptions"
- plugins may add their own options attribute, like "highlighter" or
"cursor".

** Note:  you can set attributes after the plot is created (like
plot.grid.shadow = false), but you'll have to issue the appropriate
calls to possibly reinitialize and redraw the plot.  jqPlot can
definitely handle this to change the plot after creation (this is how
the dragable plugin updates the plot data and the trend line plugin
recomputes itself when data changes).  This hasn't been documented
yet, however.

File

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