2016-10-04 110 views
1

我試圖用SQL表提供的數據構建餅圖。我的查詢結果如下:從SQL創建餅圖表

Total A B C  D  E  F 
11005 0 1485 3667 2665 2579 609 

問題是,我有許多'系列'只包含一個數據爲每個。我認爲我必須建立一個「系列」才能展示餅圖。 我的代碼是:

Dim RequeteByTpe As String = "SELECT COUNT(*) as Total, 
Sum(CASE 
    WHEN [Type] = 'A' THEN 1 
    ELSE 0 
    END) A, 
    Sum(CASE 
    WHEN [Type] = 'B' THEN 1 
    ELSE 0 
    END) B, 
    Sum(CASE 
    WHEN [Type] = 'C' THEN 1 
    ELSE 0 
    END) C, 
    Sum(CASE 
    WHEN [Type] = 'D' THEN 1 
    ELSE 0 
    END) D, 
Sum(CASE 
    WHEN [Constructeur] = 'E' THEN 1 
    ELSE 0 
    END) E, 
Sum(CASE 
    WHEN [Constructeur] = 'F' THEN 1 
    ELSE 0 
    END) F   
From DBase.dbo.MyTable" 

    Dim DA As New SqlDataAdapter(RequeteByType, ConnexionMyBase) 
    Dim DS As New DataSet() 
    DAConstC.Fill(DS, "ByType") 

    Dim ChartType As New Chart 

    Dim ChartArea2 As New ChartArea() 

    ChartConstC.ChartAreas.Add("ChartArea2") 

    ChartConstC.ChartAreas(0).Area3DStyle.Enable3D = True 

    Dim Series21 As Series = New Series("A") 
    Dim Series22 As Series = New Series("B") 
    Dim Series23 As Series = New Series("C") 
    Dim Series24 As Series = New Series("D") 
    Dim Series25 As Series = New Series("E") 
    Dim Series26 As Series = New Series("F") 

    Series21.ChartArea = "ChartArea2" 
    Series22.ChartArea = "ChartArea2" 
    Series23.ChartArea = "ChartArea2" 
    Series24.ChartArea = "ChartArea2" 
    Series25.ChartArea = "ChartArea2" 
    Series26.ChartArea = "ChartArea2" 

    ChartByType.DataSource = DS.Tables("ByType") 

    ChartByType.Series.Add(Series21) 
    ChartByType.Series.Add(Series22) 
    ChartByType.Series.Add(Series23) 
    ChartByType.Series.Add(Series24) 
    ChartByType.Series.Add(Series25) 
    ChartByType.Series.Add(Series26) 

    ChartByType.Series(0).YValueMembers = "A" 
    ChartByType.Series(1).YValueMembers = "B" 
    ChartByType.Series(2).YValueMembers = "C" 
    ChartByType.Series(3).YValueMembers = "D" 
    ChartByType.Series(4).YValueMembers = "E" 
    ChartByType.Series(4).YValueMembers = "F" 

    ChartByType.Titles.Add("Type proportions") 
    ChartByType.Titles(0).Font = New Font("Arial", 12, FontStyle.Regular) 

    ChartByType.Titles(0).Alignment = System.Drawing.ContentAlignment.TopCenter 

    ChartByType.Location = New System.Drawing.Point(15, 45) 

    ChartByType.Size = New System.Drawing.Size(760, 260) 

    Me.Controls.Add(ChartByType) 

我不知道,如果我必須改變我的SQL查詢來獲取的其他類型的數據或有一些數據歸屬方法或其他解決這一問題的可能性。任何人都可以幫忙嗎?

+0

你得到了什麼結果,你想得到什麼結果呢? –

+0

@TabAlleman,目前我只能顯示一個條形圖表,並且想顯示A,B,C,D,E,F值比例的餅圖。 – Elhendriks

回答

1

,而不是列,則需要行...

而不是把A,B,C在自己的專欄,
按行

例如SQL總各...

select [Type], count(*) as Total From DBase.dbo.MyTable group by [Type] 

我注意到有兩列在case聲明,
如果需要的話,你可以union在一起的結果...

select [Type] as Label, count(*) as Total From DBase.dbo.MyTable group by [Type] 
union all 
select [Constructeur] as Label, count(*) as Total From DBase.dbo.MyTable group by [Constructeur]