コード例 #1
0
ファイル: BarChart.cs プロジェクト: trigrass2/myreport
        /// <summary>
        /// 采用分组方式配置数据源
        /// </summary>
        /// <param name="Aim">数据源</param>
        private void SetGroupData(DataTable Aim)
        {
            if (this.Apperence != null && this.Apperence.SeriesList != null && this.Apperence.SeriesList.Count > 0)
            {
                //是否绑定数据源,绑定的数据源是否存在
                if (string.IsNullOrEmpty(GroupSource.MajorBinding))
                {
                    PMS.Libraries.ToolControls.PMSPublicInfo.Message.Error("未绑定分组-主分类字段!");
                    return;
                }
                if (string.IsNullOrEmpty(GroupSource.ValueBinding))
                {
                    PMS.Libraries.ToolControls.PMSPublicInfo.Message.Error("未绑定分组-统计字段!");
                    return;
                }
            }
            ArrayList columnList = new ArrayList();

            if (!Aim.Columns.Contains(GroupSource.MajorBinding))
            {
                if (GroupSource.MajorBinding.EndsWith("_Year"))
                {
                    CreateSplitDateFiled(Aim, GroupSource.MajorBinding, columnList, "Year");
                }
                else if (GroupSource.MajorBinding.EndsWith("Month"))
                {
                    CreateSplitDateFiled(Aim, GroupSource.MajorBinding, columnList, "Month");
                }
                else if (GroupSource.MajorBinding.EndsWith("Day"))
                {
                    CreateSplitDateFiled(Aim, GroupSource.MajorBinding, columnList, "Day");
                }
                else if (GroupSource.MajorBinding.EndsWith("Hour"))
                {
                    CreateSplitDateFiled(Aim, GroupSource.MajorBinding, columnList, "Hour");
                }
                else if (GroupSource.MajorBinding.EndsWith("Minute"))
                {
                    CreateSplitDateFiled(Aim, GroupSource.MajorBinding, columnList, "Minute");
                }
                else if (GroupSource.MajorBinding.EndsWith("Second"))
                {
                    CreateSplitDateFiled(Aim, GroupSource.MajorBinding, columnList, "Second");
                }
                else
                {
                    return;
                }
            }

            string strValueFx;

            switch (GroupSource.ValueFx)
            {
            case Functions.Count:
                strValueFx = "count";
                break;

            case Functions.Sum:
                strValueFx = "sum";
                break;

            case Functions.Avg:
                strValueFx = "avg";
                break;

            case Functions.Max:
                strValueFx = "max";
                break;

            case Functions.Min:
                strValueFx = "min";
                break;

            default:
                strValueFx = "";
                break;
            }

            DataSetHelper    ds      = new DataSetHelper();
            List <DataTable> dtList  = new List <DataTable>();
            DataTable        tbMajor = Aim.DefaultView.ToTable(true, GroupSource.MajorBinding);

            if (GroupSource.MajorSort == SortType.Asc)
            {
                tbMajor.DefaultView.Sort = GroupSource.MajorBinding + " Asc";
                tbMajor = tbMajor.DefaultView.ToTable();
            }
            if (GroupSource.MajorSort == SortType.Desc)
            {
                tbMajor.DefaultView.Sort = GroupSource.MajorBinding + " Desc";
                tbMajor = tbMajor.DefaultView.ToTable();
            }
            if (!string.IsNullOrEmpty(GroupSource.MinorBinding))
            {
                if (!Aim.Columns.Contains(GroupSource.MinorBinding))
                {
                    if (GroupSource.MinorBinding.EndsWith("_Year"))
                    {
                        CreateSplitDateFiled(Aim, GroupSource.MinorBinding, columnList, "Year");
                    }
                    else if (GroupSource.MinorBinding.EndsWith("Month"))
                    {
                        CreateSplitDateFiled(Aim, GroupSource.MinorBinding, columnList, "Month");
                    }
                    else if (GroupSource.MinorBinding.EndsWith("Day"))
                    {
                        CreateSplitDateFiled(Aim, GroupSource.MinorBinding, columnList, "Day");
                    }
                    else if (GroupSource.MinorBinding.EndsWith("Hour"))
                    {
                        CreateSplitDateFiled(Aim, GroupSource.MinorBinding, columnList, "Hour");
                    }
                    else if (GroupSource.MinorBinding.EndsWith("Minute"))
                    {
                        CreateSplitDateFiled(Aim, GroupSource.MinorBinding, columnList, "Minute");
                    }
                    else if (GroupSource.MinorBinding.EndsWith("Second"))
                    {
                        CreateSplitDateFiled(Aim, GroupSource.MinorBinding, columnList, "Second");
                    }
                    else
                    {
                        return;
                    }
                }

                DataTable tbMinor = Aim.DefaultView.ToTable(true, GroupSource.MinorBinding);
                if (GroupSource.MinorSort == SortType.Asc)
                {
                    tbMinor.DefaultView.Sort = GroupSource.MinorBinding + " Asc";
                    tbMinor = tbMinor.DefaultView.ToTable();
                }
                if (GroupSource.MinorSort == SortType.Desc)
                {
                    tbMinor.DefaultView.Sort = GroupSource.MinorBinding + " Desc";
                    tbMinor = tbMinor.DefaultView.ToTable();
                }

                foreach (DataRow row in tbMinor.Rows)
                {
                    if (row[0] != DBNull.Value)
                    {
                        dtList.Add(ds.SelectGroupByInto(row[0].ToString(), Aim, GroupSource.MajorBinding + "," + strValueFx + "(" + GroupSource.ValueBinding + ")", GroupSource.MinorBinding + "='" + row[0].ToString() + "'", GroupSource.MajorBinding));
                    }
                    else
                    {
                        dtList.Add(ds.SelectGroupByInto(row[0].ToString(), Aim, GroupSource.MajorBinding + "," + strValueFx + "(" + GroupSource.ValueBinding + ")", GroupSource.MinorBinding + " is null ", GroupSource.MajorBinding));
                    }
                }
            }
            else
            {
                dtList.Add(ds.SelectGroupByInto(Aim.TableName, Aim, GroupSource.MajorBinding + "," + strValueFx + "(" + GroupSource.ValueBinding + ")", "1=1", GroupSource.MajorBinding));
            }


            OriginPosition = new Point(this.Location.X, this.Location.Y);
            if (this.DesignMode == true)
            {
                DrawVirtualChart();
            }
            else if (Aim == null || Aim.Rows.Count == 0)
            {
                DrawVirtualChart();
            }
            else
            {
                try
                {
                    ChartArea ChartArea1 = new ChartArea("ChartArea1");
                    Legend    legend1    = new Legend();

                    InitChart();
                    //绑定绘图区
                    if (this.Apperence.ChartAreaList.Count != 0)
                    {
                        this.Apperence.ChartAreaList[0].SetChartArea(ChartArea1);
                    }
                    //绑定标题
                    if (this.Apperence.TitleList.Count != 0)
                    {
                        foreach (PMSTitle item in this.Apperence.TitleList)
                        {
                            Title title1 = new Title();
                            item.SetTitle(title1);
                            chart1.Titles.Add(title1);
                        }
                    }
                    //绑定图例
                    if (this.Apperence.LegendList.Count != 0)
                    {
                        this.Apperence.LegendList[0].SetLegend(legend1);
                    }
                    //绑定数据
                    if (this.Apperence.SeriesList.Count != 0)
                    {
                        foreach (DataTable dt in dtList)
                        {
                            Series series1 = new Series(dt.TableName);
                            this.Apperence.SeriesList[0].SetSeriseStyle(series1);
                            List <string> Xvalues = new List <string>();
                            List <double> Yvalues = new List <double>();
                            for (int i = 0; i < tbMajor.Rows.Count; i++)
                            {
                                Xvalues.Add(tbMajor.Rows[i][0].ToString());
                                Yvalues.Add(0);
                                for (int j = 0; j < dt.Rows.Count; j++)
                                {
                                    if (tbMajor.Rows[i][0].ToString() == dt.Rows[j][0].ToString())
                                    {
                                        Yvalues[i] = Convert.ToDouble(dt.Rows[j][1]);
                                    }
                                }
                            }

                            if (Xvalues.Count != 0 && Yvalues.Count != 0)
                            {
                                series1.Points.DataBindXY(Xvalues, Yvalues);
                            }
                            series1.ChartType = (SeriesChartType)ChartType;
                            chart1.Series.Add(series1);
                        }
                    }

                    chart1.ChartAreas.Add(ChartArea1);
                    chart1.Legends.Add(legend1);
                    if (this.Apperence.PMSChartAppearance != null)
                    {
                        chart1.BorderlineColor     = this.Apperence.PMSChartAppearance.BorderlineColor;
                        chart1.BorderlineDashStyle = this.Apperence.PMSChartAppearance.BorderlineDashStyle;
                        chart1.BorderlineWidth     = this.Apperence.PMSChartAppearance.BorderlineWidth;
                        chart1.BackColor           = this.Apperence.PMSChartAppearance.BackColor;
                        chart1.BackSecondaryColor  = this.Apperence.PMSChartAppearance.BackSecondaryColor;
                        chart1.BackHatchStyle      = this.Apperence.PMSChartAppearance.BackHatchStyle;
                        chart1.BackGradientStyle   = this.Apperence.PMSChartAppearance.BackGradientStyle;
                    }
                }
                catch (System.Exception ex)
                {
                    InitChart();
                    PMS.Libraries.ToolControls.PMSPublicInfo.Message.Error(ex.Message);
                }
            }
            foreach (var item in columnList)
            {
                Aim.Columns.Remove(item.ToString());
            }
        }