ChartDirector 7.0 (Java Edition)

Programmable Track Cursor


Introduction

For web applications, 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.

Track Cursor Events

By far the most common usage of track cursor is when the mouse is moving over the plot area of an XYChart. On touch enabled devices, the touch point can also be used. To implement these behaviour, the code can handle suitable mouse and touch events and draw the track cursor. The most common used events are:

A special feature of these plot area events is that it occurs even if the mouse or touch interaction is slightly outside the plot area, in which case JsChartViewer.getPlotAreaMouseX and JsChartViewer.getPlotAreaMouseY will report as if the interaction 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 plot area events is configurable using JsChartViewer.setPlotAreaMouseMargin.

In addition to the mouse or touch point moving over the chart, it is also possible for the chart to move under the mouse or touch point. For example, in a smart phone, the user can view the web page in portrait or landscape mode, and the web page can be designed to layout differently in these two modes. In this case, the chart can move or resize when the phone is rotated, and the track cursor would need to update. The JsChartViewer.ChartMove event can be used to detect chart movement or resizing and update the track cursor.

JsChartViewer.attachHandler can be used to attach a function as the handler to these events. This should be performed only after the chart is loaded. It is recommended that the code to attach the 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.

MethodDescription
JsChartViewer.drawHLineDraws a horizontal line on the chart.
JsChartViewer.drawVLineDraws a vertical line on the chart.
JsChartViewer.showCrossHairDisplays the crosshair cursor.
JsChartViewer.showTextBoxDraws a textbox on the chart.
JsChartViewer.showClickableTextBoxDraws a clickable textbox on the chart.
JsChartViewer.hideObjHides one or more objects drawn by JsChartViewer.
JsChartViewer.setAutoHideHides one or more objects drawn by JsChartViewer when a certain event occurs.
JsChartViewer.removeAutoHideCancels the automatic action sets up by JsChartViewer.setAutoHide.
JsChartViewer.htmlRectGenerates HTML code that represents an inline color box.

Snapping to the Nearest X Data Position

Very often, the track cursor is not drawn exactly at the interaction position, but is drawn at the data point position nearest to the interaction position 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".

FilenameDescription
cdjcv.jsThe main Javascript library file.
wait.gifRotating clock wait symbol.
spacer.gifA 1-pixel transparent image.
zoomin.curZoom in cursor.
zoomin.curZoom out 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 CodeDescription
Track Line with LegendDemonstrates 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 LabelsDemonstrates 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 LabelsDemonstrates 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 Line with Vertical LegendDemonstrates 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 vertical legend showing their values. The legend box is put on the left side of the plot area if the track cursor is on the right side and vice versa. This ensures the legend box would not block the region near the track cursor.
Track Box with Floating LegendDemonstrates 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 LabelsDemonstrates 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 LineThis is similar to Track Line with Legend, but is modified to apply to the FinanceChart object.
Zooming and Scrolling with Track LineDemonstrates 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.
Zooming and Scrolling with Viewport ControlDemonstrates how to apply track cursors to a Zoomable and Scrollable chart. The track cursor drawing code is the similar to that in Track Line with Legend.
Real-Time Chart with Track LineDemonstrates how to apply track cursors to a Realtime chart. The track cursor drawing code is the same as that in Track Line with Legend.