Ejemplo n.º 1
0
        private List <ChartSeriesInfo> createExtendChart(DataTable table, ChartMappingInfo mapping, string viewType)
        {
            List <ChartSeriesInfo> chartSeries = new List <ChartSeriesInfo>();

            foreach (DataRow row in table.Rows)
            {
                string xValue     = row[mapping.XColumn].ToString();
                string seriesName = row[mapping.SeriesColumn].ToString();

                decimal yValue = 0;

                if (row[mapping.YColumn] != null)
                {
                    yValue = decimal.Parse(row[mapping.YColumn].ToString());
                }

                var chart = chartSeries.Find(c => c.Name == seriesName);

                if (chart == null)
                {
                    chart = new ChartSeriesInfo(seriesName, viewType);
                    chartSeries.Add(chart);
                }

                chart.SeriesPoints.Add(new ChartSeriesPointInfo(xValue, yValue));
            }

            return(chartSeries);
        }
Ejemplo n.º 2
0
        public void RefreshBindingData(DataSet ds)
        {
            try
            {
                //参数校验
                if (string.IsNullOrEmpty(XColumn))
                {
                    throw new MB.Util.APPException("XColumn未设置,初始化控件失败!", Util.APPMessageType.DisplayToUser);
                }
                if (string.IsNullOrEmpty(YColumn))
                {
                    throw new MB.Util.APPException("YColumn未设置,初始化控件失败!", Util.APPMessageType.DisplayToUser);
                }
                if (string.IsNullOrEmpty(SeriesColumn))
                {
                    throw new MB.Util.APPException("SeriesColumn未设置,初始化控件失败!", Util.APPMessageType.DisplayToUser);
                }
                if (string.IsNullOrEmpty(ArgumentType))
                {
                    throw new MB.Util.APPException("ArgumentType未设置,初始化控件失败!", Util.APPMessageType.DisplayToUser);
                }
                if (string.IsNullOrEmpty(ViewType))
                {
                    throw new MB.Util.APPException("ViewType未设置,初始化控件失败!", Util.APPMessageType.DisplayToUser);
                }

                ChartMapping = new ChartMappingInfo(_XColumn, _YColumn, _SeriesColumn, _ArgumentType);

                chartControl.Titles.Clear();
                chartControl.Titles.Add(new ChartTitle()
                {
                    Text = Title, Font = new Font("宋体", 24, System.Drawing.FontStyle.Bold)
                });
                chartControl.Legend.Visible = true;

                chartControl.Series.Clear();
                chartControl.DataSource = ds;

                if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
                {
                    return;
                }

                ChartMappingHelper     helper = new ChartMappingHelper(null);
                List <ChartSeriesInfo> series = helper.ConvertToChartSeries(ds.Tables[0], _ChartMapping, _ViewType);
                if (series == null || series.Count == 0)
                {
                    throw new MB.Util.APPException("ChartSeries转换失败,请检查数据源与配置的Mapping项是否匹配!", Util.APPMessageType.DisplayToUser);
                }

                DxChartControlHelper.Instance.BindingDxChartControl(chartControl, series);
            }
            catch (Exception ex)
            {
                MB.WinBase.ApplicationExceptionTerminate.DefaultInstance.ExceptionTerminate(ex);
            }
        }
Ejemplo n.º 3
0
        private List <ChartSeriesInfo> createSumChart(DataTable table, ChartMappingInfo mapping, string viewType)
        {
            List <ChartSeriesInfo> chartSeries = new List <ChartSeriesInfo>();
            IEnumerable <IGrouping <string, DataRow> > result = table.Rows.Cast <DataRow>().GroupBy <DataRow, string>(dr => dr[mapping.SeriesColumn].ToString());

            foreach (IGrouping <string, DataRow> series in result)
            {
                var seriesInfo = new ChartSeriesInfo(series.Key, viewType.ToString());
                var xValues    = series.Cast <DataRow>().GroupBy <DataRow, string>(dr => dr[mapping.XColumn].ToString());
                foreach (IGrouping <string, DataRow> points in xValues)
                {
                    seriesInfo.SeriesPoints.Add(new ChartSeriesPointInfo(points.Key, points.Sum <DataRow>(o => decimal.Parse(o[mapping.YColumn].ToString()))));
                }
                chartSeries.Add(seriesInfo);
            }
            return(chartSeries);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 根据Datatable转换成ChartSeriesInfo数组,用于在图表中显示
        /// </summary>
        /// <param name="table">数据源</param>
        /// <param name="mapping">DataTable与Chart的映射信息</param>
        /// <param name="viewType">图表类型</param>
        /// <returns></returns>
        public List <ChartSeriesInfo> ConvertToChartSeries(DataTable table, ChartMappingInfo mapping, string viewType)
        {
            if (!table.Columns.Contains(mapping.XColumn))
            {
                throw new ArgumentException(string.Format("X轴字段名{0}在数据源中不存在", mapping.XColumn));
            }

            if (!table.Columns.Contains(mapping.YColumn))
            {
                throw new ArgumentException(string.Format("Y轴字段名{0}在数据源中不存在", mapping.YColumn));
            }

            if (!table.Columns.Contains(mapping.SeriesColumn))
            {
                throw new ArgumentException(string.Format("系列字段名{0}在数据源中不存在", mapping.SeriesColumn));
            }

            List <ChartSeriesInfo> chartSeries = new List <ChartSeriesInfo>();
            ChartArgumentType      type        = (ChartArgumentType)Enum.Parse(typeof(ChartArgumentType), mapping.ArgumentType);
            DataColumn             dc          = table.Columns[mapping.YColumn];
            Type yType = dc.DataType;

            //MB.WinBase.MessageBoxEx.Show(yType.FullName);
            if (type != ChartArgumentType.Extend)
            {
                //判断传入的YColumn是否为数值类型
                if (!yType.FullName.Equals("System.Decimal") &&
                    !yType.FullName.Equals("System.Double") &&
                    !yType.FullName.Equals("System.Int32"))
                {
                    throw new MB.Util.APPException("配置的参数YColumn类型不正确,请配置数值类型!", APPMessageType.DisplayToUser);
                }
            }
            if (type == ChartArgumentType.Extend)
            {
                chartSeries = createExtendChart(table, mapping, viewType);
            }
            else if (type == ChartArgumentType.Sum)
            {
                chartSeries = createSumChart(table, mapping, viewType);
            }
            else if (type == ChartArgumentType.Count)
            {
                chartSeries = createCountChart(table, mapping, viewType);
            }
            else if (type == ChartArgumentType.MaxValue)
            {
                chartSeries = createMaxValueChart(table, mapping, viewType);
            }
            else if (type == ChartArgumentType.MinValue)
            {
                chartSeries = createMinValueChart(table, mapping, viewType);
            }
            else if (type == ChartArgumentType.Average)
            {
                chartSeries = createAverageChart(table, mapping, viewType);
            }
            else
            {
                throw new MB.Util.APPException(string.Format("暂不支持 {0} 值类型计算", mapping.ArgumentType), APPMessageType.DisplayToUser);
            }

            return(chartSeries);
        }