ChartDirector 5.1 (ColdFusion Edition)
Programmable Track Cursor
Introduction
ChartDirector Programmable Track Cursor is implemented by drawing the track cursor on the browser side with Javascript. For example, drawing a horizontal line and a vertical line will create a crosshair cursor. The drawing is typically performed in a
JsChartViewer.MouseMovePlotArea event handler so that the track cursor follows the mouse cursor. It is also possible to draw the track cursor at other times. For example, one may program the track cursor so that it is movable by arrow keys. In a realtime chart, the track cursor can be programmed to sweep through the chart as new data points are added.
Javascript Chart Model
To draw track cursors on the browser side, certain chart configuration (such as the position and size of the plot area, the axis scale of all axes, the data values, etc) needs to be accessible on the browser side. In ChartDirector, this is achieved by using the
Javascript Chart Model.
To set up the Javascript Chart Mode,
BaseChart.getJsChartModel can be used on the server side to generate a data structure containing the chart configuration. The data structure can then be passed to the WebChartViewer using
WebChartViewer.setChartModel, which will send it to the browser. On the browser side, the
Javascript Chart Viewer will use the data structure to construct Javascript objects representing the chart. These objects can be accessed using
JsChartViewer.getChart.
MouseMovePlotArea Event
By far the most common usage of track cursor is when the mouse is moving over the plot area of an
XYChart. The
JsChartViewer.MouseMovePlotArea event , which occurs when the mouse moves over the plot area, is specially designed to facilitate track cursor implementation.
A special feature of MouseMovePlotArea is that it occurs even if the mouse is slightly outside the plot area, in which case
JsChartViewer.getPlotAreaMouseX and
JsChartViewer.getPlotAreaMouseY will report as if the mouse is still on the edge of the plot area. This feature allows the user to easily position the track cursor on the edge of the plot area, at which the latest data points are often located. The margin around the plot area that would still trigger MouseMovePlotArea is configurable using
JsChartViewer.setPlotAreaMouseMargin.
JsChartViewer.attachHandler can be used to attach a function as the MouseMovePlotArea event handler of a chart. This should be performed only after the chart is loaded. It is recommended that the code to attach the MouseMovePlotArea event handler be executed in the <body> or window onload event handler of the web page.
JsChartViewer Drawing Methods
To facilitate drawing track cursors,
JsChartViewer includes a number of methods to draw lines, rectangles and text over the chart as follows.
Snapping to the Nearest X Data Position
Very often, the track cursor is not drawn exactly at the mouse cursor position, but is drawn at the data point position nearest to the mouse cursor in the x direction. ChartDirector includes a method
JsXYChart.getNearestXValue, which will search all data in the chart to get the x data value nearest to a given point. This value can then be used to obtain the x pixel coordinate (using
JsXYChart.getXCoor) for drawing the track cursor. It can also be used to look up the data points (using
JsLayer.getXIndexOf) so as to draw dots that track the data points, generate dynamic data labels, axis labels or legend entries.
Files Required for Javascript Chart Viewer
To use the
Javascript Chart Viewer, your web site would need to contain the following files, which should be in the same directory. In your web page, please include a <script> tag to load "cdjcv.js".
Filename | Description |
---|
cdjcv.js | The main Javascript library file. |
wait.gif | Rotating clock wait symbol. |
spacer.gif | A 1-pixel transparent image. |
zoomin.cur | Zoom in cursor. |
zoomin.cur | Zoom out cursor. |
nozoom.cur | No zoom cursor. |
Sample Track Cursors
ChartDirector includes a number of sample drawing routines in its sample code. You may reuse them, or modify them for your own needs. You may also develop your own track cursor drawing code to implement the track cursor behaviour you want.
Sample Code | Description
|
---|
Track Line with Legend | Demonstrates a track cursor that consists of a vertical line snapped to the nearest x data position. There are dots to highlight the nearest data points, and a dynamically updated legend showing their values.
|
Track Line with Data Labels | Demonstrates a track cursor that consists of a vertical line snapped to the nearest x data position with a floating x-axis label. There are also dots to highlight the nearest data points, with labels beside them displaying their values.
|
Track Line with Axis Labels | Demonstrates a track cursor that uses horizontal and vertical lines to connect the nearest data points to the x-axis and y-axis, with floating axis labels showing their values.
|
Track Box with Legend | Demonstrates a track cursor that consists of a rectangle enclosing the slot corresponding to the nearest x-axis label. A floating legend box that moves with the mouse cursor is used to display the data values in that slot.
|
Crosshair with Axis Labels | Demonstrates a track cursor that consists of a vertical line and a horizontal line at the mouse cursor position, thereby forming a crosshair. Floating axis labels are used to display the cursor position.
|
Finance Chart Track Line | This is similar to Track Line with Legend, but is modified to apply to the FinanceChart object.
|
Zooming and Scrolling with Track Line | Demonstrates how to apply track cursors to a Zoomable and Scrollable chart. The track cursor drawing code is the same as that in Track Line with Legend.
|
Realtime Chart with Track Line | Demonstrates how to apply track cursors to a Realtime chart. The track cursor drawing code is the same as that in Track Line with Legend.
|
© 2012 Advanced Software Engineering Limited. All rights reserved.