Blame | Letzte Änderung | Log anzeigen | RSS feed
Title: Options TutorialThis document will help you understand how jqPlot's optionsrelate to the API documentation and the jqPlot objectitself. 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'soptions. The online documentation is API documentation. Whileit explains what attributes and methods various objects possess,it doesn't explain how to use or set those attributes throughoptions. This tutorial will help explain that.Let's assume you are creating a plotlike 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 viathe "optionsObj".The optionsObj really represents the plot object (jqPlot object, notto be confused with the $.jqplot function which will create a jqPlotobject). Attributes you specify on that object will be merged withattributes in the jqPlot object. The axes, legend, series, etc. areattributes on the jqPlot object. The jqPlot/optionsObj object lookssomething 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 onThe optionsObj should follow the same construction as if it were ajqPlot object (with some exceptions/shortcuts I'll mention in amoment). 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 becomesoptionsObj.grid.drawGridLines. Do likewise with the other objects inthe plot, replacing "this", with the respective attribute on the plotlike "legend" or "title". Series and Axes are handled a littledifferently, 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 sizeyou 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 youwould do:> optionObj = {> textColor:"#ff0000",> grid:{shadow:false, borderWidth:9.0}> }Here is a more deeply nested example. Say you want to specify a minand max on your y axis and use a specific color for your secondseries. 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 yousent in to your plot. To get to the second series, you have to put anobject (even if empty) in place of the first series.There is a handy shortcut to assign options to all axes or all seriesat one go. Use axesDefaults and seriesDefaults. So, if you wantedboth x and y axes to start at 0 and you wanted all series to not showmarkers, you could do:> optionsObj = {axesDefaults:{min:0}, seriesDefaults:{showMarker:false}}Another shortcut is for the plot title. Normally, you would assignoptions to the title as an object. If you specify a title option as astring, 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 andtheir options. Briefly, what's the difference between a renderer anda plugin.A renderer is an object that is used to draw something and getsattached to an existing object in the plot in order to draw it. Aplugin does more than just provide drawing functionality to anobject; it can calculate a trend line, change thecursor, provide event driven functionality, etc. I consider renderersplugins, 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 youwant to render your series as a bar chart with each set of barsshowing 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 plotwith a bar renderer and the x axis default renderer (but not any otheraxis) with a category renderer.Now, how would I assign options to those renderers? The renderer'sattributes may not be present in the pre-existing jqPlot object, theymay be specific to the renderer. This is done through the"rendererOptions" option on the appropriate object. So, if I wanted mybars 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 applyoptions to all series in the plot. You could do the same on anyparticular series in the plot through the "series" options array.Plugins are free to add their own options. For example, thehighlighter 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 thehighlighter tooltip to fade in and out slowly and be positioneddirectly above the point I'm highlighting:> optionsObj = {> highlighter:{tooltipFadeSpeed:'slow', tooltipLocation:'n'}> }Other plugins, like dragable and trendlines, add their options in withthe series. (Yes, that's the correct name for the dragable plugin; itdoesn't use the correct spelling of "draggable".)This is because both of those plugins can have differentoptions for different series in the plot. So, if you wanted to specify thecolor for the dragable plugin and constrain it to drag only on the x axis as wellas 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 seriesand 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 onthe series itself, not with a suboption of "dragable". This may be improvedin 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 withthe name of the object (in lowercase) you are looking at in youroptions 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 (likeplot.grid.shadow = false), but you'll have to issue the appropriatecalls to possibly reinitialize and redraw the plot. jqPlot candefinitely handle this to change the plot after creation (this is howthe dragable plugin updates the plot data and the trend line pluginrecomputes itself when data changes). This hasn't been documentedyet, however.