2011-06-20 74 views
2

我正在使用數據集作爲我的數據源在asp.net中創建圖表,iv設法顯示一個列圖表。我現在堅持如何顯示圖表上每列的值。如何在ASP.NET圖表控件中顯示值

有人可以請教我如何做到這一點?

回答

4

我假設你正在使用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中給出的示例項目並查看示例代碼。該項目非常廣泛,並詳細介紹了有關圖表的所有知識。如果你被困在一個特定的邏輯或一段代碼,你可以張貼,所以我們可以進一步建議

謝謝。

3

嚴格地說..

從後面的代碼:(我的系列整體的稱呼)

series_overal_overall.Label = "#PERCENT{P0}" 

這將顯示百分比值


要顯示多一點考慮這個樣本。

enter image description here

從這個禁止的數據,我創建了大量的圖表,以保持它的簡單,我「會只顯示2

enter image description here

(抱歉,我使用3D餅圖,但任何事情都會發生)

一)我的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> 

我定義了2個標籤並告訴它們應該「停靠」。 我只做了一個傳說,因爲其餘部分將從代碼隱藏完成。 最後,自己定義chartareas。

在代碼隱藏中,我打電話給我的子創建購物車,並通過引用表如上所示,所以我可以處理那個日期,我已經計算到那個時間。

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 

我讀基本上全部爲奇數列和偶數列的超時值。 最後一個字母指定它是筆記本電腦(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 

對於圖表中的一個,我hinding圖例,因爲它是兩次相同的,我稍後將把這個傳說放在兩張圖表的中間。

' 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 

需要打一點與價值觀正確放置在你的課程

的chartarea如果你想看到的值,而不是百分比,更改標籤的實例。

series_overal_laptop.Label = "#VALY" 

希望這有助於

ķ

1
Chart1.Series[0].IsValueShownAsLabel = true; 
+0

我不知道其他人試圖做這似乎是做了明顯的方法是什麼。 – RustyH

相關問題