回复 12楼evenwan的帖子
evenwan 你好
希望附件中的Demo能够解决你当前的问题:
- private void CreateChart()
- {
- OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\App_Data\6708.mdb");
- OleDbCommand command1 = new OleDbCommand("", connection);
- OleDbDataAdapter adapter = new OleDbDataAdapter(command1);
- // 取出TktDate列的数据,作为图表的横坐标
- command1.CommandText = "SELECT DISTINCT TktDate FROM Tkt";
- DataTable dt1 = new DataTable();
- adapter.Fill(dt1);
- // 取出每天内所有公司数据的合计,作为堆积柱状图的数据源
- command1.CommandText = "SELECT TktDate, TktCarrier, SUM(TAmount) AS TAmount FROM Tkt GROUP BY TktDate, TktCarrier";
- DataTable dt2 = new DataTable();
- adapter.Fill(dt2);
- // 取出所有公司的名称,作为堆积柱状图的系列名称
- command1.CommandText = "SELECT DISTINCT TktCarrier FROM Tkt";
- DataTable dt3 = new DataTable();
- adapter.Fill(dt3);
- // 取出每天内所有公司的合计件数,作为折线图的数据源
- command1.CommandText = "SELECT TktDate, SUM(TCount) AS TAmount FROM Tkt GROUP BY TktDate";
- DataTable dt4 = new DataTable();
- adapter.Fill(dt4);
-
- // 添加堆积图系列
- StackedBarSeries series_stack = new StackedBarSeries();
- series_stack.YAxisId = 1;
- //series_stack.LegendAreaId = 0;
- // 用于组织堆积柱状图中各个系列的值
- List<double> list = null;
- DataView view = dt2.DefaultView;
- foreach (DataRow row in dt3.Rows)
- {
- BarSeries series_bar = new BarSeries();
- series_bar.CategoryNames.DataSource = dt1;
- series_bar.CategoryNames.DataField = dt1.Columns[0].ColumnName;
- series_bar.SeriesName = row["TktCarrier"].ToString();
- view.RowFilter = string.Format("TktCarrier = '{0}'", row["TktCarrier"].ToString());
- view.Sort = "TktDate";
-
- list = CreateBarSeriesValues(dt1, dt1.Columns[0].ColumnName,view);
- series_bar.Values.AddRange(list.ToArray<double>());
- series_stack.Series.Add(series_bar);
- }
- // 添加折线图系列
- FarPoint.Win.Chart.LineSeries series_line = new FarPoint.Win.Chart.LineSeries();
- series_line.Values.DataSource = dt4;
- series_line.Values.DataField = dt4.Columns[1].ColumnName;
- series_line.CategoryNames.DataSource = dt1;
- series_line.CategoryNames.DataField = dt1.Columns[0].ColumnName;
- // 设置绘图区域属性
- FarPoint.Win.Chart.YPlotArea plotArea = new FarPoint.Win.Chart.YPlotArea();
- FarPoint.Win.Chart.ChartModel model = new FarPoint.Win.Chart.ChartModel();
- plotArea.Location = new PointF(0.2F, 0.2F);
- plotArea.Size = new SizeF(0.6F, 0.6F);
- plotArea.XAxis.LabelRotationAngle = -60;
- plotArea.YAxes[0].AutoMinorUnit = false;
- // 添加第二纵坐标
- ValueAxis y2 = new ValueAxis();
- y2.AxisId = 1;
- y2.Location = AxisLocation.Far;
- plotArea.YAxes.Add(y2);
- // 图例区
- LegendArea legendArea = new LegendArea();
- legendArea.Vertical = false;
- legendArea.Location = new PointF(0.2f, 0.1f);
- // 将系列添加到绘图区域
- plotArea.Series.Add(series_stack);
- plotArea.Series.Add(series_line);
-
- model.PlotAreas.Add(plotArea);
- model.LegendAreas.Add(legendArea);
- FarPoint.Win.Spread.Chart.SpreadChart chart = new FarPoint.Win.Spread.Chart.SpreadChart();
- chart.Size = new Size(1024, 500);
- chart.Location = new Point(0, 0);
- chart.Model = model;
- fpSpread1.ActiveSheet.Charts.Add(chart);
- }
- // 组织堆积柱状图中各个系列的值
- private List<double> CreateBarSeriesValues(DataTable dt, string col, DataView view)
- {
- Dictionary<string, double> list = new Dictionary<string, double>();
- foreach (DataRow row in dt.Rows)
- {
- list.Add(row[col].ToString(), 0);
- }
- foreach (DataRowView row in view)
- {
- list[row["TktDate"].ToString()] = Convert.ToDouble(row["TAmount"].ToString());
- }
- return list.Select( item => item.Value).ToList<double>();
- }
复制代码
VS2010 + Spread .NET 6
6708_Chart.zip
(23.24 KB, 下载次数: 207)
|