2012-10-09 41 views
0

我想使用Chart組件向用戶展示一些精簡數據。如何顯示按2參數分組的asp.net圖表

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 

我有這樣的SQL在於通過在.aspx頁數據集到ObjectDataSource的方法(該approved字段可以有3個值:REJECTED,批准和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條)批准/待定合同/ BENS的數字,但圖表顯示只有兩列。 Column chart

回答

0

通過創建一個對象relatorio保存返回的數據(而不是數據集),使用LINQ到對象的過濾的結果,並在代碼隱藏編程添加系列解決。

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"); 

數據綁定系列

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

每個系列創建一個組..

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