首先下載owc COM組件
注冊owc
在工程中添加 C\Program Files\Common Files\Microsoft Shared\Web Components\ 文件下的owcdll引用
在工程中添加
using OWC
開始coding 舉例如下
public class ChartFactory { public ChartFactory()
{ InitTypeMap()// // TODO 在此處添加構造函數邏輯// } protected SystemWebUIWebControlsImage imgHondaLineupprivate string[] chartCategoriesArrprivate string[] chartValuesArrprivate OWCChartChartTypeEnum chartType = OWCChartChartTypeEnumchChartTypeColumnD//默認值private static Hashtable chartMap = new Hashtable()private static string chartTypeCh = 垂直柱狀圖 private static string chartTitle =
private void InitTypeMap()
{ chartMapClear()OWCChartChartTypeEnum[] chartTypes = new OWCChartChartTypeEnum[]{ ChartChartTypeEnumchChartTypeColumnClusteredChartChartTypeEnumchChartTypeColumnDChartChartTypeEnumchChartTypeBarClusteredChartChartTypeEnumchChartTypeBarDChartChartTypeEnumchChartTypeAreaChartChartTypeEnumchChartTypeAreaDChartChartTypeEnumchChartTypeDoughnutChartChartTypeEnumchChartTypeLineStackedChartChartTypeEnumchChartTypeLineDChartChartTypeEnumchChartTypeLineMarkersChartChartTypeEnumchChartTypePieChartChartTypeEnumchChartTypePieD
ChartChartTypeEnumchChartTypeRadarSmoothLineChartChartTypeEnumchChartTypeSmoothLine}
string[] chartTypesCh = new string [] {垂直柱狀統計圖D垂直柱狀統計圖水平柱狀統計圖D水平柱狀統計圖區域統計圖D區域統計圖中空餅圖折線統計圖D折線統計圖折線帶點統計圖餅圖D餅圖網狀統計圖弧線統計圖}
for(int i=i<chartTypesLengthi++)
{ chartMapAdd(chartTypesCh[i]chartTypes[i])} public ChartSpaceClass BuildCharts ()
{ string chartCategoriesStr = StringJoin (\t chartCategoriesArr)string chartValuesStr = StringJoin (\t chartValuesArr)
OWCChartSpaceClass oChartSpace = new OWCChartSpaceClass ()
// ——// Give pie and doughnut charts a legend on the bottom For the rest of // them let the control figure it out on its own // ——
chartType = (ChartChartTypeEnum)chartMap[chartTypeCh]
if (chartType == ChartChartTypeEnumchChartTypePie || chartType == ChartChartTypeEnumchChartTypePieD || chartType == ChartChartTypeEnumchChartTypeDoughnut)
{ oChartSpaceHasChartSpaceLegend = trueoChartSpaceChartSpaceLegendPosition = ChartLegendPositionEnumchLegendPositionBottom}
oChartSpaceBorderColor = blueoChartSpaceChartsAdd()oChartSpaceCharts[]HasTitle = trueoChartSpaceCharts[]Type = chartTypeoChartSpaceCharts[]ChartDepth = oChartSpaceCharts[]AspectRatio = oChartSpaceCharts[]TitleCaption = chartTitleoChartSpaceCharts[]TitleFontBold = true
oChartSpaceCharts[]SeriesCollectionAdd()oChartSpaceCharts[]SeriesCollection[]DataLabelsCollectionAdd ()
// ——// If youre charting a pie or a variation thereof percentages make a lot // more sense than values……
// ——
if (chartType == ChartChartTypeEnumchChartTypePie || chartType == ChartChartTypeEnumchChartTypePieD || chartType == ChartChartTypeEnumchChartTypeDoughnut)
{ oChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]HasPercentage = trueoChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]HasValue = false} // ——// Not so for other chart types where values have more meaning than // percentages // ——else { oChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]HasPercentage = falseoChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]HasValue = true}
// ——// Plug your own visual bells and whistles here // ——oChartSpaceCharts[]SeriesCollection[]Caption = StringEmptyoChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]FontName = verdanaoChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]FontSize = oChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]FontBold = trueoChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]FontColor = redoChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]Position = ChartDataLabelPositionEnumchLabelPositionCenter
if (chartType == ChartChartTypeEnumchChartTypeBarClustered || chartType == ChartChartTypeEnumchChartTypeBarD || chartType == ChartChartTypeEnumchChartTypeColumnClustered || chartType == ChartChartTypeEnumchChartTypeColumnD)
{ oChartSpaceCharts[]SeriesCollection[]DataLabelsCollection[]Position = ChartDataLabelPositionEnumchLabelPositionOutsideEnd}
oChartSpaceCharts[]SeriesCollection[]SetData (OWCChartDimensionsEnumchDimCategoriesConvertToInt(OWCChartSpecialDataSourcesEnumchDataLiteral) chartCategoriesStr)
oChartSpaceCharts[]SeriesCollection[]SetData (OWCChartDimensionsEnumchDimValuesConvertToInt(OWCChartSpecialDataSourcesEnumchDataLiteral) chartValuesStr)
return oChartSpace}
#region 屬性設置public string[] chartCategoriesArrValue { get { return chartCategoriesArr} set { chartCategoriesArr = value}
public string[] chartValuesArrValue { get { return chartValuesArr
} set { chartValuesArr = value} public string chartTypeValue { get { return chartTypeCh} set { chartTypeCh = value} public string chartTitleValue { get { return chartTitle} set { chartTitle = value} #endregion }
//調用 首先需要在頁面上放置一個Image來顯示產生的統計圖
public void ShowChart()
{
//初始化賦值chartFactorychartCategoriesArrValue = chartCategorieschartFactorychartValuesArrValue = chartValueschartFactorychartTitleValue = chartTitlechartFactorychartTypeValue = chartType
OWCChartSpaceClass oChartSpace = chartFactoryBuildCharts()string path = ServerMapPath() + @\images\Chartjpeg //產生圖片並保存 頁可以是png gif圖片oChartSpaceExportPicture(pathjpeg )ImageImageUrl = path // 顯示統計圖}
// 保存統計圖請參照上一篇文章
//由於每次生成的統計圖都會覆蓋原來的圖片所以有必要的話可以用日期加時間的方式來作為圖片的名字但是這樣將會產生很多圖片需及時處理如不需要只需取同名覆蓋原來圖片即可
From:http://tw.wingwit.com/Article/program/net/201311/12994.html