ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Real Time Finance Charting
Posted by Marc on Jul-13-2012 23:46
Hi,

I have looked at the real time demo as well as the finance chart for qt. Both appear to be
doing something along the lines of:
1. Use drawChart() to avoid too many updates in a short period of time
2. Create a new chart
3. Copy the data into the chart
4. Set a bunch of chart parameters
5. Delete old chart and set new chart


First, in the finance demo, why do you not need to use new for:
    // Create the chart object using the selected size
    FinanceChart m(width);
whereas in the other realtime tracking demo you do new and delete every time?


Second, why do we need a different chart object every time? Isn't it more efficient to
update the chart object directly? Are we not allowed to update due to race conditions? If
this is the case and we know there is very little change between updates, can we just use
two charts and switch back and forth between the two?

As an idea maybe in the finance chart should include locks for your updates then.

Finally, as a request for future releases, can you please make finance chart have all either
public or protected functions so we do not need to modify the code and instead we can
inherent this chart.

Thanks,
Marc

  Re: Real Time Finance Charting
Posted by Peter Kwan on Jul-14-2012 01:57
Hi Marc,

Like all C++ code, you can declare something in the stack or in the heap. It is up to you. For example, you may use:

double y[1000];

or you may use:

double *y = new double[1000];

The same applies to ChartDirector objects. You can use:

FinanceChart *m = new FinanceChart(xxxx);

or

FinanceChart m(xxxx);

Of course, if you use "new", you need to use "delete" to clear the memory. If you declare the object in the stack, as according to C++ standard, it will automatically be removed when it goes out of scope.

Creating a new chart object everytime can be efficient. If you really perform timing analyse, you can see that creating a chart object takes negligible time (may be 0.1% of the overall charting time). The majority of the time is rendering the chart and updating the screen, which must be done when the display changes. By requiring a new chart object to be created everytime, it makes the chart object more efficient internally (as it does not need to keep track of things), and so it is not necessarily slower.

If we make something public or protected, it also means we should not change those API in the future, because people will depend on it.

So in a product, public or protected API must be well thought out and be prepared for all possible usages in the many years to come, because they cannot be changed once released. For this reason, it may take more than 10 to 100 times the effort to create a public/protected API as opposed to a private API. For a private API, we just need to make sure it works. We have the freedom to change it in future versions. That is why not all APIs in FinanceChart is public/protected.

If you do want to change the private API, it is appropriate to modify the source code. This ensures you are using a copy of the code and no longer depend on the original code.

Regards
Peter Kwan

  Re: Real Time Finance Charting
Posted by Marc on Jul-14-2012 02:12
in financedemo.cpp doing:
    // Set the chart to the viewer
    viewer->setChart(&m);
will pass a pointer to an object that gets destroyed when the scope exits.

This assigns:
void QChartViewer::setChart(BaseChart *c)
{
    m_currentChart = c;

If it is ok that c gets destroyed right after setChart is called, then why keep a reference to
it in the viewer?


For performance, what about in the cases were we know we are just updating the last
point, which is a common operation for the finance chart? Is there no optimization we can
do for that?

I also wanted to clarify that once I pass the chart to setChart, I should no longer touch the
chart object even for updates?

As per the API, we will just replace all private with protected and deal with code changes
and keep our code separate so that upgrades are relativity easier.

Thanks for your help.

Marc

  Re: Real Time Finance Charting
Posted by Peter Kwan on Jul-16-2012 03:59
Hi Marc,

For the BaseChart object, the QChartViewer does not need it after it has rendered the chart (after setChart is called). So it is safe the delete it. QChartViewer keeps the reference so that your code can get the chart back using QChartViewer::getChart.

So if your code needs to get the BasChart object back later, your code should not delete the chart before that. If you do not need to get the chart back later, your code can delete the chart anytime after setChart.

For example, in the "Track Line with Legend" sample code in ChartDirector Ver 5.1, the sample code does get the chart back in order to support the track cursor when the mouse moves over the chart after the chart is displayed. So it does not delete the chart immediately after setChart.

In the Interactive Financial Chart sample code, the sample code does not use the FinanceChart object after setChart. So the chart can be deleted at anytime after setChart, and the most convenient place is to immediately delete it after setChart.

For updating the chart, if you just append one data point to the chart, the update to the data are minimal. However, the update to the resulting display can be large. For example, in a typical chart, the axis scale depends on the data range. If a new data point is added, the data range can change, and the axis scale can change. If the axis scale changes, the entire chart changes (everything needs to be rescale, which means most of the pixels need to be changed). That means even a minimal update to the data structure can mean a lot of updates to the pixels.

Now updating or creating the new data structure takes negligible CPU time and memory. The majority of the CPU time and memory is in processing pixels. The optimization in ChartDirector is that it does not keep track of which pixel needs to change to save CPU time and memory, since the majority of the pixels may need to change even the data have a minor update. As a result, ChartDirector can render charts quite quickly.

There is a realtime chart sample code included in ChartDirector. You can try to compile it in "Release Mode" and run it outside the debugging to test its speed. When when the chart is updated very frequently, the CPU and memory usage should still be low.

Hope this can help.

Regards
Peter Kwan