星期一, 3月 24, 2008

利用OWC畫統計圖

VB超人告訴我,可以用Microsoft Office Web Components(OWC)畫統計圖,比起其他的Asp.net統計圖,又多了些互動功能(在用戶端有裝Office的情況)。

下載:Office 2003 增益集:Office 網頁元件
參考:
The Microsoft Office Web Components
ASP.NET Server-Side Charting with OWC11

在Asp.Net 2.0的開發模式中,只要放在bin下就算加入參考,所以鳥毅先開一個檔案系統的網站


再把從codeproject下載的專案裏的bin目錄複製過來,省得自己找dll檔,在專案總管按下重新整理即可


把Alvin Bruney寫的這段Code複製到Page_Load,變成
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. //First create a ChartSpace object to hold the chart
  4. ChartSpace objCSpace = new ChartSpaceClass();
  5. //Add a chart and provide a type
  6. ChChart objChart = objCSpace.Charts.Add(0);
  7. objChart.Type = ChartChartTypeEnum.chChartTypePie3D;
  8. //add chart titles and legend
  9. objChart.HasTitle = true;
  10. objChart.Title.Caption = "ASP.NET Charts";
  11. objChart.HasLegend = true;
  12. objChart.Legend.Border.DashStyle = OWC11.ChartLineDashStyleEnum.chLineDash;
  13. objChart.Legend.Position = OWC11.ChartLegendPositionEnum.chLegendPositionRight;
  14. //Populate with contrived data
  15. string strCategory = "Cars, Trucks, Vans, Big Rigs, Motorcycles, Mopeds";
  16. string strValue = "13,12,31,43,23,15";
  17. //Add a series to the chart's series collection
  18. objChart.SeriesCollection.Add(0);
  19. //load the category and value data
  20. objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,
  21. (int)ChartSpecialDataSourcesEnum.chDataLiteral, strCategory);
  22. objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues,
  23. (int)ChartSpecialDataSourcesEnum.chDataLiteral, strValue);
  24. //show the chart on the client
  25. Response.ContentType = "image/gif";
  26. Response.BinaryWrite((byte[])objCSpace.GetPicture("gif", 500, 400));
  27. Response.End();
  28. }

執行後結果:


詳細的物件模型請參考:HOW TO:尋找 Office Web 元件 (OWC) 程式設計說明文件與範例

說真的,這種COM物件的繪圖方式類似ProEssentials得一個個指定,我還是比較喜歡原生支援.Net的Crystal Reports

沒有留言: