0

I have 2 charts: chart1 and chart2.

I want both charts to have same innerPlotSize and location.

But chart1 has a secondary yaxis.

This does NOT work:

chart2.ChartAreas[0].AlignWithChartArea = chart1.ChartAreas[0].Name;
chart2.ChartAreas[0].AlignmentStyle = AreaAlignmentStyles.PlotPosition;
chart2.ChartAreas[0].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
M. Schena
  • 2,039
  • 1
  • 21
  • 29
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • You could create a new Postition for the second chartarea from the one of the first chartarea. simply copy the x and Width form one then keep the Y and Height from the other! – TaW Jun 01 '16 at 15:51
  • chart2.ChartAreas[0].InnerPlotPosition = chart1.ChartAreas[0].InnerPlotPosition; appears to make NO DIFFERENCE. Also, where do I find width of InnerPosition? – ManInMoon Jun 01 '16 at 16:35
  • Oh, sorry. I had assumed that the two charts are actually in the same chart control in different chartareas. If they both are set to auto the code indeed makes no difference. you need to get the actual numbers (in percent of the respective container !!) by calling `RectangleF r1 = chart.ChartAreas[0].Position.ToRectangleF(); RectangleF r2 = chart.ChartAreas[0].InnerPlotPosition.ToRectangleF();` – TaW Jun 01 '16 at 17:27
  • See [here](http://stackoverflow.com/questions/36221672/constrain-aspect-ratio-in-windowsforms-datavisualization-chart/36248343#36248343) [and here](http://stackoverflow.com/questions/36491231/ms-chart-rectangular-annotation-width-in-percent-and-not-pixel/36497262#36497262) for a discussion! - In your case it would require you to pick the one where the Left value is the larger as the template for left&width.. – TaW Jun 01 '16 at 17:30

1 Answers1

0

This will align the InnerPlotPositions of two Charts:

    // align the controls:
    yourChart1.Left = yourChart2.Left;
    yourChart1.Size = yourChart2.Size;


    // get the numbers of the current innerplotpositions
    RectangleF ri1 = yourChart1.ChartAreas[0].InnerPlotPosition.ToRectangleF();
    RectangleF ri2 = yourChart2.ChartAreas[0].InnerPlotPosition.ToRectangleF();

    if (ri1.Width < ri2.Width)
    {
        yourChart2.ChartAreas[0].InnerPlotPosition =
            new ElementPosition(ri1.Left, ri2.Top, ri1.Width, ri2.Height);
    }
    else 
    {
        yourChart1.ChartAreas[0].InnerPlotPosition =
            new ElementPosition(ri2.Left, ri1.Top, ri2.Width, ri1.Height);
    }

Before and after:

enter image description hereenter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
  • That seems quite good -thank you. Only problem now is that when first chart is in Zoom mode, second is miss-aligned by the amount of the yAxis scroll bar. Any idea how to fix that? – ManInMoon Jun 01 '16 at 21:25
  • I believe the scrollbars are part of the chararea, not the innerplotposition. So we'll have to go back one step and calculate the values from both. The ipp ist nested in the chartarea which is nested in the chart client rectangle. - . I'm tied up tonight; I hope I can get it to work tomorrow. – TaW Jun 01 '16 at 22:14
  • Actually the IPP are still aligned when zooming in on the (originally) larger of the charts. The solution is simple: You need to re-align them on every zoom just as on every resize! So best call the alignment from the ScaleViewChanged event! – TaW Jun 02 '16 at 06:49
  • 1
    Thanks for this. After I had got this working - I realised it would be much easier to have 2 chartArea on the same control (I believe you mentioned that in another thread). Then you can use the Align functionality which works perfectly. – ManInMoon Jun 02 '16 at 08:51