diff --git a/API.txt b/API.txt index e8cfb99..6f72aec 100644 --- a/API.txt +++ b/API.txt @@ -57,6 +57,8 @@ The format of a single series object is as follows: lines: specific lines options, bars: specific bars options, points: specific points options, + xaxis: 1 or 2, + yaxis: 1 or 2, shadowSize: number } @@ -81,6 +83,11 @@ The latter is mostly useful if you let the user add and remove series, in which case you can hard-code the color index to prevent the colors from jumping around between the series. +The "xaxis" and "yaxis" options specify which axis to use, specify 2 +to get the secondary axis (x axis at top or y axis to the right). +E.g., you can use this to make a dual axis plot by specifying +{ yaxis: 2 } for one data series. + The rest of the options are all documented below as they are the same as the default options passed in via the options parameter in the plot commmand. When you specify them for a specific data series, they will @@ -148,7 +155,7 @@ that it will overwrite the contents of the container. Customizing the axes ==================== - xaxis, yaxis: { + xaxis, yaxis, x2axis, y2axis: { mode: null or "time" min: null or number max: null or number @@ -163,7 +170,7 @@ Customizing the axes tickDecimals: null or number } -The two axes have the same kind of options. The "mode" option +The axes have the same kind of options. The "mode" option determines how the data is interpreted, the default of null means as decimal numbers. Use "time" for time series data, see the next section. @@ -447,11 +454,11 @@ Customizing the grid clickable: boolean } -The grid is the thing with the two axes and a number of ticks. "color" +The grid is the thing with the axes and a number of ticks. "color" is the color of the grid itself whereas "backgroundColor" specifies the background color inside the grid area. The default value of null means that the background is transparent. You should only need to set -backgroundColor if want the grid area to be a different color from the +backgroundColor if you want the grid area to be a different color from the page color. Otherwise you might as well just set the background color of the page with CSS. @@ -488,18 +495,42 @@ An example function might look like this: If you set "clickable" to true, the plot will listen for click events on the plot area and fire a "plotclick" event on the placeholder with -an object { x: number, y: number } as parameter when one occurs. The -returned coordinates will be in the unit of the plot (not in pixels). -You can use it like this: +a position and a nearby data item object as parameters. The returned +coordinates are in the unit of the axes (not in pixels). + +If you set "hoverable" to true, the plot will listen for mouse move +events on the plot area and fire a "plothover" event with the same +parameters as the "plotclick" event. + +You can use "plotclick" and "plothover" events like this: $.plot($("#placeholder"), [ d ], { grid: { clickable: true } }); - $("#placeholder").bind("plotclick", function (e, pos) { - // the values are in pos.x and pos.y + $("#placeholder").bind("plotclick", function (event, pos, item) { + alert("You clicked at " + pos.x + ", " + pos.y); + // secondary axis coordinates if present are in pos.x2, pos.y2 }); -Support for hover indications or for associating the clicks with any -specific data is still forthcoming. +The item object in this example is either null or a nearby object on the form: + + item: { + datapoint: the point as you specified it in the data, e.g. [0, 2] + dataIndex: the index of the point in the data array + series: the series object + seriesIndex: the index of the series + } + +For instance, if you have specified the data like this + + $.plot($("#placeholder"), [ { label: "Foo", data: [[0, 10], [7, 3]] } ], ...); + +and the mouse is near the point (7, 3), "datapoint" is the [7, 3] we +specified, "dataIndex" will be 1, "series" is a normalized series +object with among other things the "Foo" label in series.label and the +color in series.color, and "seriesIndex" is 0. + +Note that the detection of nearby points is still limited to points, +and support for highlighting the point or graph is still forthcoming. Customizing the selection @@ -515,12 +546,15 @@ You enable selection support by setting the mode to one of "x", "y" or similarly for "y" mode. For "xy", the selection becomes a rectangle where both ranges can be specified. "color" is color of the selection. -When selection support is enabled, a "selected" event will be emitted +When selection support is enabled, a "plotselected" event will be emitted on the DOM element you passed into the plot function. The event -handler gets one extra parameter with the area selected, like this: +handler gets one extra parameter with the ranges selected on the axes, +like this: - placeholder.bind("selected", function(event, area) { - // area selected is area.x1 to area.x2 and area.y1 to area.y2 + placeholder.bind("plotselected", function(event, ranges) { + alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to) + // similar for yaxis, secondary axes are in x2axis + // and y2axis if present }); @@ -534,18 +568,20 @@ members: Clear the selection rectangle. - - setSelection(area) + - setSelection(ranges) - Set the selection rectangle. The passed in area should have the - members x1 and x2 if the selection mode is "x" and y1 and y2 if - the selection mode is "y" and both x1, x2 and y1, y2 if the - selection mode is "xy", like this: + Set the selection rectangle. The passed in ranges is on the same + form as returned in the "plotselected" event. If the selection + mode is "x", you should put in either an xaxis (or x2axis) object, + if the mode is "y" you need to put in an yaxis (or y2axis) object + and both xaxis/x2axis and yaxis/y2axis if the selection mode is + "xy", like this: - setSelection({ x1: 0, x2: 10, y1: 40, y2: 60}); + setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } }); - setSelection will trigger the "selected" event when called so you + setSelection will trigger the "plotselected" event when called so you may have to do a bit of shortcircuiting to prevent an eternal loop - if you invoke the method inside the "selected" handler. + if you invoke setSelection inside a "plotselected" handler. - getCanvas() @@ -580,7 +616,7 @@ members: var series = plot.getData(); for (var i = 0; i < series.length; ++i) - alert([i].color); + alert(series[i].color); - setupGrid() diff --git a/NEWS.txt b/NEWS.txt index 4bab393..b0fff73 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -1,7 +1,16 @@ Flot x.x -------- -API changes: timestamps in time mode are now displayed according to +Backwards API change summary: Timestamps are now in UTC. Also +"selected" event -> becomes "plotselected" with new data, the +parameters for setSelection are now different (but backwards +compatibility hooks are in place). + +Interactivity: added a new "plothover" event and this and the +"plotclick" event now returns the closest data item (based on patch by +/david). + +Timestamps in time mode are now displayed according to UTC instead of the time zone of the visitor. This affects the way the timestamps should be input; you'll probably have to offset the timestamps according to your local time zone. It also affects any @@ -9,6 +18,14 @@ custom date handling code (which basically now should use the equivalent UTC date mehods, e.g. .setUTCMonth() instead of .setMonth(). +Support for dual axis has been added (based on patch by someone who's +annoyed and /david). For each data series you can specify which axes +it belongs to, and there are two more axes, x2axis and y2axis, to +customize. This affects the "selected" event which has been renamed to +"plotselected" and spews out { xaxis: { from: -10, to: 20 } ... } and +setSelection in which the parameters are on a new form (backwards +compatible hooks are in place so old code shouldn't break). + Added support for specifying the size of tick labels (axis.labelWidth, axis.labelHeight). Useful for specifying a max label size to keep multiple plots aligned. @@ -26,7 +43,8 @@ sets. Prevent the possibility of eternal looping in tick calculations. Fixed a bug when borderWidth is set to 0 (reported by Rob/sanchothefat). Fixed a bug with drawing bars extending below 0 (reported by James Hewitt, convenient patch by Ryan Funduk). Fixed a -bug with line widths of bars (reported by MikeM). +bug with line widths of bars (reported by MikeM). Fixed a bug with +'nw' and 'sw' legend positions. Flot 0.4 diff --git a/TODO b/TODO index 870b5f2..1add33e 100644 --- a/TODO +++ b/TODO @@ -24,16 +24,13 @@ support for highlighting stuff legend - interactive auto-highlight of graph? + - ability to specify noRows instead of just noColumns labels - labels on bars, data points - - plan "all points" option + - plain "all points" option - interactive "label this point" command -interactive hover over - - fire event with value for points - - fire event with graph id for lines - error margin indicators - for scientific/statistical purposes diff --git a/examples/dual-axis.html b/examples/dual-axis.html new file mode 100644 index 0000000..d97fa8a --- /dev/null +++ b/examples/dual-axis.html @@ -0,0 +1,38 @@ + + +
+ +Dual axis support showing the raw oil price in US $/barrel of + crude oil (left axis) vs. the exchange rate from US $ to € (right + axis).
+ +As illustrated, you can put in secondary y and x axes if you + need to. For each data series, simply specify the axis number.
+ + + + diff --git a/examples/index.html b/examples/index.html index 88c6fb5..36ae0a1 100644 --- a/examples/index.html +++ b/examples/index.html @@ -15,13 +15,11 @@