Bezgodov Bezgodov - 1 year ago 182
C# Question

How can I resize a ChartArea during runtine

I have a chart and a ChartArea inside of it.

I want to resize the ChartArea on the Y axis or on the X axis, but I can't do it. I need a ChartArea control element which can be resized with the mouse at runtime.

I need resizing element on the chart (or ChartArea) which resizes my ChartArea..

Answer Source

Here is an example that..

  • ..adds a moveable HorizontalLineAnnotation as a handle and..
  • the AnnotationPositionChanging to use the handle as a slider between two ChartAreas:

enter image description here

Define at class level:

HorizontalLineAnnotation slider =   new HorizontalLineAnnotation();

Set it up and add to Chart:

slider.AllowMoving = true;
slider.LineWidth = 2;
slider.LineColor = Color.DarkSlateGray;
slider.X = 0;     
slider.Y = 50;
slider.Width = 100;

This set the slider to the left at the middle and lets it go across the whole chart.

private void chart1_AnnotationPositionChanging(object sender,
                                               AnnotationPositionChangingEventArgs e)
    if (e.Annotation == slider)
        chart1.ChartAreas[0].Position.Height = (float)slider.Y - 4;
        chart1.ChartAreas[1].Position.Height = (float)(100f - slider.Y) - 4;
        chart1.ChartAreas[1].Position.Y = (float)slider.Y;


This resizes the two ChartAreas, keeping 4% slack for outside stuff. I you have a Title, a top-docked Legend or large Labels you need to give more than the 4%...

Of course you can modify it to change the size of only one ChartArea as well, although I don't see why you would want that..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download