如何显示按2个参数分组的asp.net图表

时间:2012-10-09 21:25:05

标签: asp.net visual-studio-2010 .net-4.0 charts

我想使用Chart组件向用户展示一些精简数据。

SQL(C#/ Oracle):

SELECT c.date, c.approved, count(distinct c.f1) amt_c, count(b.f1) amt_b, sum(b.value) sum_values
FROM contracts c
JOIN bens b ON c.ben_id = b.id
WHERE :YearMonth = to_char(c.date,'YYYYMM') AND NOT c.approved = 'REJECTED'
GROUP BY c.date, c.approved
ORDER BY c.date

我在一个将DataSet传递给.aspx页面中的ObjectDataSource的方法中有这个SQL(approved字段可以有3个值:REJECTED,APPROVED和PENDING)。

.aspx页面中的图表:

<asp:Chart ID="Chart1" runat="server" DataSourceID="RelatorioDataSource" 
    Width="700px" Compression="10" Palette="Chocolate">
    <Series>
        <asp:Series Name="Contracts" XValueMember="date" 
            YValueMembers="amt_c" IsXValueIndexed="False" 
            XValueType="DateTime" IsValueShownAsLabel="True" BorderDashStyle="DashDot" 
            CustomProperties="DrawingStyle=Emboss, EmptyPointValue=Zero, DrawSideBySide=True" 
            YValuesPerPoint="4">
        </asp:Series>
        <asp:Series BorderDashStyle="DashDot" ChartArea="ChartArea1" 
            CustomProperties="DrawingStyle=Emboss, EmptyPointValue=Zero, DrawSideBySide=True" 
            IsValueShownAsLabel="True" Name="Bens" 
            XValueMember="date" XValueType="DateTime" 
            YValueMembers="amt_b" YValuesPerPoint="4">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

我想显示每天的批准/待定合约/合约数量(4条),但图表仅显示两列。 Column chart

2 个答案:

答案 0 :(得分:0)

通过创建一个对象relatorio来保存返回的数据(而不是DataSet),使用LINQ to Objects过滤结果,并以编程方式在codeBehind中添加系列来解决。

var approved = relatorio
    .Where(r => r.APPROVED == "APPROVED")
    .ToList()
    ;
var pending = relatorio
    .Where(r => r.APPROVED == "PENDING")
    .ToList()
    ;

创建传奇

Legend legenda = new Legend("Legenda");
legenda.Docking = Docking.Bottom;
legenda.LegendStyle = LegendStyle.Row;
legenda.Alignment = System.Drawing.StringAlignment.Center;

在循环中创建系列

for (int i = 0; i < 4; i++) {
    Series temp = new Series {
        XAxisType = AxisType.Primary,
        XValueType = ChartValueType.DateTime,
        YAxisType = AxisType.Primary,
        //mostra só a quantidade de contratos
        IsValueShownAsLabel = i % 2 == 0 ? true : false,
        ChartType = SeriesChartType.Column,
        CustomProperties = "EmptyPointValue=Zero",
        Legend = "Legenda"
    };
    grafico.Series.Add(temp);
}
approvedValues.Points.DataBindXY(approved, "DATE", approved, "SUM_VALUES");

DataBinding系列

// approved CONTRACTS
grafico.Series[0].Points.DataBindXY(approved, "DATE", approved, "AMT_C");
grafico.Series[0].LegendText = "Contratos approved";
// approved BENS
grafico.Series[1].Points.DataBindXY(approved, "DATE", approved, "AMT_B");
grafico.Series[1].LegendText = "Ben approved";
grafico.Series[1].ChartType = SeriesChartType.Line;
// pending CONTRACTS
grafico.Series[2].Points.DataBindXY(pending, "DATE", pending, "AMT_C");
grafico.Series[2].LegendText = "Contratos pending";
// pending BENS
grafico.Series[3].Points.DataBindXY(pending, "DATE", pending, "AMT_B");
grafico.Series[3].LegendText = "Ben pending";
grafico.Series[3].ChartType = SeriesChartType.Line;

答案 1 :(得分:-1)

为每个系列创建一个组..

例如: chart1.series[0]["StackedGroupName"] = "group1";