轉(zhuǎn)帖|使用教程|編輯:我只采一朵|2014-06-25 14:42:45.000|閱讀 7922 次
概述:今天為大家介紹一下如何在DevExpress Chart中創(chuàng)建動(dòng)態(tài)數(shù)據(jù)源。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
今天為大家介紹一下如何在DevExpress Chart中創(chuàng)建動(dòng)態(tài)數(shù)據(jù)源。在列表中點(diǎn)擊不同的行時(shí),圖表中顯示和其數(shù)據(jù)關(guān)聯(lián)的圖,效果如下:
效果挺炫吧,現(xiàn)在就開始設(shè)計(jì)吧。首先在界面中拉一個(gè)分割面板,并將PivotGdidControl(多維數(shù)據(jù)控件)和ChartControl放進(jìn)去:
然后為項(xiàng)目模擬一個(gè)數(shù)據(jù)源,在這里用北風(fēng)數(shù)據(jù)庫里的數(shù)據(jù)進(jìn)行模擬。先在項(xiàng)目中添加一個(gè)數(shù)據(jù)集:
在數(shù)據(jù)庫設(shè)計(jì)界面添加到數(shù)據(jù)庫的一個(gè)連接:
從表中選一些數(shù)據(jù):
為剛才添加的多維數(shù)據(jù)表添加一個(gè)BingdSource,設(shè)置其綁定數(shù)據(jù)源為剛添加的數(shù)據(jù)集:
再把多維數(shù)據(jù)表的數(shù)據(jù)源選為剛創(chuàng)建的綁定源。然后開始對(duì)多維表進(jìn)行設(shè)計(jì):打開設(shè)計(jì)器,我們?cè)诶锩嫣砑恿?,并分別配置好它們的Name、FieldName、UnboundFieldName、Area、GroupInterval、Caption等屬性。需要注意的是Area屬性,決定了列在列表中的位置:
此時(shí)會(huì)呈現(xiàn)如下界面:
這時(shí)第一部分?jǐn)?shù)據(jù)表的工作已經(jīng)完成,運(yùn)行后會(huì)有下面的效果:
現(xiàn)在開始第二部分,先配置一下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;
然后創(chuàng)建一個(gè)類CustomChartDataSource,此類的作用是取得PivotGridView的數(shù)據(jù)源然后返回chart的數(shù)據(jù)源,具體代碼有些繁瑣,在此就不貼出來了。下面就可以進(jìn)行兩個(gè)表的關(guān)聯(lián)了。先創(chuàng)建一個(gè)全局變量,然后讓窗口加載時(shí)給這個(gè)變量賦值,值就是PivotGridView里面綁定的數(shù)據(jù):
private CustomChartDataSource chartDataSource; private void Form1_Load(object sender, EventArgs e) { // TODO: 這行代碼將數(shù)據(jù)加載到表“dataSet1.SalesPerson”中。您可以根據(jù)需要移動(dòng)或刪除它。 this.salesPersonTableAdapter.Fill(this.dataSet1.SalesPerson); chartDataSource = new CustomChartDataSource(salesPersonPivot); salesPersonChart.DataSource = chartDataSource; }
接下來創(chuàng)建PivotGridview的CustomCellValue事件,在此事件中創(chuàng)建一個(gè)月份和數(shù)值的字典,用來保存點(diǎn)擊不同類型后的不同值:
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; } }
最后創(chuàng)建窗口關(guān)閉的方法進(jìn)行資源回收:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { salesPersonChart.DataSource = null; chartDataSource.Dispose(); }
到此為止,功能全部完成,最主要的就是對(duì)PivotGridview的掌握和對(duì)CustomCellValue事件的理解。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件