如何控制System.Web.UI.DataVisualization.Charting中自定义标签的宽度?
问题描述:
我有一些代码使用旧的Dundas制图组件(版本7)生成一个简单的温度计类图表。下面是代码:如何控制System.Web.UI.DataVisualization.Charting中自定义标签的宽度?
'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截断。我一直在寻找如何从登打士那里得到行为,但没有运气。我尝试了各种设置,例如:
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组件的默认行为的任何建议?
答
我知道你的意思,试图找到权利属性与MS图表控件一起使用。走在对象层次可以是一个痛苦,但我认为你正在寻找的是下面的代码:
Me.Chart1.ChartAreas("Default").AxisY2.LabelStyle.Font = New System.Drawing.Font("Arial", 6)
其实我已经得到了字体与该行设定 - `.AxisY2.LabelStyle.Font =新系统.Drawing.Font(“Tahoma”,10)`。麻烦的是,我不希望字体缩小以适应,我宁愿图形缩小。如果我让字体缩小,它会显示整个文本,但如果我只能缩小图形,那么在没有省略号的情况下,文本在10pt处有足够的空间。我知道这是因为Dundas版本正常工作。 – mattmc3 2011-01-30 02:47:55