ChartDirector 6.0 (ColdFusion Edition)
Zooming and Scrolling Framework
Introduction
The ChartDirector Zooming and Scrolling Framework is a framework for "logical zooming" of charts. "Logical zooming" means the chart is plotted with more details as it is zoomed in, but the chart objects do not necessary become bigger. The font size, line width, symbol size, etc, may remain the same. The chart may change type or use different data when it is zoomed in. For example, for a finance chart, it may start as a daily candlestick chart when the time range is long, and becomes a realtime intraday line chart when zoomed to one day.
The main elements of the ChartDirector Zooming and Scrolling Framework include a click and drag user interface to control zooming and scrolling, a set of methods to manage axis scales, and AJAX support to allow updating charts without refreshing the web page.
WebChartViewer and JsChartViewer
The ChartDirector Zooming and Scrolling Framework for web applications is implemented in a server side control and a browser side control.
- WebChartViewer : This is the server side control for ASP.NET Web Forms. It handles AJAX communications with the JsChartViewer on the browser side.
- JsChartViewer : This is a browser side Javascript control that provides the click and drag user interface to control zooming and scrolling. It communicates with the WebChartViewer on the server side using AJAX means to update the chart.
For a typical web page with a zoomable and scrollable chart, the operation is as follows:
- The browser sends a request to the server for the web page.
- On the server side, the charting code creates the chart and assigns it to the WebChartViewer. The WebChartViewer would then generated the HTML for the chart, with other necessary information to support zooming and scrolling and AJAX communications.
- The browser receives the web page from the server and displays the chart. The user can then use mouse or touch actions to zoom or scroll the chart.
- When a zooming or scrolling action occurs, the JsChartViewer will send an AJAX request to the server to update the chart.
- On the server side, a new chart is generated based on the updated axis scale, and the WebChartViewer returns it to the browser as an AJAX response. The browser can then display the updated chart to the user.
The last three steps may repeat as the user continues to zoom or scroll the chart.
The above is only a brief description and omit many details. For example, custom parameters may be included in the AJAX request/response. Besides mouse or touch actions on the chart, the framework allows using external controls or custom methods for zooming and scrolling. In addition to updating the chart, the framework allows other custom things to be updated. These features will be discussed in the following sections.
The View Port
In the ChartDirector Zooming and Scrolling framework, the view port is used to determine which part of the data range should be plotted.
The view port can be imagined as a rectangular window over the entire XY data range. For example, suppose the full data range is 10 years in the horizontal direction. A view port width of 0.1 means only 10% of the horizontal data range should be plotted. In other words, the chart is zoomed in so that only 1 year of data is visible. The view port left side will determine exactly which year of data that is visible.
The view port position and size can be get and set on both the server side and the client side using the following methods. The full data range is always assumed to be 0 to 1. So the view port position and size should be between 0 to 1.
Server Side Methods
Browser Side Methods
The Zoom and Scroll User Interface
The
JsChartViewer control allows using mouse actions on the chart to control zooming and scrolling. Several mouse usage modes are supported, including drag to scroll, drag to select a region to zoom into, click to zoom in, and click to zoom out. They can be selected using
JsChartViewer.setMouseUsage. The zooming and scrolling direction can be horizontal only, vertical only, or both, configurable using
JsChartViewer.setZoomDirection and
JsChartViewer.setScrollDirection.
When the user performs zooming or scrolling,
JsChartViewer will update the view port to reflect the new visible region and fires the
JsChartViewer.ViewPortChanged event . In typical usage,
JsChartViewer.attachHandler can be used to attach
JsChartViewer.partialUpdate as the ViewPortChanged event handler. This will cause an AJAX request to be sent to the server to update the chart when the view port changes.
WebViewPortControl and JsViewPortControl
In addition to using mouse or touch actions directly on the chart being zoomed or scrolled, ChartDirector provides the
WebViewPortControl and
JsViewPortControl to let users visualize and manipulate the view port managed by the
JsChartViewer. The WebViewPortControl works on the server side. The JsViewPortControl is the corresponding browser side Javascript control.
In typical usage, the WebViewPortControl displays an "overall chart" that shows the full data range. On the browser side, the JsViewPortControl connects with the JsChartViewer using
JsViewPortControl.setViewer. The JsViewPortControl can then include a rectangle on the overall chart to represent the view port. The user can drag the rectangle to move the view port, which corresponds to scrolling. The user can also drag the rectangle border to resize the view port, which corresponds to zooming. In additional, the user can drag a new rectangular region as the new view port or click on a point on the overall chart to center the view port at that point. These user interactions can be configured with the methods of the WebViewPortControl or JsViewPortControl.
Zoom and Scroll with External Controls
In some applications, it may be desirable to have other ways to control zooming and scrolling. For example, in finance charts, there are often links that allow the user to jump to certain predefined period (eg. the last 30, 90, 180 or 360 days), which are essentially a type of zooming and scrolling.
To perform zooming and scrolling using external controls, the controls can update the view port position and size, and then fire the
JsChartViewer.ViewPortChanged event by using
JsChartViewer.raiseViewPortChangedEvent.
In some cases, the updates may need to be bi-directional - that is, the external controls may need to be updated when the view port changes. For example, suppose a scrollbar control is used to scroll the chart. When the user drags on the scrollbar, it can update the view port. The user may also drag on the chart directly to scroll the chart. In this case, the scrollbar would need to be updated to reflect that the changed view port.
To handle updating external controls when the view port changes, one may use the
JsChartViewer.PostUpdate event. This event occurs after the server has responded to the AJAX request from the browser, at which stage the view port should have been confirmed and validated by the server. External controls can be updated in the PostUpdate event handler to reflect the view port positions.
Custom Parameters in AJAX Requests and Responses
Apart from the view port state, in some cases, other custom parameters may be required to determine how to update the chart. For example, in a finance chart application, the server may need to know the "ticker symbol" to determine which data series to draw, and the "ticker symbol" can be controlled by the user through an HTML text box on the web page. In this case, during AJAX chart update, the contents of the HTML text box would also need to be sent to the server.
Similarly, in some cases, apart from updating the chart, the server may want to update other things in the web page. In this case, custom parameters would need to be sent from the server to the browser.
In ChartDirector,
WebChartViewer.setCustomAttr,
WebChartViewer.getCustomAttr,
JsChartViewer.setCustomAttr and
JsChartViewer.getCustomAttr can be used to transfer custom parameters between the server and the browser during an AJAX chart update.
In typical usage, to transfer parameters from the browser to the server, one may use
JsChartViewer.setCustomAttr in a
JsChartViewer.PreUpdate event. The PreUpdate event occurs just before an AJAX chart update, so any parameters set will be transferred immediately to the server. On the server side,
WebChartViewer.getCustomAttr can be used to retrieve the parameters. If the server needs to transfer custom parameters to the browser side, it can use
WebChartViewer.setCustomAttr. The browser can retrieve the parameters using
JsChartViewer.getCustomAttr in a
JsChartViewer.PostUpdate event, which occurs immediately after the browser receives an AJAX response from the server.
Managing Axis Scale
The view port always assumes the full data range to be 0 to 1. In practice, a chart can have multiple x-axes and y-axes, using a combination of date/time, linear and logarithmic scales.
To facilitate conversion between the view port and the axis scale, ChartDirector includes a number of methods as follows:
In typical usage,
WebChartViewer.setFullRange is used to define the full data ranges for the axes in the chart.
WebChartViewer.getValueAtViewPort and
WebChartViewer.getViewPortAtValue can then be used to map between data values and view port positions.
WebChartViewer.syncLinearAxisWithViewPort,
WebChartViewer.syncLogAxisWithViewPort and
WebChartViewer.syncDateAxisWithViewPort can be used to synchronize the axis scale with the view port position.
If
WebChartViewer.syncLinearAxisWithViewPort,
WebChartViewer.syncLogAxisWithViewPort or
WebChartViewer.syncDateAxisWithViewPort is called without first defining the full data range, ChartDirector will automatically configure the axis scale based on the data in the chart, and assume it to be the full data range. This is useful if the full data range is not known but must be inferred from the data.
Server Side Code Structure
The server side code needs to handle both the initial page request and subsequent AJAX chart updates. The first thing it needs to do is to distinguish which type of requests it is handling, so that it can execute the appropriate code.
WebChartViewer.isPartialUpdateRequest can be used to detect if the current HTTP request is an AJAX chart update request.
Initial Page Request
In the initial page request, the chart image created can be stored in a session variable using
BaseChart.makeSession, or in a temporary file using
BaseChart.makeTmpFile.
The URL to retrieve the chart should be set to the WebChartViewer using
WebChartViewer.setImageUrl. In addition, the chart metrics (which contains the plot area position and size) should be obtained using
BaseChart.getChartMetrics and set to the WebChartViewer using
WebChartViewer.setChartMetrics. If an image map is available for the chart, it should be set to the WebChartViewer using
WebChartViewer.setImageMap.
After setting up the WebChartViewer,
WebChartViewer.renderHTML should be used to generate the HTML tags that represent the chart. These tags may then be inserted into the HTML web page to be sent to the browser.
AJAX Chart Updates
In an AJAX chart update, only the chart needs to be updated. Instead of using
WebChartViewer.renderHTML to generate the HTML tags for the chart, the server should use
WebChartViewer.partialUpdateChart to return an AJAX response to the browser.
Note that HTTP GET or POST parameters in the initial page request would no longer exists in subsequent AJAX chart updates. If these parameters are necessary in AJAX chart updates, they should be saved as custom attributes using
WebChartViewer.setCustomAttr during initial page request. As custom attributes are persistent across AJAX chart updates, they can be retrieved using
WebChartViewer.getCustomAttr and used in charting code.
Browser Side Code Structure
On the browser side, the typical code structure is:
Library Files Required for Zoomable/Scrollable Charts
A zoomable/scrollable chart requires the following files. Please ensure you copy the necessary files to the proper directories (typically the same directory as the script) and reference them using the proper paths.
File | Description
|
---|
getchart.cfm | The standard utility script used in the <IMG> tag URL for retrieving the chart image from a session variable (see BaseChart.makeSession).
|
cdjcv.js zoomin.cur zoomout.cur nozoom.cur wait.gif spacer.gif | The script file for JsChartViewer and the supporting cursor and image files. They must be in the same directory.
|
Zooming and Scrolling Examples
Sample Code | Description
|
---|
Simple Zooming and Scrolling | A simple zoomable and scrollable chart with tooltips, using mouse click and drag to control zooming and scrolling.
|
Zooming and Scrolling with Track Line | Besides using mouse click and drag for zooming and scrolling, this example has buttons for jumping immediately to display data for the last 30 days, last 90 days, last year or all time. There are check boxes to select the data series to plot, and an update button to immediately update the chart. In addition, the chart includes a track cursor that updates the legend dynamically to display the data values as the mouse cursor moves over the chart.
|
Zooming and Scrolling with Viewport Control | The example extends the Zooming and Scrolling with Track Line example by adding a WebViewPortControl to visualize and control the viewport, and a button to download the chart as PDF.
|
XY Zooming and Scrolling | This example demonstrates zooming and scrolling in both horizontal and vertical directions. In addition to using mouse click and drag, this example demonstrates using a WebViewPortControl to visualize and control the viewport. There is also an image map for data point tooltips.
|
© 2017 Advanced Software Engineering Limited. All rights reserved.