This example demonstrates a surface chart that can rotate in 3D using mouse drag. An important
part of this example is to illustrate continuous chart interaction in ChartDirector by using the
viewport event system.
Continuous Chart Interaction and Viewport Event
When you click on a bar in a bar chart to drill down, it is a one-off interaction. In contrast,
when you drag on a surface chart to rotate it, the chart needs to be updated continuously as
the mouse moves. One way to do this is to update the chart in the mouse move event handler.
However, modern systems can generate a large number of mouse move events very quickly. If the
update is complex or the system is not fast enough, the GUI thread may not have time left to
process other GUI events, and the GUI may appear sluggish.
ChartDirector has supported mouse drag for zooming and scrolling for a long time. Instead of
updating the chart in the mouse move event handler, ChartDirector raises "viewport events" and
updates the chart in the viewport event handler. The viewport system is designed to ensure a gap
between invoking the event handler. This allows other GUI events can get processed and the GUI
Surface Chart Rotation using Viewport Events
Although viewport events are original developed for supporting zooming and scrolling, they are
also useful in applications with continuous chart interaction. This example demonstrates using
viewport events to update a surface chart on mouse drag. As ChartDirector supports multiple
programming languages and GUI frameworks, we just use simplified "pseudo code" for the explanation.
- Define member variables for the chart parameters that can be changed. In this example, they
are the elevation and rotation angles of the surface chart.
double elevationAngle = 30;
double rotationAngle = 45;
- Creates a drawChart function that draws and displays the chart, using the member variables
for the view angles. This function is called once when the window is loaded to display the
initial chart. In this example, the charting code is based on the Surface
Chart (1) sample code in the ChartDirector distribution.
- If the mouse move event handler, if the mouse button is down (that is, mouse dragging),
update the angles based on the distance the mouse has moved, then raise the viewport changed
elevationAngle += y_movement * y_scale_factor;
rotationAngle += x_movement * x_scale_factor;
// generate viewport changed event
- In the viewport changed event handler, just calls drawChart to redraw the chart.
*** Note *** :
If you are new to ChartDirector, it is
recommended you download ChartDirector from the download page
to try it.
It includes complete ChartDirector documentation as well as plenty of sample code designed as tutorials. The
following assumes you already have some basic understanding of ChartDirector.
The sample code in this page is included as part of the "ChartDirector Extra Sample Code Pack".
ChartDirector Extra Sample Code Pack for C++ (MFC, Qt)
- Remember to unblock the ZIP
file before extracting the contents out
- Includes Visual Studio solution (for C++/MFC), Qt project file (for C++/Qt), and both 32-bit and
64-bit ChartDirector Windows DLL.
- If you are using another operating system edition of ChartDirector, simply copy the sample code folder
inside the qtdemo folder to the qtdemo folder of your ChartDirector distribution, and you can compile and
run the Qt sample code.
The original Surface Chart (1) sample code with fixed view angles.
The API to configure the gap between viewport events.