我有一些代碼使用舊的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組件的默認行爲的任何建議?
其實我已經得到了字體與該行設定 - `.AxisY2.LabelStyle.Font =新系統.Drawing.Font(「Tahoma」,10)`。麻煩的是,我不希望字體縮小以適應,我寧願圖形縮小。如果我讓字體縮小,它會顯示整個文本,但如果我只能縮小圖形,那麼在沒有省略號的情況下,文本在10pt處有足夠的空間。我知道這是因爲Dundas版本正常工作。 – mattmc3 2011-01-30 02:47:55