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

Message ListMessage List     Post MessagePost Message

  Multiple Legends in a Line in FinaceChart
Posted by Xord on Jan-31-2020 15:51
Hi,

I am now working on a FinanceChart(C++) and encountered the same problem detailed in this thread:
https://www.chartdir.com/forum/download_thread.php?bn=chartdir_general&pattern=legendbox+truncate&thread=1272434118

I tried implementing the solution provided in the thread, i.e. setting the width of legend box to be very long, then add a empty textbox to cover the overflowed legend. However, I encountered a few problems.

1. the empty textbox of a lower chart will cover the bottom-most y-axis label of the chart above.

2. I tried to set the z-order of the textbox to be smaller then the axis label but larger than the legend box so that it will not cover the axis label but will cover the legend. However, I do not know the z-order of the axis labels and I am unable to set z-order of the legend box (maybe I did something wrong, I used legendBox->setZOrder())

Can you help me with these problems?

  Re: Multiple Legends in a Line in FinaceChart
Posted by Peter Kwan on Feb-03-2020 00:41
Hi Xord,

In recent versions of ChartDirector, there is a more flexible methods to draw the legend box. See:

https://www.advsofteng.com/doc/cdcpp.htm#trackfinance.htm

In the above example, the legend box data corresponds to a track line at a given coordinate. The coordinate is obtained from the mouse move event handler. If you need a fix legend box instead of one that tracks the mouse cursor, you can just set the coordinate to be at the right side of the chart, which means the legend box data will reflect the latest data values.

The legend box is drawn using the code in the "trackFinance" subroutine. In the code, the legend box will have a fixed width and will wrap to multiple lines if necessary. You can change the code:

"<*block,valign=top,maxWidth="

to:

"<*block,valign=top,truncate=1,maxWidth="

The legend box will then be truncated inside of wrapped to multiple lines.

Hope this can help.

Regards
Peter Kwan

  Re: Multiple Legends in a Line in FinaceChart
Posted by Xord on Feb-03-2020 09:35
Thanks for your reply.

I have read the code you provided and see that we are no longer using the build-in legend box, but use TTFText instead.

1. Is it correct to assume we can no longer use getLegend() to obtain the legend box in this case? If so, to manipulate the textbox, is there an easy way to get the textbox, or do we have to store all textbox we created manually?

2. Is there a way to obtain the TextBox object created by TTFText. I thought TTFText and TextBox are 2 different classes.

Thank you for your help.

  Re: Multiple Legends in a Line in FinaceChart
Posted by Peter Kwan on Feb-03-2020 15:40
Hi Xord,

You asked "Is there a way to obtain the TextBox object created by TTFText?". Actually, it is the other way around, the TextBox object creates the TTFText object internally.

In the trackFinance sample code, it creates the TTFText object directly, like:

TTFText *t = d->text(legendText.str().c_str(), "arial.ttf", 8);
t->draw(plotAreaLeftX + 5, plotAreaTopY + 3, 0x000000, Chart::TopLeft);
t->destroy();

You can actually use a TextBox object instead. The TextBox object can be created by using BaseChart.addText. It is like:

TextBox *t = m->addText(plotAreaLeftX + 5, plotAreaTopY + 3, legendText.str().c_str());

Internally, the TextBox object creates the TTFText object to draw the content.

One of the differences between the two approaches is that you must destroy the TTFText after use, otherwise there would be memory leak. On the other hand, you cannot explicitly delete the TextBox. When the BaseChart hosting the TextBox is deleted, the TextBox will be deleted automatically.

In the trackFinance sample code, the legend box is repeated redrawn as the mouse moves without deleting the BaseChart. If we use TextBox, since the TextBox cannot be deleted without deleting the BaseChart, there will be memory leak. So we use TTFText instead.

If in your code, you only create the LegendBox once per chart object, then it does not matter, and you can use either TextBox or TTFText.

If you created a TextBox object and you would need to use it later, you would need to store its pointer in a variable that you can use later. The getLegend is for the built-in legend box, and is not used in this case.

Regards
Peter Kwan