轉帖|使用教程|編輯:我只采一朵|2014-06-25 14:42:45.000|閱讀 7922 次
概述:今天為大家介紹一下如何在DevExpress Chart中創建動態數據源。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
今天為大家介紹一下如何在DevExpress Chart中創建動態數據源。在列表中點擊不同的行時,圖表中顯示和其數據關聯的圖,效果如下:
效果挺炫吧,現在就開始設計吧。首先在界面中拉一個分割面板,并將PivotGdidControl(多維數據控件)和ChartControl放進去:
然后為項目模擬一個數據源,在這里用北風數據庫里的數據進行模擬。先在項目中添加一個數據集:
在數據庫設計界面添加到數據庫的一個連接:
從表中選一些數據:
為剛才添加的多維數據表添加一個BingdSource,設置其綁定數據源為剛添加的數據集:
再把多維數據表的數據源選為剛創建的綁定源。然后開始對多維表進行設計:打開設計器,我們在里面添加六列,并分別配置好它們的Name、FieldName、UnboundFieldName、Area、GroupInterval、Caption等屬性。需要注意的是Area屬性,決定了列在列表中的位置:
此時會呈現如下界面:
這時第一部分數據表的工作已經完成,運行后會有下面的效果:
現在開始第二部分,先配置一下chart的屬性:
// // salesPersonChart // xyDiagram1.AxisX.VisibleInPanesSerializable = "-1"; xyDiagram1.AxisX.WholeRange.AutoSideMargins = true; xyDiagram1.AxisY.VisibleInPanesSerializable = "-1"; xyDiagram1.AxisY.WholeRange.AutoSideMargins = true; this.salesPersonChart.Diagram = xyDiagram1; this.salesPersonChart.Dock = System.Windows.Forms.DockStyle.Fill; this.salesPersonChart.Location = new System.Drawing.Point(0, 0); this.salesPersonChart.Name = "salesPersonChart"; this.salesPersonChart.SeriesDataMember = "Series"; this.salesPersonChart.SeriesSerializable = new DevExpress.XtraCharts.Series[0]; this.salesPersonChart.SeriesTemplate.ArgumentDataMember = "Arguments"; this.salesPersonChart.SeriesTemplate.ValueDataMembersSerializable = "Values"; this.salesPersonChart.Size = new System.Drawing.Size(696, 287); this.salesPersonChart.TabIndex = 0;
然后創建一個類CustomChartDataSource,此類的作用是取得PivotGridView的數據源然后返回chart的數據源,具體代碼有些繁瑣,在此就不貼出來了。下面就可以進行兩個表的關聯了。先創建一個全局變量,然后讓窗口加載時給這個變量賦值,值就是PivotGridView里面綁定的數據:
private CustomChartDataSource chartDataSource; private void Form1_Load(object sender, EventArgs e) { // TODO: 這行代碼將數據加載到表“dataSet1.SalesPerson”中。您可以根據需要移動或刪除它。 this.salesPersonTableAdapter.Fill(this.dataSet1.SalesPerson); chartDataSource = new CustomChartDataSource(salesPersonPivot); salesPersonChart.DataSource = chartDataSource; }
接下來創建PivotGridview的CustomCellValue事件,在此事件中創建一個月份和數值的字典,用來保存點擊不同類型后的不同值:
private void salesPersonPivot_CustomCellValue(object sender, DevExpress.XtraPivotGrid.PivotCellValueEventArgs e) { if (e.DataField == fieldExtendedPriceUnbound) { PivotDrillDownDataSource ds = e.CreateDrillDownDataSource(); decimal value = 0; Dictionary<PivotGridField, object> columnFieldValues = new Dictionary<PivotGridField, object>(); PivotGridField[] columnFields = e.GetColumnFields(); foreach (PivotGridField field in columnFields) { columnFieldValues[field] = e.GetFieldValue(field); } for (int i = 0; i < ds.RowCount; i++) { bool skip = false; foreach (PivotGridField field in columnFields) { if (!Comparer.Equals(ds[i][field], columnFieldValues[field])) { skip = true; break; } } if (skip) continue; decimal v1 = Convert.ToDecimal(ds[i][e.DataField]); value += v1; } e.Value = value; } }
最后創建窗口關閉的方法進行資源回收:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { salesPersonChart.DataSource = null; chartDataSource.Dispose(); }
到此為止,功能全部完成,最主要的就是對PivotGridview的掌握和對CustomCellValue事件的理解。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件