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

发布于 2024-11-16 07:26:10 字数 88 浏览 3 评论 0 原文

我使用数据集作为数据源在 asp.net 中创建图表,iv 设法显示柱形图。我现在卡在如何显示图表上每列的值上。

有人可以建议我如何做到这一点吗?

I'm using a dataset as my data source to create a chart in asp.net, iv managed to display a column chart. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2024-11-23 07:26:10

我假设您使用的是 .net 4 标准的 ASP.NET 图表控件,您可以通过下面的代码获得非常基本的图表

<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 并浏览示例代码。该项目非常广泛,详细描述了您需要了解的有关图表的所有内容。如果您遇到特定的逻辑或代码片段,您可以发布该内容,以便我们提供进一步的建议,

谢谢。

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: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. If you are stuck at a specific logic or piece of code, could you post that so we can advise further

Thanks.

岁月苍老的讽刺 2024-11-23 07:26:10

严格来说..

从代码后面:(我的系列称为整体)

   series_overal_overall.Label = "#PERCENT{P0}"

这将以百分比显示值


要显示更多,请考虑此示例。

在此处输入图像描述

从这个禁止的数据中,我创建了很多图表,为了简单起见,我“会只需显示 2

在此处输入图像描述

(抱歉,我正在使用 3d 饼图,但一切正常)

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>

我定义 2 个标签并告诉图表区域它们应该“停靠”。
我只做了一个图例,其余的将在代码隐藏中完成。
最后,定义图表区域本身。

在代码隐藏中,我调用我的子程序来创建购物车并传递对如上所示的表的引用,以便我可以处理我计算到的那个时间的日期。

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 的所有奇数列和输出值的偶数列。
最后一个字母指定它是笔记本电脑 (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 

对于其中一个图表,我隐藏了图例,因为它是相同的两倍,我将把图例放在中间稍后再看这两个图表。

   ' 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"

希望这对

K有帮助

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.

enter image description here

from this barred data, i'm creating a lot of graphs, to keep it simple, i"ll just show 2

enter image description here

(sorry, i'm using 3d pie charts but anything goes)

a) my aspx page..

            <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.

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.

' 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.
the Last letter specifies wether it's a Laptop (L) or a Desktop (D) value. 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.

   ' 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

傻比既视感 2024-11-23 07:26:10
Chart1.Series[0].IsValueShownAsLabel = true;
Chart1.Series[0].IsValueShownAsLabel = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文