从SQL表创建饼图

时间:2016-10-04 14:34:05

标签: sql-server vb.net charts

我正在尝试使用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查询以获取其他类型的数据,或者是否有可能使用某些数据归因方法修复此问题。有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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]