改进VB.NET图表标签值

时间:2013-12-30 15:06:47

标签: graphics charts vb.net-2010 mschart

当使用轴标签的自动调整时,标签值具有不吸引人的值。有没有人开发过代码来评估轴值的范围和比例,然后以1,5,10,20等间隔显示标签值?我正在使用的语法如下所示:

                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 个答案:

答案 0 :(得分:1)

解决:我发现如果你在xmin和xmax上使用地板和天花板功能,并设置intervalautomode = True,那么通常会显示最后一个右侧标签,标签会更好看。地板和天花板防止标签值-2.3,5.7等。

    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

如果x的范围小于1,则需要做更多的工作,例如0.02到0.85,或0.0002到0.005等,因为地板和天花板总是向下舍入到下一个较低的整数并且向右舍入到下一个最大的整数。

答案 1 :(得分:0)

您需要设置AxisX.MinimumAxisX.Interval

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

如果需要,您可以根据AxisX.Minimum设置Series.Points(0).XValue

Chart1.ChartAreas(0).AxisX.Minimum = Math.Floor(Chart1.Series(0).Points(0).XValue / 5) * 5
Chart1.ChartAreas(0).AxisX.Interval = 5