![](/img/trans.png)
[英]Asp.net chart control IsValueShownAsLabel and label will display values with $ symbol
[英]How to display values in ASP.NET chart control
我正在使用數據集作為我的數據源在asp.net中創建圖表,iv設法顯示柱形圖。 我現在停留在如何顯示圖表上每列的值。
有人可以就我如何做到這一點提出建議嗎?
我假設你使用的是ASP.NET圖表控件,它是.net 4的標准配置,你可以得到的基本圖表是下面的代碼
<asp:Chart ID="Chart1" runat="server">
<Series>
<asp:Series Name="Series1" ChartType="Pie" Palette="EarthTones" >
<Points>
<asp:DataPoint AxisLabel="Celtics" YValues="17" />
<asp:DataPoint AxisLabel="Lakers" YValues="15" />
<asp:DataPoint AxisLabel="Bulls" YValues="6" />
<asp:DataPoint AxisLabel="Spurs" YValues="4" />
<asp:DataPoint AxisLabel="76ers" YValues="3" />
<asp:DataPoint AxisLabel="Pistons" YValues="3" />
<asp:DataPoint AxisLabel="Warriors" YValues="3" />
</Points>
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" Area3DStyle-Enable3D="true" />
</ChartAreas>
</asp:Chart>
現在,如果要以編程方式訪問此項,您可能需要下載http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-中給出的示例項目。 lt-asp-chart-runat-quot-server-quot-gt.aspx並查看示例代碼。該項目非常廣泛,詳細描述了您需要了解的有關圖表的所有信息。 如果你被困在一個特定的邏輯或代碼片段,你可以發布,所以我們可以進一步建議
謝謝。
嚴格來講..
從后面的代碼:(我的系列被稱為整體)
series_overal_overall.Label = "#PERCENT{P0}"
這將以百分比顯示值
再展示一下這個樣本。
從這個禁止的數據,我創建了很多圖表,為了保持簡單,我只會顯示2
(對不起,我正在使用三維餅圖,但任何事情都有)
a)我的aspx頁面..
<asp:Chart ID="chartOverall" runat="server" Height="200px" Width="1000 px">
<BorderSkin SkinStyle="Emboss" />
<Titles>
<asp:Title Text="Laptop" TextStyle="Shadow" Font="Trebuchet MS, 14pt, style=Bold" IsDockedInsideChartArea="false" DockedToChartArea="laptop" ></asp:Title>
<asp:Title Text="Desktop" TextStyle="Shadow" Font="Trebuchet MS, 14pt, style=Bold" IsDockedInsideChartArea="false" DockedToChartArea="desktop" ></asp:Title>
</Titles>
<Legends>
</Legends>
<ChartAreas>
<asp:ChartArea Name="laptop" Area3DStyle-Enable3D="true" > <Position Y="15" Height="65" Width="22" X="1"></Position></asp:ChartArea>
<asp:ChartArea Name="desktop" Area3DStyle-Enable3D="true" > <Position Y="15" Height="65" Width="22" X="34"></Position></asp:ChartArea>
</ChartAreas>
</asp:Chart>
我定義了兩個標簽並告訴它們應該“停靠”。 我只做了一個傳奇,其余的將來自代碼隱藏。 最后,定義圖表區域。
在codebehind中,我正在調用我的sub來創建購物車並傳遞對Table的引用,如上所示,這樣我就可以處理我計算到那個時間的日期。
Protected Sub createchart(ByRef t As Table)
'create series
Dim series_overal_laptop As New Series("Overalll")
Dim series_overal_desktop As New Series("Overalld")
'create arrays
Dim yvalueslaptop(1) As Integer
Dim yvaluesdesktop(1) As Integer
Dim Xvalues(2) As String
Dim Xvaluesio(1) As String
' fill X values
For i = 1 To 2 ' in/out label.
Xvaluesio(i - 1) = t.Rows(2).Cells(i).Text
Next
到目前為止,准備工作已經完成。 現在我們將把Y值放入。
' fill y values
For i = 1 To 5 Step 2
'laptops IN
YValuesINL(((i + 1) / 2) - 1) = t.Rows(3).Cells(i).Text
'Desktops IN
YValuesIND(((i + 1) / 2) - 1) = t.Rows(4).Cells(i).Text
Next
For i = 2 To 6 Step 2
'laptops out
YValuesOUTL(((i) / 2) - 1) = t.Rows(3).Cells(i).Text
'desktop out
YValuesOUTD(((i) / 2) - 1) = t.Rows(4).Cells(i).Text
Next
我基本上讀取IN的所有奇數列和out值的偶數列。 最后一個字母指定它是筆記本電腦(L)或桌面(D)值。 然后總結這些讀數值,因為它們包含我想要顯示的保修/外保修的百分比數字。 (請注意,我只顯示頁面的一部分,中間數組正在其他地方使用)
'overall laptops and desktops
'reuse the values i've collected already
yvalueslaptop(0) = YValuesINL.Sum
yvalueslaptop(1) = YValuesOUTL.Sum
yvaluesdesktop(0) = YValuesIND.Sum
yvaluesdesktop(1) = YValuesOUTD.Sum
'now name and place the series, specfiy appearance and point values
'#First Section
series_overal_laptop.Name = "laptop"
series_overal_laptop.ChartArea = "laptop"
series_overal_laptop.ChartType = SeriesChartType.Pie
series_overal_laptop.Label = "#PERCENT{P0}"
series_overal_laptop.IsVisibleInLegend = False
series_overal_desktop.Name = "desktop"
series_overal_desktop.ChartArea = "desktop"
series_overal_desktop.ChartType = SeriesChartType.Pie
series_overal_desktop.Label = "#PERCENT{P0}"
series_overal_desktop.IsVisibleInLegend = True
series_overal_desktop.LegendText = "#AXISLABEL"
'#End of First Section
對於其中一個圖表,我正在追蹤它的兩倍相同的傳說,我將把這個傳奇放在兩個圖表的middel之后。
' now bind the datapoints to the series
series_overal_laptop.Points.DataBindXY(Xvaluesio, yvalueslaptop)
series_overal_desktop.Points.DataBindXY(Xvaluesio, yvaluesdesktop)
'finally add the series to the charts
chartOverall.Series.Dispose() ' just to be sure nothing is left behind
chartoverall.series.add(series_overal_laptop)
chartOverall.Series.Add(series_overal_desktop)
chartOverall.Series("laptop").Palette = ChartColorPalette.Excel
chartOverall.Series("desktop").Palette = ChartColorPalette.Excel
在這里我添加了我的傳奇。
'only 1 legend per chart is fine as they all have the same colors
Dim topviewlegend As New Legend("topviewlegend")
chartOverall.Legends.Add(topviewlegend)
chartOverall.Series("desktop").Legend = "topviewlegend"
topviewlegend.IsDockedInsideChartArea = False
topviewlegend.Docking = 0
topviewlegend.Position.Auto = False
topviewlegend.Position.X = 20
topviewlegend.Position.Y = 13
topviewlegend.Position.Width = 20
topviewlegend.Position.Height = 10
您需要使用值來稍微正確地將其定位在您的圖表區域上
如果要查看值而不是百分比,請將標簽更改為。
series_overal_laptop.Label = "#VALY"
希望這可以幫助
ķ
Chart1.Series[0].IsValueShownAsLabel = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.