In applications that show multiple charts, you can sync the ticks on the x-axis of each chart.
Use the Margins property to align the axis positions of several chart areas. For example, the following method arranges the x-axis positions for two charts. Note that it can also be modified for syncing three charts.
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub AdjustAxisPositions() C1Chart1.ChartArea.Margins.Left = 10 C1Chart1.ChartArea.Margins.Right = 10 C1Chart2.ChartArea.Margins.Left = 10 C1Chart2.ChartArea.Margins.Right = 10 ' force redrawing Dim img As Image = C1Chart1.GetImage() If img IsNot Nothing Then img.Dispose() End If img = C1Chart2.GetImage() If img IsNot Nothing Then img.Dispose() End If Dim ch1_X As Rectangle = C1Chart1.ChartArea.AxisX.GetAxisRect() Dim ch2_X As Rectangle = C1Chart2.ChartArea.AxisX.GetAxisRect() Dim d As Integer = ch1_X.Left - ch2_X.Left If d > 0 Then C1Chart2.ChartArea.Margins.Left += d ElseIf d < 0 Then c1Chart1.ChartArea.Margins.Left -= d End If d = ch1_X.Right - ch2_X.Right If d > 0 Then C1Chart1.ChartArea.Margins.Right += d ElseIf d < 0 Then C1Chart2.ChartArea.Margins.Right -= d End If End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
void AdjustAxisPositions() { c1Chart1.ChartArea.Margins.Left = 10; c1Chart1.ChartArea.Margins.Right = 10; c1Chart2.ChartArea.Margins.Left = 10; c1Chart2.ChartArea.Margins.Right = 10; // force redrawing Image img = c1Chart1.GetImage(); if( img!=null) img.Dispose(); img = c1Chart2.GetImage(); if( img!=null) img.Dispose(); Rectangle ch1_X = c1Chart1.ChartArea.AxisX.GetAxisRect(); Rectangle ch2_X = c1Chart2.ChartArea.AxisX.GetAxisRect(); int d = ch1_X.Left - ch2_X.Left; if( d>0) c1Chart2.ChartArea.Margins.Left += d; else if( d<0) c1Chart1.ChartArea.Margins.Left -= d; d = ch1_X.Right - ch2_X.Right; if( d>0) c1Chart1.ChartArea.Margins.Right += d; else if( d<0) c1Chart2.ChartArea.Margins.Right -= d; } |