使用CODE BEHIND创建堆积条形图表

时间:2012-03-06 20:37:16

标签: c# code-behind stacked

我需要创建一个Stacked柱形图,如图所示。 X轴:组件名称 Y轴:错误数量 堆栈:对于每个组件,我需要创建堆叠图表,告诉我:     --->对于每个组件类别(音频/视频),我需要说我有x个HIGH,MEDIUM,LOW,SHOWSTOPPER错误。

我的数据格式为: 类别名称||   BUG TYPE:数字   BUG TYPE:数字   BUG TYPE:数字   BUG TYPE:数字

示例数据: AUDIO :: =>   高:30   中:17   低:1 VIDEO :: =>   高:1 外设:: =>   中:15   高:14   低:1   showstopper:1 SENSOR :: =>   中:2

上面显示的这些数据是使用一些LINQ查询创建的。所以它不在字典中。用于创建上述数据的代码:

var dbQuery = from bug in RawListData
              group bug by new { bug.category, bug.customer_priority } into grouped
              select new { 
                  Category = grouped.Key.category,
                  Priority = grouped.Key.customer_priority,
                  Count = grouped.Count()
              };

            var query = dbQuery.ToLookup(result => result.Category,
                                         result => new { result.Priority, result.Count });


            foreach (var result in query)
            {
                //Console.WriteLine("{0}: ", result.Key);
                System.Diagnostics.Debug.WriteLine(" : ", result.Key);
                foreach (var subresult in result)
                {
                    //Console.WriteLine("  {0}: {1}", subresult.Priority, subresult.Count);
                    System.Diagnostics.Debug.WriteLine("  {0}: {1}", subresult.Priority, subresult.Count);
                }
            }

请注意,某些类别没有针对每种错误类型的值。

我知道如何在XAML中执行此操作。我想在“代码背后”这样做。任何关于此的指示将不胜感激。我起诉创建图表的代码是简单的XAML:

<asp:Chart ID="Chart2" runat="server" ImageLocation="~/FolderLocation/Chart_#SEQ(1000,0)" ImageStorageMode="UseImageLocation" ImageType="Png" IsSoftShadows="true">

      <series>
        <asp:Series Name="Championships" YValueType="Int32" ChartType="StackedColumn" > 
         <Points> 
            <asp:DataPoint AxisLabel="Audio" YValues="17" /> 
            <asp:DataPoint AxisLabel="Video" YValues="15" /> 
            <asp:DataPoint AxisLabel="Peripheral" YValues="6" />            
         </Points> 
        </asp:Series> 
        <asp:Series Name="Championships2" YValueType="Int32" ChartType="StackedColumn" > 
         <Points> 
            <asp:DataPoint AxisLabel="Audio" YValues="2" /> 
            <asp:DataPoint AxisLabel="Video" YValues="5" /> 
            <asp:DataPoint AxisLabel="Peripheral" YValues="16" /> 
         </Points> 
        </asp:Series> 
        <asp:Series Name="Championships3" YValueType="Int32" ChartType="StackedColumn" > 
         <Points> 
            <asp:DataPoint AxisLabel="Audio" YValues="10" /> 
            <asp:DataPoint AxisLabel="Video" YValues="3" /> 
            <asp:DataPoint AxisLabel="Peripheral" YValues="16" /> 

         </Points> 
        </asp:Series> 
        <asp:Series Name="Championships4" YValueType="Int32" ChartType="StackedColumn" > 
         <Points> 
            <asp:DataPoint AxisLabel="Audio" YValues="10" /> 
            <asp:DataPoint AxisLabel="Video" YValues="3" /> 
            <asp:DataPoint AxisLabel="Peripheral" YValues="16" /> 
         </Points> 
        </asp:Series> 
      </series>
            <chartareas>
                <asp:ChartArea Name="ChartArea1">
                    <Area3DStyle Enable3D="True" />
                </asp:ChartArea>
            </chartareas>
        </asp:Chart>

我还想显示每个堆栈组件上的错误数量。

如何通过编写代码而不是在XAML中创建此堆积图表?

1 个答案:

答案 0 :(得分:6)

您可以使用Highcharts纯粹的javascript,它们更具互动性而非Microsoft图表。使用DotNet.Highcharts库也很容易在后面的代码中创建它们。以下是创建堆积柱形图的代码:

Highcharts chart = new Highcharts("chart")
            .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
            .SetTitle(new Title { Text = "Stacked column chart" })
            .SetXAxis(new XAxis { Categories = new[] { "Championships 1", "Championships 2", "Championships 3", "Championships 4" } })
            .SetPlotOptions(new PlotOptions { Column = new PlotOptionsColumn { Stacking = Stackings.Normal } })
            .SetSeries(new[]
                       {
                           new Series { Name = "Audio", Data = new Data(new object[] { 17, 2, 10, 10 }) },
                           new Series { Name = "Video", Data = new Data(new object[] { 15, 5, 3, 3 }) },
                           new Series { Name = "Peripheral", Data = new Data(new object[] { 6, 16, 16, 16 }) }
                       });

以下是代码的结果: This is the stacked column chart

您还可以看到Highcharts here的现场演示。

有关如何安装和使用home page at CodePlex上可以找到的DotNet.Highcharts库的更多信息。您可以在example project中找到更多示例代码。