Dim Cmd As new SQLCommand
Cmd.C
Cmd.CommandText = "select distinct 分区,年,月份,目标 from 目标 where " & CustomFilter
Dim dt1 As DataTable = Cmd.ExecuteReader
Dim dtb As New DataTableBuilder("统计")
dtb.AddDef("分区", Gettype(String), 50)
Dim prds As List(of String()) = dt1.GetUniqueValues("","年","月份")
Dim exp As String
Dim prd As String()
dtb.AddDef("目标总额", Gettype(Double))
dtb.AddDef("销售总额", Gettype(Double))
dtb.AddDef("达成率", Gettype(Double),"[销售总额]/[目标总额]")
For Each prd In prds
dtb.AddDef(prd(0) & "_" & prd(1) & "_目标", Gettype(Double))
dtb.AddDef(prd(0) & "_" & prd(1) & "_销售", Gettype(Double))
exp = String.format("[{0}]/[{1}]",prd(0) & "_" & prd(1) & "_销售" ,prd(0) & "_" & prd(1) & "_目标" )
dtb.AddDef(prd(0) & "_" & prd(1) & "_达成率", Gettype(Double),exp)
Next
dtb.Build()
Cmd.CommandText = "select 分区,year(日期) as 年,month(日期) as 月份,sum(金额) as 销售 from 订单 where " & CustomFilter & " group by 分区,year(日期),month(日期)"
Dim dt2 As DataTable = Cmd.ExecuteReader
Dim dr As DataRow
For Each cus As String In dt1.GetUniqueValues("","分区")
dr = DataTables("统计").AddNew()
dr("分区") = cus
For Each prd In prds
dr(prd(0) & "_" & prd(1) & "_目标") = dt1.Compute("Sum(目标)","分区 = \'" & cus & "\' And [年] = \'" & prd(0) & "\' and 月份 = \'"& prd(1) &"\'")
dr(prd(0) & "_" & prd(1) & "_销售") = dt2.Compute("Sum(销售)","分区 = \'" & cus & "\' And [年] = \'" & prd(0) & "\' and 月份 = \'"& prd(1) &"\'")
Next
dr("目标总额") = dt1.Compute("Sum(目标)","分区 = \'" & cus & "\'")
dr("销售总额") = dt2.Compute("Sum(销售)","分区 = \'" & cus & "\'")
Next
\'********************************
Dim nr As Row = Tables("统计").AddNew
nr(0) = "合计"
For Each c As Col In Tables("统计").Cols
If c.IsNumeric AndAlso c.DataCol.Expression = "" Then
nr(c.name) = Tables("统计").Compute("sum([" & c.Name & "])", Tables("统计").Cols(0).Name & " <> \'合计\'")
MainTable = Tables("统计")
For Each prd In prds
DataTables("统计").DataCols(prd(0) & "_" & prd(1) & "_达成率").SetFormat("0.00%")
Next
DataTables("统计").DataCols("达成率").SetFormat("0.00%")
End If
Next
Dim Chart As WinForm.Chart \'定义一个图表变量
Dim Series As WinForm.ChartSeries \'定义一个图系变量
Dim t As Table = Tables("统计") \'定义一个变量t引用数据表
Chart= e.Form.Controls("Chart1") \' 引用窗口中的图表
Chart.ChartType = ChartTypeEnum.Bar \'图表类型改为Bar(条形)
Chart.SeriesList.Clear() \'清除图表原来的图系
Series = Chart.SeriesList.Add() \'增加一个图系
Series.Length = (t.Cols.Count-3) / 3 \'图系的数据点数等于表的行数
Dim Series1 As WinForm.ChartSeries = Chart.SeriesList.Add()
Series1.Length = (t.Cols.Count-3) / 3 \'图系的数据点数等于表的行数
Dim Series2 As WinForm.ChartSeries = Chart.SeriesList2.Add() \'给图表2增加一个图系
Series2.Length = (t.Cols.Count-3) / 3 \'图系的数据点数等于表的行数
Chart.ChartType2 = ChartTypeEnum.XYPlot
Series.Text = "目标"
Series1.Text = "销售"
Series2.Text = "达成率"
Dim i As Integer = 0
For Each c As Col In Tables("统计").Cols
If c.Name.EndsWith("目标") Then
Series.X(i) = i \'指定水平坐标
Series1.X(i) = i \'指定水平坐标
Series2.X(i) = i \'指定水平坐标
Series.Y(i) = nr(c.Name) \'指定垂直坐标
Series1.Y(i) = nr(Tables("统计").Cols(c.Index+1).Name) \'指定垂直坐标
Series2.Y(i) = nr(Tables("统计").Cols(c.Index+2).Name) \'指定垂直坐标
Series2.DataLabelText =nr(Tables("统计").Cols(c.Index+2).Name) & "%"
Chart.AxisX.SetValueLabel(i, c.Name.Split("_")(1)) \'指定字符表示
i+=1
End If
Next
Chart.AxisX.AnnoWithLabels = True \'启用字符标示
Chart.LegendVisible = True \'显示图列
Chart.LegendCompass= CompassEnum.South \'图列显示在南方(底端)
Chart.VisualEffect = True \'加上这一行,让你的图表更漂亮
Chart.AxisY.Text = "金额"
Chart.AxisY2.Text = "比例"
Series.DataLabelText = "{#YVAL}"
Series1.DataLabelText = "{#YVAL}"
Series1.DataLabelBackColor = Color.Blue \'蓝底
Series1.DataLabelForeColor = Color.White \'白字
Series.DataLabelBackColor = Color.red \'红底
Series.DataLabelForeColor = Color.White \'白字
Series2.DataLabelBackColor = Color.green \'绿底
Series2.DataLabelForeColor = Color.White \'白字