轉帖|其它|編輯:郝浩|2010-08-23 11:49:29.000|閱讀 3982 次
概述:最近項目中想采用Dundas Chart繪制圖形,于是讓我們先試試可不可以滿足原有的所有圖形,所以開始學習Dundas Chart控件+VS2008繪制圖形。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
最近項目中想采用Dundas Chart繪制圖形,于是讓我們先試試可不可以滿足原有的所有圖形,所以開始學習Dundas Chart控件+VS2008繪制圖形。
在這期間也遇到了好多問題,Dundas Chart圖形控件本身沒有多餅圖的樣式,所以采用添加多個 ChartArea 來繪制多餅圖,這樣有一個缺點就是,它自動分配的區域是從上到下,從左到右排列,一般我們都是水平方向排列,要想實現這樣的話就得自己設置區域的左上角(x、y坐標),寬度和高度大小。
還有一個問題就是在繪制散點圖的時候,需要添加趨勢線(線性回歸分析),例如Excel 2007中,如下圖:
項目中有這樣的需求,之前采用TeeChart做過這樣的圖形,原以為Dundas不存在這樣的函數,通過自己查看幫助和廠家的技術人員交流后,存在這樣的功能,并且可以完成這樣的圖形,具體方法如下:
chart.BackGradientEndColor = Color.White;
chart.BackColor = Color.White;
chart.BackGradientType = GradientType.DiagonalLeft;
chart.Palette = ChartColorPalette.Dundas;
chart.BorderLineColor = Color.FromArgb(26, 59, 105);
chart.BorderLineStyle = ChartDashStyle.Solid;
chart.BorderLineWidth = 1;
chart.BorderSkin.FrameBackColor = Color.CornflowerBlue;
chart.BorderSkin.FrameBackGradientEndColor = Color.CornflowerBlue;
chart.BorderSkin.PageColor = Color.CadetBlue;
chart.BorderSkin.SkinStyle = BorderSkinStyle.None;
chart.Width = 700;
chart.Height = 500;
chart.ImageType = ChartImageType.Jpeg;
//----------綁定數據----------
ds = OracleDB.Dataset(sqlString);
chart.DataSource = ds;
DataView dv = new DataView(ds.Tables[0]);
if (ds.Tables[0].Rows.Count > 0)
{
//----------設置圖表區域樣式----------
chart.ChartAreas.Clear();
for (int C = 1; C < 2; C++)
{
ChartArea chartarea = new ChartArea();
chartarea.Name = "ChartArea" + C.ToString();
chart.ChartAreas.Add(chartarea);
chart.ChartAreas["ChartArea" + C.ToString()].Area3DStyle.Enable3D = false;
chart.ChartAreas["ChartArea" + C.ToString()].AlignOrientation = AreaAlignOrientation.All;
chart.ChartAreas["ChartArea" + C.ToString()].Area3DStyle.Light = LightStyle.Simplistic;
chart.ChartAreas["ChartArea" + C.ToString()].BorderWidth = 1;
chart.ChartAreas["ChartArea" + C.ToString()].BorderStyle = ChartDashStyle.Solid;
chart.ChartAreas["ChartArea" + C.ToString()].BackColor = Color.White;
chart.ChartAreas["ChartArea" + C.ToString()].Position.Auto = true;
chart.ChartAreas["ChartArea" + C.ToString()].AxisX.Title = "A(10^4t)";
chart.ChartAreas["ChartArea" + C.ToString()].AxisX.MajorGrid.LineStyle = ChartDashStyle.Dash;
chart.ChartAreas["ChartArea" + C.ToString()].AxisX.MajorTickMark.Style = TickMarkStyle.Inside;
chart.ChartAreas["ChartArea" + C.ToString()].AxisY.Title = "B";
chart.ChartAreas["ChartArea" + C.ToString()].AxisY.MajorGrid.LineStyle = ChartDashStyle.Dash;
chart.ChartAreas["ChartArea" + C.ToString()].AxisY.MajorTickMark.Style = TickMarkStyle.Inside;
chart.ChartAreas["ChartArea" + C.ToString()].AxisY.StartFromZero = false;
}
//----------添加標題----------
chart.Titles["Title1"].Text = "散點圖趨勢線" ;
chart.Titles["Title1"].Alignment = ContentAlignment.TopCenter;
chart.Titles["Title1"].Font = new Font("Microsoft Sans Serif", 12, FontStyle.Bold);
chart.Titles["Title1"].Color = Color.FromArgb(72, 72, 72);
chart.Titles["Title1"].DockInsideChartArea = false;
//----------設置圖例區域樣式----------
chart.Legends["Default"].Enabled = false;
chart.Legends.Clear();
for (int L = 1; L < 2; L++)
{
Legend legend = new Legend();
legend.Name = "Legend" + L.ToString();
chart.Legends.Add(legend);
legend.DockToChartArea = "ChartArea" + L.ToString();
chart.Legends["Legend" + L.ToString()].Alignment = StringAlignment.Center;
chart.Legends["Legend" + L.ToString()].Docking = LegendDocking.Right;//圖例顯示位置
chart.Legends["Legend" + L.ToString()].DockInsideChartArea = false;
chart.Legends["Legend" + L.ToString()].BackColor = Color.Transparent;
chart.Legends["Legend" + L.ToString()].BorderStyle = ChartDashStyle.Solid;
chart.Legends["Legend" + L.ToString()].BorderWidth = 1;
}
//----------添加圖形系列---------
chart.Series.Clear();
Series series = new Series();
series.Name = "Series1";
chart.Series.Add(series);
series.ChartArea = "ChartArea1";
series.Type = SeriesChartType.Point;
//-----必須添加第二個空系列,Type為直線(Line),用于趨勢線
Series series2 = new Series();
series2.Name = "Series2";
chart.Series.Add(series2);
series2.ChartArea = "ChartArea1";
series2.Type = SeriesChartType.Line;
series2.Color = Color.Red;
this.chart.Series["Series1"].Points.DataBindXY(dv, "A", dv, "B");
this.chart.DataManipulator.FormulaFinancial(FinancialFormula.Forecasting, "Linear,1,true,true", "Series1:Y", "Series2:Y,Series2:Y2,Series2:Y3");
//----------添加圖形----------
this.Pal_Chart.Controls.Add(chart);
在這段代碼中,倒數第二句非常重要,也是用來添加趨勢線的函數,我之前使用的時候出現了好多問題,經過技術人員的指導,總算是清楚了這個函數的正確的使用方法。如下:
//錯誤寫法
this.Chart1.DataManipulator.FormulaFinancial(FinancialFormula.Forecasting, "Linear,2,true,true", "Series1:Y", "Series2:Y");
//正確
this.Chart1.DataManipulator.FormulaFinancial(FinancialFormula.Forecasting, "Linear,2,true,true", "Series1:Y", "Series2:Y,Series2:Y2,Series2:Y3");
這里用到了四個參數:
第一個是選擇的統計公式,可有好多種選擇,具體不在細說,可以看幫助。
主說的是第二個參數和第四個參數:
第二個參數中第1項指定的是預測類型(Exponential、Logarithmic、Power和Linear)、第2項是預測的數據量(這里不光是趨勢線,它還可以做預測,因此要填寫預測的數據量)、第3項是是否填寫被測數據誤差、第4項是是否填寫預測數據誤差。
第四個參數是用來接收,通過對第二個參數的3,4項的了來看,輸出中可以看到Series2:Y是第一個用來接收2個數據點預測的Y值,而被測數據誤差和預測數據誤差設置為接受,也要填寫對應的數據項目來接收 ,否則會出錯。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園