ChartDirector 7.0 (Java 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
The initial page request is just a normal page request, and can be handled just like a normal web page request.
AJAX Chart Updates
In an AJAX chart update, only the chart needs to be updated. The server should return an AJAX response using
WebChartViewer.partialUpdateChart instead of the full web page.
Note that HTTP GET or POST parameters in the initial page request 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:
- Include the ChartDirector Javascript Library "cdjcv.js" in the web page. This is typically by inserting the following code in the <HEAD> block of the page.
<script type="text/javascript" src="cdjcv.js"></script>
- Include code to initialize the JsChartViewer after the web page has loaded. This is typically done by performing the initialization function in the onload event handler of the window object or the <BODY> tag. A typical example is:
//
// Execute the following initialization code after the web page is loaded
//
JsChartViewer.addEventListener(window, 'load', function() {
// Get the Javascript ChartViewer object (please replace idOfChartViewer below with
// the real id used in your code)
var viewer = JsChartViewer.get('idOfChartViewer');
// Update the chart when the view port has changed (eg. when the user zooms in using the mouse)
viewer.attachHandler("ViewPortChanged", viewer.partialUpdate);
// Set the zoom and scroll mode to horizontal
viewer.setScrollDirection(JsChartViewer.Horizontal);
viewer.setZoomDirection(JsChartViewer.Horizontal);
// Set the initial mouse usage to "scroll"
viewer.setMouseUsage(JsChartViewer.Scroll);
document.getElementById("scrollChart").checked = true;
});
- If there are JsChartViewer.PreUpdate and JsChartViewer.PostUpdate event handlers, they can also be set up during initialization.
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.jsp | The standard utility script used in the <IMG> tag URL for retrieving the chart image from
|
cdjcv.js zoomin.cur zoomout.cur nozoom.cur wait.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.
|
© 2021 Advanced Software Engineering Limited. All rights reserved.