How to adapt the chart rect in a tool?

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
dimitrig
Newbie
Newbie
Posts: 17
Joined: Wed Jul 18, 2007 12:00 am
Location: Belgium
Contact:

How to adapt the chart rect in a tool?

Post by dimitrig » 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:

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;
    }
}
However this has no effect, since the code of the InternalDraw of the Chart class is:

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();

    ....
}
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:

Code: Select all

internal void InternalDraw(Graphics g, bool noTools)
{
    if (!noTools)
    {
        this.BroadcastToolEvent(new BeforeDrawEventArgs());
    }
    Rectangle chartRect = this.ChartRect;

    ...
}
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

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: How to adapt the chart rect in a tool?

Post by Narcís » Wed Mar 17, 2010 2:04 pm

Hi Dimitri,

Yes, you can use CustomChartRect, for example:

Code: Select all

			tChart1.Chart.CustomChartRect = true;
			Rectangle rect = new Rectangle(100, 100, 200, 200);
			tChart1.Chart.ChartRect = rect;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: How to adapt the chart rect in a tool?

Post by Narcís » Wed Mar 17, 2010 2:17 pm

Hello Dimitri,

I see that CustomChartRect is not working as expected. I've added the issue (TF02014734) to the list to be investigated for future releases.

Using TeeChart for .NET 2009 I get this:
ChartRectNET.jpg
ChartRectNET.jpg (25.42 KiB) Viewed 10193 times
While using TeeChart Pro 2010 VCL beta I get this:
ChartRectVCL.jpg
ChartRectVCL.jpg (26.94 KiB) Viewed 10195 times
Which is what I'd expect from CustomChartRect.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

dimitrig
Newbie
Newbie
Posts: 17
Joined: Wed Jul 18, 2007 12:00 am
Location: Belgium
Contact:

Re: How to adapt the chart rect in a tool?

Post by dimitrig » Wed Mar 17, 2010 4:19 pm

Hi Narcis,

CustomChartRect could indeed be a solution, but for a static scenario.

Here I want to have dynamic control from a tool you add to the chart. Each time the chart is drawn, the tool reserves some place on the chart to draw his things. He can do this by adapting the chart rect, as you already do in TeeChart for fixed integrated things (e.g. legend, header, ...).

I also want to be capable of just adding 2 instances of this tool, and they can reserve their own spot separately, without overlapping.

The thing that I was trying to do, with capturing the BeforeDrawEventArgs is just perfect for this. Every tool then just adapts the chart rect by reducing its size (i.e. calling inflate with negative values). This way you can also have multiple of these tools without a problem, and they will not conflict.

It's only a pitty that this one line that I was talking about (in Steema.TeeChart.Chart.InternalDraw), is not moved a little more to the bottom. From looking with Reflector to the code, this would have no impact, but it would allow me to do what I intended. Do you see it possible to move this one line down, or are there good reasons why this is not possible?

kind regards,
Dimitri

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: How to adapt the chart rect in a tool?

Post by Narcís » Wed Mar 17, 2010 6:53 pm

Hi Dimitri,

We will investigate if the modification you suggest could brake existing functionality and get back to you.

BTW: What about using SubChart tools? You could hide legend, titles, axes, etc.; and everything you don't need to be displayed there. You'll find SubChart examples at All Features\Welcome !\Tools\SubChart Tool in the features demo available at TeeChart's program group.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

dimitrig
Newbie
Newbie
Posts: 17
Joined: Wed Jul 18, 2007 12:00 am
Location: Belgium
Contact:

Re: How to adapt the chart rect in a tool?

Post by dimitrig » Wed Mar 17, 2010 10:36 pm

Hi Narcís,

Thanks for the response.

I've been using SubChart tools for this purpose already in the past. The main problem is that a subchart is also overlapping with the main chart. The SubChart tool is not reducing the ChartRect of the main chart as well. You have to specify absolute position and size of the subchart.

You can solve this overlapping by using a static method to resize the chart. I have been using TChart.Panel.MarginRight then on the main chart, to make sure it is not overlapping with the subchart. But I have experienced that this static method is giving some problems in certain scenarios, so that's why I'm looking for some dynamic method now.

So if you see a solution for the SubChart, that it can auto-position itself (like e.g. the legend) and that in that case it reduces the chart rect automatically, that would also be fine for me. But as far as I can see, that is not available now.


There is one tool that I know of that TeeChart has that is successful in dynamically reducing the chart rectangle, and that is the DataTable tool. However, this tool is using a somewhat "dirty" trick. It places itself next to the labels of an axis. He is just increasing the length of the axis labels, to free up space to draw the data table. My tool is not next to an axis, and I cannot use that method.

kind regards,
Dimitri

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: How to adapt the chart rect in a tool?

Post by Narcís » Thu Mar 18, 2010 12:27 pm

Hi Dimitri,

We have made the change you requested to TeeChart for .NET 2009 source code. We don't think it will have a negative impact with other functionality.

Also, CustomChartRect is not working perfectly in TeeChart VCL as it's not correctly positioning other chart items: legend, axes labels, titles, etc.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

dimitrig
Newbie
Newbie
Posts: 17
Joined: Wed Jul 18, 2007 12:00 am
Location: Belgium
Contact:

Re: How to adapt the chart rect in a tool?

Post by dimitrig » Thu Mar 18, 2010 12:59 pm

Hi Narcís,

Wow... thanks!

Do you think you can make this change also available in v3? We currently stick with this version. Probably we will migrate to 2009 or later version in the future, when the right time is there, but currently we do not plan to upgrade.

kind regards,
Dimitri

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: How to adapt the chart rect in a tool?

Post by Narcís » Thu Mar 18, 2010 2:21 pm

Hi Dimitri,

Yes, I think we will also be able to make the change in v3.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply