How to adapt the chart rect in a tool?
Posted: Wed Mar 17, 2010 10:53 am
Hi,
I am developing a custom tool for TChart.NET. I would like to know how I can make the tool resize the chart rectangle.
Suppose I want to auto-position the tool on my chart, and have it next to the chart for example on the right. Then I would like the normal chart to shrink down its ChartRect by the width of my tool. You can compare it to the legend, which auto-positions itself, and does not overlap with the chart region itself. I want my tool to behave the same way.
Currently I see no way, by examining the TChart code to achieve this. In a tool you can override the ChartEvent, and capture there some of the events.
I want to resize the chart rectangle before the legend, walls, axes and series are drawn. To make sure that all the rest is properly adapting to my reduced chart rectangle.
The only one I currently see that would work is the BeforeDrawEventArgs chart event, and there adapt the ChartRect with this code in my tool:
However this has no effect, since the code of the InternalDraw of the Chart class is:
So before the event you already cache the chart rectangle. So if I would change it in my tool, it would have no effect. I would possibly work if you would just move that first line down:
To prove that I need to do at that point, I once did the same thing by capturing the BeforeDrawAxesEventArgs in my tool. It works and you see the effect, however then it is already to late, and some things (like walls, legend, ...) are already drawn. So you end up with a screwed-up chart where some things are drawn on the original chart rectangle, and some on my reduced one.
Is there a good reason why TeeChart decides not to allow to resize the chart rect in the BeforeDraw chart event?
Do you see another option to achieve what I want to do?
kind regards,
Dimitri
I am developing a custom tool for TChart.NET. I would like to know how I can make the tool resize the chart rectangle.
Suppose I want to auto-position the tool on my chart, and have it next to the chart for example on the right. Then I would like the normal chart to shrink down its ChartRect by the width of my tool. You can compare it to the legend, which auto-positions itself, and does not overlap with the chart region itself. I want my tool to behave the same way.
Currently I see no way, by examining the TChart code to achieve this. In a tool you can override the ChartEvent, and capture there some of the events.
I want to resize the chart rectangle before the legend, walls, axes and series are drawn. To make sure that all the rest is properly adapting to my reduced chart rectangle.
The only one I currently see that would work is the BeforeDrawEventArgs chart event, and there adapt the ChartRect with this code in my tool:
Code: Select all
protected override void ChartEvent(EventArgs e)
{
base.ChartEvent(e);
if (e is BeforeDrawEventArgs)
{
Rectangle chartRect = this.Chart.ChartRect;
chartRect.Inflate(-this.Width, 0);
this.Chart.ChartRect = chartRect;
}
}
Code: Select all
internal void InternalDraw(Graphics g, bool noTools)
{
Rectangle chartRect = this.ChartRect;
if (!noTools)
{
this.BroadcastToolEvent(new BeforeDrawEventArgs());
}
this.redrawing = true;
this.CalcInvertedRotation();
foreach (Series series in this.series)
{
if (series.bActive)
{
series.DoBeforeDrawChart();
}
}
if (!this.Graphics3D.SupportsFullRotation)
{
this.DrawTitlesAndLegend(g, ref chartRect, true);
}
this.ChartRect = chartRect;
this.SetSeriesZOrder();
....
}
Code: Select all
internal void InternalDraw(Graphics g, bool noTools)
{
if (!noTools)
{
this.BroadcastToolEvent(new BeforeDrawEventArgs());
}
Rectangle chartRect = this.ChartRect;
...
}
Is there a good reason why TeeChart decides not to allow to resize the chart rect in the BeforeDraw chart event?
Do you see another option to achieve what I want to do?
kind regards,
Dimitri