【问题标题】:How to control the width of custom labels in System.Web.UI.DataVisualization.Charting?如何控制 System.Web.UI.DataVisualization.Charting 中自定义标签的宽度?
【发布时间】:2011-01-20 16:25:31
【问题描述】:

我有一些代码可以使用旧的 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

现在,不是缩放图表以显示我的自定义标签的全文,而是图表变得更宽,文本被省略号截断。我一直在寻找如何从 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 组件的默认行为的任何建议?

【问题讨论】:

    标签: .net vb.net charts


    【解决方案1】:

    我知道您试图找到与 MS 图表控件一起使用的 right 属性是什么意思。遍历对象层次结构可能会很痛苦,但我认为您正在寻找的是以下代码:

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

    【讨论】:

    • 我实际上已经用这一行设置了字体 - .AxisY2.LabelStyle.Font = New System.Drawing.Font("Tahoma", 10)。麻烦的是,我不希望字体缩小以适应,我希望图形缩小。如果我让字体缩小,它确实会显示整个文本,但是如果我可以缩小图形,那么在没有省略号的情况下,10pt 的文本还有足够的空间。我知道这一点是因为 Dundas 版本可以正常工作。
    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 2018-12-16
    • 2014-09-11
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多