简体   繁体   English

如何在ASP.NET图表控件中显示值

[英]How to display values in ASP.NET chart control

I'm using a dataset as my data source to create a chart in asp.net, iv managed to display a column chart. 我正在使用数据集作为我的数据源在asp.net中创建图表,iv设法显示柱形图。 Im now stuck on how I can display the values of each column on the chart. 我现在停留在如何显示图表上每列的值。

Can some one please advice on how I may do this? 有人可以就我如何做到这一点提出建议吗?

I am assuming you are using ASP.NET chart controls which come as standard with .net 4, the very basic chart you can get is by below code 我假设你使用的是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>

Now, if you want to access this programmatically, you might want to download the sample project given at http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx and go through the sample code.The project is quite extensive and describes in details about everything you need to know about charts. 现在,如果要以编程方式访问此项,您可能需要下载http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-中给出的示例项目。 lt-asp-chart-runat-quot-server-quot-gt.aspx并查看示例代码。该项目非常广泛,详细描述了您需要了解的有关图表的所有信息。 If you are stuck at a specific logic or piece of code, could you post that so we can advise further 如果你被困在一个特定的逻辑或代码片段,你可以发布,所以我们可以进一步建议

Thanks. 谢谢。

Strictly speaking.. 严格来讲..

From code behind:(my series is called overall) 从后面的代码:(我的系列被称为整体)

   series_overal_overall.Label = "#PERCENT{P0}"

This will show the values in percent 这将以百分比显示值


To show a little more consider this sample. 再展示一下这个样本。

在此输入图像描述

from this barred data, i'm creating a lot of graphs, to keep it simple, i"ll just show 2 从这个禁止的数据,我创建了很多图表,为了保持简单,我只会显示2

在此输入图像描述

(sorry, i'm using 3d pie charts but anything goes) (对不起,我正在使用三维饼图,但任何事情都有)

a) my aspx page.. 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>

I define 2 labels and tell to wich chartarea they should be 'docked'. 我定义了两个标签并告诉它们应该“停靠”。 I do just one legend as the rest will done from codebehind. 我只做了一个传奇,其余的将来自代码隐藏。 Finally, define the chartareas themselves. 最后,定义图表区域。

In codebehind, i'm calling my sub to create the carts and pass a reference to the Table as shown above so i can process that date i've calculated to that time. 在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

so far the preparation work is done. 到目前为止,准备工作已经完成。 Now we're going to put the Y values in. 现在我们将把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

I'm reading basically all the odd Columns for the IN and the even columns for the out values. 我基本上读取IN的所有奇数列和out值的偶数列。 the Last letter specifies wether it's a Laptop (L) or a Desktop (D) value. 最后一个字母指定它是笔记本电脑(L)或桌面(D)值。 Then sum up these read values as they contain the figures i want to show as a percentage of in warranty/out warranty. 然后总结这些读数值,因为它们包含我想要显示的保修/外保修的百分比数字。 (Please note that i'm only showing a portion of the page, the intermediate arrays are being used elsewhere) (请注意,我只显示页面的一部分,中间数组正在其他地方使用)

   '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 

For one of the charts, i'm hinding the legend as it's twice the same, i'll put the legend in the middel of the two charts later. 对于其中一个图表,我正在追踪它的两倍相同的传说,我将把这个传奇放在两个图表的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

and here i add my legend. 在这里我添加了我的传奇。

    '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

you need to play a bit with the values to position it correctly on your chartarea of course 您需要使用值来稍微正确地将其定位在您的图表区域上

If you want to see the values instead of a percentage, change your label for instance to. 如果要查看值而不是百分比,请将标签更改为。

 series_overal_laptop.Label = "#VALY"

hope this helps 希望这可以帮助

K ķ

Chart1.Series[0].IsValueShownAsLabel = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM