|
Dynamically Hide/Show legend |
Posted by herve on Mar-01-2011 17:45 |
|
Hello,
I would like to dynamically hide or show the legend in a chart.
By dynamically, I mean when the legend box is too big comparing to the plot area.
My charts have always the same size (800*500). The goal is to avoid the legend to take too much space in the chart.
The condition is :
- if the legend height is greater than 150, then hide it.
- if the legend height is lesser or equal to 150, then show it.
How could I do that? because I have to call the method BaseChart::layoutLegend() to know the size of the LegendBox, and once this is done, I can't remove or just hide the legend box.
I'm using ChartDirector V5.0.
Thanks,
Herv |
Re: Dynamically Hide/Show legend |
Posted by Peter Kwan on Mar-02-2011 02:15 |
|
Hi herve,
You can move the legend box after layoutLegend. (It is common for people to position the legend box based on its size.) To hide the legend box, simply move it outside the chart.
For example, in VB/VBScript:
Call c.layoutLegend()
If c.getLegend().getHeight() > 150 Then
Call c.getLegend().setPos(-9999, 9999)
End If
Hope this can help.
Regards
Peter Kwan |
Great tip, Thank you |
Posted by daniel on Mar-02-2011 15:41 |
|
One can move the legend once layed out:)
I was unaware of this functionality, thanks a lot.
One is sometimes trapped with the final layout of the legend. Hiding it is certainly an option. It would be great as well to have some way to layout the legend again in order to improve the set up (say changing the fonts or setcols settings) for real... Or alternatively to calculate its size before the layout. |
Re: Great tip, Thank you |
Posted by Peter Kwan on Mar-03-2011 00:19 |
|
Hi Daniel,
Currently, you can layout the legend again by re-creating the chart. So the code structure is like:
(a) Set up the initial legend font size
(b) Create chart, layout legend and check if the legend size is acceptable. If not, modify font size and repeat.
As long as you do not actually render the chart (call makeSession or makeChart), re-creating the chart object usually takes negligible amount of CPU time, even if you have huge amount of data.
Hope this can help.
Regards
Peter Kwan |
Re: Great tip, Thank you |
Posted by daniel on Mar-03-2011 02:43 |
|
Peter Kwan wrote:
Currently, you can layout the legend again by re-creating the chart. So the code structure is like:
(a) Set up the initial legend font size
(b) Create chart, layout legend and check if the legend size is acceptable. If not, modify font size and repeat.
As long as you do not actually render the chart (call makeSession or makeChart), re-creating the chart object usually takes negligible amount of CPU time, even if you have huge amount of data.
Hope this can help.
Regards
Peter Kwan
Hi Peter,
Of course the Chartdirector part of the process is highly efficient (kudos...). Sure you can iterate on that part effectively. But sometimes the rest of the process can be demanding in terms data mangling. Which makes it longer and anyway less straightforward codewise to process in the way you recommend.
I currently process in a different way, pushing the "test code" in subroutine using similar patterns with limited process, basically empty graphs, extract relevant informations and re-use it later.
I do that "legend sizing" but for Axis().getMinValue and getMinValue as well. There are cases where you need the some of this information before processing it.
My input - as for the input on Edward Tufte sparklines - was for improvements of the API. But sure we can circumvent these in some way. The API is brilliant! |
|