GiPi
GiPi

Reputation: 67

C# MSChart - charts area limits

I have one Chart and three ChartArea that are aligned in view, zoom, cursor: this is my related previous post. All things works well except that the three ChartArea are not aligned at the beginning. Following an image of the problem:

graphs example

I think it depends from the digit's number of Y values axis. From some research I try the following configuration:

// selezione e zoom
dlChart.ChartAreas[VOLTAGE_AREA].CursorX.Interval = 1;
dlChart.ChartAreas[VOLTAGE_AREA].CursorX.IsUserEnabled = true;
dlChart.ChartAreas[VOLTAGE_AREA].CursorX.IsUserSelectionEnabled = true;
// generale
dlChart.ChartAreas[VOLTAGE_AREA].AxisX.LabelStyle.Format = "dd/MM/yy - HH:mm:ss.fff";       
dlChart.ChartAreas[VOLTAGE_AREA].AxisX.ScaleView.Zoomable = true;
dlChart.ChartAreas[VOLTAGE_AREA].AxisY.LabelStyle.Format = "D5";

In witch the last row:

dlChart.ChartAreas[VOLTAGE_AREA].AxisY.LabelStyle.Format = "D5";

should specifies always five digits. This mitigate in some way the problem but it doesn't desappers. Furthermore with this row the program starts to throws very lots exceptions of form below any time I scroll the graph:

Generate exception: 'System.FormatException' in mscorlib.dll

Does anyone knows the solution for this problem? Thanks in advance.

Upvotes: 2

Views: 2615

Answers (2)

TaW
TaW

Reputation: 54453

You may want to take control of the size of the InnerPlotPosition.

(But Baddack's solution is simpler and more flexible!)

Here is an example:

After setting up a Chart with three CharAreas, setting Minima and Maxima as well as adding one DataPoint to each we get this :

enter image description here

Your issue is showing clearly.

After setting the InnerPlotPosition to a fixed percentage it looks like this:

enter image description here

Here is how to set the InnerPlotPosition size:

ca1.InnerPlotPosition = new ElementPosition(10, 5, 80, 90);
ca2.InnerPlotPosition = new ElementPosition(10, 5, 80, 90);
ca3.InnerPlotPosition = new ElementPosition(10, 5, 80, 90);

Note that both ChartArea.Position and ChartArea.InnerPlotPosition are called 'Position' but really are areas of percentages referring to the respective containers!

So my example has a Left distance of 10%, a Top space of 5% and Width of 80% and Height of 90%. Which leaves 10% space at the Bottom and 5% at the Right. Note: All are referring to the ChartAreas not the ClientArea of the Chart! (Which are still at Auto, which maximizes the size.)

This was my initial setup:

ChartArea ca1 = chart.ChartAreas[0];
ChartArea ca2 = chart.ChartAreas[1];
ChartArea ca3 = chart.ChartAreas[2];


Series s1 = chart.Series[0];
Series s2 = chart.Series.Add("Series2");
Series s3 = chart.Series.Add("Series3");

s2.ChartArea = ca2.Name;
s3.ChartArea = ca3.Name;

s1.Points.AddXY(1, 7);
s2.Points.AddXY(1, 777);
s3.Points.AddXY(1, Math.PI);

Upvotes: 2

Baddack
Baddack

Reputation: 2053

Have you tried using the chart area alignment options? I would try something like:

//define inner plot position of the chart areas
dlChart.ChartAreas[0].InnerPlotPosition.Auto = true;
dlChart.ChartAreas[1].InnerPlotPosition.Auto = true;
dlChart.ChartAreas[2].InnerPlotPosition.Auto = true;

//set our second chart area's alignments to match our first chart area
dlChart.ChartAreas[1].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
dlChart.ChartAreas[1].AlignmentStyle = AreaAlignmentStyles.All;
dlChart.ChartAreas[1].AlignWithChartArea = dlChart.ChartAreas[0].Name;

//set our third chart area's alignments to match our first chart area
dlChart.ChartAreas[2].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
dlChart.ChartAreas[2].AlignmentStyle = AreaAlignmentStyles.All;
dlChart.ChartAreas[2].AlignWithChartArea = dlChart.ChartAreas[0].Name;

Upvotes: 1

Related Questions