0

It works out that when using the autofit of axis labels, the label values take on unappealing values. Has anyone developed code to evaluate range and scale of axis values and then show for example label values at intervals of 1, 5, 10, 20, etc? The syntax I am using is listed below:

                Chart1.Series.Clear()
                Chart1.ChartAreas("ChartArea1").AxisX.MajorGrid.Enabled = False
                Chart1.ChartAreas("ChartArea1").AxisY.MajorGrid.Enabled = False
                Chart1.ChartAreas("ChartArea1").AxisX.LabelStyle.Format = "N2"
                Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Format = "N2"
                Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Angle = 0
                Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Enabled = True
                Chart1.ChartAreas("ChartArea1").AxisX.IsLabelAutoFit = True
                Chart1.ChartAreas("ChartArea1").AxisX.LabelStyle.Font = New System.Drawing.Font("Times New Roman", 12.0F, System.Drawing.FontStyle.Bold)
                Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Font = New System.Drawing.Font("Times New Roman", 12.0F, System.Drawing.FontStyle.Bold)
                Chart1.ChartAreas("ChartArea1").AxisX.LabelStyle.IsEndLabelVisible = True 

2 Answers2

1

RESOLVED: I discovered that if you use the floor and ceiling functions on xmin and xmax, and set intervalautomode=True, then the last right label will typically be displayed, and the labels will appear better. The floor and ceiling prevent label values of -2.3, 5.7, etc.

    Dim xmin, xmax As Double
    xmin = 1.0E+30
    xmax = -1.0E+30
    For i = 1 To 1000
        If x(i) < xmin Then xmin = x(i)
        If x(i) > xmax Then xmax = x(i)
    Next
    Chart1.ChartAreas(0).AxisX.Minimum = Math.Floor(xmin)
    Chart1.ChartAreas(0).AxisX.Maximum = Math.Ceiling(xmax)
    Chart1.ChartAreas(0).AxisX.IntervalAutoMode = True
    Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "N1"
    For i = 1 To 1000
        Chart1.Series(0).Points.AddXY(x(i), y(i))
    Next

You would need to do more work if the range of x is less than 1, for example 0.02 to 0.85, or 0.0002 to 0.005, etc. since the floor and ceiling always round down to the next lower integer and round right to the next greatest integer.

0

You need to set AxisX.Minimum and AxisX.Interval.

Chart1.ChartAreas(0).AxisX.Minimum = 0
Chart1.ChartAreas(0).AxisX.Interval = 1
Chart1.ChartAreas(0).AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount

You can set AxisX.Minimum according to Series.Points(0).XValue if you want.

Chart1.ChartAreas(0).AxisX.Minimum = Math.Floor(Chart1.Series(0).Points(0).XValue / 5) * 5
Chart1.ChartAreas(0).AxisX.Interval = 5
user3093781
  • 374
  • 3
  • 6
  • My thinking was to first determine the range $\Delta= x_{max}-x_{min}$ and then determine how many times $10^x$ goes into the $\Delta$, where $x=10^{-4},10^{-3},10^{-2},10^{-1},10^{0},10^{1},10^{2},10^{3}$ etc. and then determine label intervals of 0.2, 0.5, 1, 5, 10, 50, 100, etc., which look the most appealing. –  Dec 31 '13 at 06:01
  • Please check out [draw-chart-which-axis-interval-is-non-sequential-in-c-sharp](http://stackoverflow.com/questions/20515734/draw-chart-which-axis-interval-is-non-sequential-in-c-sharp/20536243#20536243) – user3093781 Dec 31 '13 at 06:14
  • Thanks- that's a nice log-linear chart with a grid. –  Jan 01 '14 at 17:04
  • The syntax below fixed it: Dim xmin, xmax As Double xmin = 1.0E+30 xmax = -1.0E+30 For i = 1 To 1000 If x(i) < xmin Then xmin = x(i) If x(i) > xmax Then xmax = x(i) Next Chart1.ChartAreas(0).AxisX.Minimum = Math.Floor(xmin) Chart1.ChartAreas(0).AxisX.Maximum = Math.Ceiling(xmax) Chart1.ChartAreas(0).AxisX.IntervalAutoMode = True Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "N1" –  Jan 01 '14 at 18:31