如何在System.Web.UI.DataVisualization.Charting中控制自定义标签的宽度?

时间:2011-01-20 16:25:32

标签: .net vb.net charts

我有一些代码使用旧的Dundas图表组件(版本7)生成一个简单的类温度计图表。这是代码:

'Imports Dundas, Dundas.Charting, Dundas.Charting.WebControl
Public Shared Function GetSimpleThermometerChart(ByVal currentValue As Integer, ByVal goalValue As Integer) As Stream
    If goalValue < 0 Then goalValue = 0
    If currentValue < 0 Then currentValue = 0
    If currentValue > goalValue Then currentValue = goalValue

    Dim red = Drawing.Color.FromArgb(255, 192, 80, 77)
    Dim chart1 = New Chart()
    Dim series1 = chart1.Series.Add("Series1")
    Dim defaultArea = chart1.ChartAreas.Add("Default")

    With chart1
        .Legends(0).Enabled = False
        .Height = New System.Web.UI.WebControls.Unit(200)
        .Width = New System.Web.UI.WebControls.Unit(275)
    End With

    With defaultArea
        .BorderStyle = ChartDashStyle.Solid

        ' Y-axis
        .AxisY2.MajorGrid.Enabled = True
        .AxisY2.Interval = goalValue / 2
        .AxisY2.Maximum = goalValue
        .AxisY2.LabelStyle.Interval = goalValue
        .AxisY2.MajorTickMark.Interval = goalValue / 2
        .AxisY2.MajorTickMark.Enabled = False
        .AxisY2.LabelStyle.Font = New System.Drawing.Font("Tahoma", 10)
        .AxisY2.CustomLabels.Add((goalValue * 0.45), (goalValue * 0.55), String.Format(" You're half way to your goal of {0}!", goalValue))
        .AxisY2.CustomLabels.Add((goalValue * 0.9), (goalValue * 0.99), String.Format(" You met the goal of {0}!!!", goalValue))

        ' X-axis
        .AxisX.MajorGrid.Enabled = False
        .AxisX.Maximum = 1
        .AxisX.Minimum = 0.8
        .AxisX.LabelStyle.Enabled = False
        .AxisX.MajorTickMark.Enabled = False
    End With

    ' Populate series data
    Dim pt1 = New DataPoint(1, currentValue)
    series1.Points.Add(pt1)

    With series1
        .Type = SeriesChartType.Column
        .Color = red
        .YAxisType = AxisType.Secondary
    End With

    Dim memStrm As New MemoryStream()
    chart1.Save(memStrm, ChartImageFormat.Png)
    memStrm.Position = 0
    Return memStrm
End Function

此代码生成的图表在左侧显示了一个漂亮的小图表,右侧Y轴上的自定义标签的文本完整,未截断。

Microsoft购买并将Dundas图表组件集成到.NET 4.0框架中,因此我一直在测试图表是否以相同的方式运行。当然,我立刻遇到了不一致的问题。在将上面的代码转换为新的System.Web.UI.DataVisualization.Charting等价物时,我最后得到以下代码:

'Imports System.Web.UI.DataVisualization.Charting
Public Shared Function GetSimpleThermometerChart(ByVal currentValue As Integer, ByVal goalValue As Integer) As Stream
    If goalValue < 0 Then goalValue = 0
    If currentValue < 0 Then currentValue = 0
    If currentValue > goalValue Then currentValue = goalValue

    Dim red = Drawing.Color.FromArgb(255, 192, 80, 77)
    Dim chart1 = New Chart()
    Dim series1 = chart1.Series.Add("Series1")
    Dim defaultArea = chart1.ChartAreas.Add("Default")

    With chart1
        .Height = New System.Web.UI.WebControls.Unit(200)
        .Width = New System.Web.UI.WebControls.Unit(275)
    End With

    With defaultArea
        '.InnerPlotPosition.Height = 100 ' YUCK!  Why???
        '.InnerPlotPosition.Width = 20  ' YUCK!  Why???

        .BorderDashStyle = ChartDashStyle.Solid

        ' Y-axis
        .AxisY2.MajorGrid.Enabled = True
        .AxisY2.Interval = goalValue / 2
        .AxisY2.Maximum = goalValue
        .AxisY2.LabelStyle.Interval = goalValue
        .AxisY2.MajorTickMark.Interval = goalValue / 2
        .AxisY2.MajorTickMark.Enabled = False
        .AxisY2.LabelStyle.Font = New System.Drawing.Font("Tahoma", 10)
        .AxisY2.CustomLabels.Add((goalValue * 0.45), (goalValue * 0.55), String.Format(" You're half way to your goal of {0}!", goalValue))
        .AxisY2.CustomLabels.Add((goalValue * 0.9), (goalValue * 0.99), String.Format(" You met the goal of {0}!!!", goalValue))

        ' X-axis
        .AxisX.MajorGrid.Enabled = False
        .AxisX.Maximum = 1
        .AxisX.Minimum = 0.8
        .AxisX.LabelStyle.Enabled = False
        .AxisX.MajorTickMark.Enabled = False
    End With

    ' Populate series data
    Dim pt1 = New DataPoint(1, currentValue)
    series1.Points.Add(pt1)

    With series1
        .ChartType = SeriesChartType.Column
        .Color = red
        .YAxisType = AxisType.Secondary
    End With

    Dim memStrm As New MemoryStream()
    chart1.SaveImage(memStrm, ChartImageFormat.Png)
    memStrm.Position = 0
    Return memStrm
End Function

现在,不是缩放图形以显示我的自定义标签的全文,而是图形变得更胖,文本被删除用elipsis截断。我已经搜索了如何从Dundas获得没有运气的行为。我尝试了各种设置,例如:

With defaultArea
   .AxisY2.IsLabelAutoFit = True
   .AxisY2.LabelAutoFitStyle = LabelAutoFitStyles.None
   .AxisY2.LabelStyle.TruncatedLabels = False
End With

这些不起作用。我似乎唯一能够开始工作的是我是这样做的:

With defaultArea
   .InnerPlotPosition.Height = 100 ' YUCK!  Why???  Seems brittle...
   .InnerPlotPosition.Width = 20 ' YUCK!  Why???  Seems brittle...
End With

这段代码的问题在于我更关心文本的大小而不是关于图形的大小,我希望文本总是显示出来。虽然这个设计示例中的文本是硬编码的,但在实际系统中它并不是那样我真的需要图形大小是动态的,以便自定义标签始终显示文本而不缩放字体大小。有关如何模仿旧Dundas组件的默认行为的任何建议吗?

1 个答案:

答案 0 :(得分:3)

我通过尝试找到与MS Chart Controls一起使用的 right 属性来了解您的意思。走对象层次结构可能很痛苦,但我认为您正在寻找的是以下代码:

Me.Chart1.ChartAreas("Default").AxisY2.LabelStyle.Font = New System.Drawing.Font("Arial", 6)