下面是我做項目時,用OWC來畫圖,自己寫的一個類
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.IO;
using System.Data.SqlClient;
using OWC10;
using System.Text.RegularExpressions;
using System.Globalization;
namespace OWC
{
/// <summary>
/// ChartClass 的摘要描述。
/// </summary>
public class ChartClass
{
public string ConnString = "server=INLLHBI01;database=INNOLUX;uid=biadm;pwd=bidwh";
public string PathInfo = "";
private System.Globalization.DateTimeFormatInfo format = new DateTimeFormatInfo();
public ChartClass()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
#region DrawPic
/*
* 1 ChartFigure: 圖的形狀
* 2.ChartTitleCaption 圖片上端中間的Title
* 3.ChartXCaption X軸的描述,為Pie圖的時候無效
* 4.ChartYCaption Y軸的描述,為Pie圖的時候無效
* 5.SeriesName[] 有幾個系列的數組
* 6.strValue[] 每個系列對應一個strValue[i]
* 7.Column[] 每個系列取值時,對應的欄位名稱
* 8.GroupBy strCategory從DataTable取值時對應的欄位名稱
* 9.Name 保存圖片的名稱
* 10.Group 保存到硬盤上時分類文件夾的名稱
* 11.dt 傳入數據表
* 12.ChartWidth 指定圖的寬度
* 13.ChartHeight 指定圖的高度
* 14.IsPer 是否計算百分比
* 15.LinePer 是否是線狀百分比
* 16.Label 在圖例中需要加入的字符
* */
public string Draw(string ChartFigure,string ChartTitleCaption,string ChartXCaption,string ChartYCaption,
string []SeriesName,string []strValue,string [] Column,string GroupBy,string Name,string Group,DataTable dt,
int ChartWidth,int ChartHeight,bool IsPer,bool LinePer,string Label)
{
//創建ChartSpace對象來放置圖表
OWC10.ChartSpaceClass Obj = new ChartSpaceClass();
Obj.Clear();
Obj.Refresh();
//在ChartSpace對象中添加圖表,Add方法返回chart對象
OWC10.ChChart ObjChart = Obj.Charts.Add(0);
//設置圖形的背景顏色
ObjChart.PlotArea.Interior.Color = "LightGoldenrodYellow";
/*
// 設置漸變色
ObjChart.PlotArea.Interior.SetPresetGradient(OWC10.ChartGradientStyleEnum.chGradientDiagonalDown,OWC10.
ChartGradientVariantEnum.chGradientVariantEdges,OWC10.ChartPresetGradientTypeEnum.chGradientGoldII);
*/
//設置邊框
ObjChart.Border.Color = "Blue";
//指定圖表的類型。類型由OWC.ChartChartTypeEnum枚舉值得到
switch(ChartFigure)
{
case "Line":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypeLine ;//.chChartTypeLineStacked; //Line
break;
case "Pie":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypePieExploded; //Pie
break;
case "Bar":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypeColumnStacked;
break;
default:
break;
}
//指定圖表是否需要圖例及圖例位置
ObjChart.HasLegend= true;
ObjChart.Legend.Position=ChartLegendPositionEnum.chLegendPositionRight;
//標題及相關屬性
ObjChart.HasTitle=true;
ObjChart.Title.Caption=ChartTitleCaption;
ObjChart.Title.Font.Bold=true;
ObjChart.Title.Font.Size=12;
ObjChart.Title.Font.Color="red";
ObjChart.Title.Font.Name="Arial";
//給定x,y軸的圖示說明
/*//如果是餅圖,則下面的代碼得註釋掉 */
if(ChartFigure!="Pie")
{
ObjChart.Axes[0].HasMajorGridlines = false;
ObjChart.Axes[0].HasTitle=true;
ObjChart.Axes[0].Title.Caption=ChartXCaption;
ObjChart.Axes[0].Title.Font.Bold=true;
ObjChart.Axes[0].Title.Font.Size=12;
ObjChart.Axes[0].Title.Font.Name="Arial";
ObjChart.Axes[1].HasMajorGridlines = false; //去除圖形上的刻度線
ObjChart.Axes[1].HasTitle=true;
ObjChart.Axes[1].Title.Caption=ChartYCaption;
ObjChart.Axes[1].Title.Font.Bold=true;
ObjChart.Axes[1].Title.Font.Size=12;
ObjChart.Axes[1].Title.Font.Name="Arial";
}
string strCategory = "";
double maxNum = 0;
double [] Sum = new double [SeriesName.Length]; //用來求百分比的
double []maxNumBar = new double[SeriesName.Length]; //用來把StackBar類型的每個系列的最大值求出來,然後
string ColumnName = ""; //用來記錄會有多少欄位出現,記錄住第一個欄位的名稱,每次名稱更換的時候,將該欄位的第一個值保存進maxNumBar[]
//因為可能出現每次第一個值就是最大值,如果不給 maxNumBar[] 附值得話,後面 maxNum也會是 0,這樣就會出錯了.
if(strValue.Length>0)
{
ColumnName = Column[0].Trim().ToString(); //這裡記錄第一個欄位的名稱
for(int j=0;j<strValue.Length;j++)
{
if(dt.Rows.Count>0)
{
if(ColumnName!=Column[j].Trim().ToString())
{
maxNumBar[j] = Convert.ToDouble(dt.Rows[0][Column[j]].ToString().Trim()); //每次更換欄位的時候,都將該欄位的第一個值附值給 maxNumBar[j] 這裡的 j 是該 欄位的Index
}
maxNum = Convert.ToDouble(dt.Rows[0][Column[j]].ToString());
for(int i=0;i<dt.Rows.Count;i++)
{
strValue [j] += dt.Rows[i][Column[j]].ToString()+ "\t";
//如果需要求百分比就做求和
if(IsPer&&LinePer)
{
Sum[j] += Convert.ToDouble(dt.Rows[i][Column[j]].ToString().Trim());
}
if(maxNum<Convert.ToDouble(dt.Rows[i][Column[j]].ToString().Trim()))
{
maxNum = Convert.ToDouble(dt.Rows[i][Column[j]].ToString().Trim());
if(ChartFigure=="Bar")
{
maxNumBar[j]=maxNum;
}
}
if(j ==0)
{
if(Label!="")
{
strCategory += dt.Rows[i][GroupBy].ToString().Trim()+Label+"\t";
}
else
{
strCategory += dt.Rows[i][GroupBy].ToString().Trim()+"\t";
}
}
}
}
}
}
if(ChartFigure=="Bar")
{
maxNum = 0;
for(int i =0;i<maxNumBar.Length;i++)
{
maxNum += maxNumBar[i];
}
}
if(!IsPer)
{
for(int j =0;j<strValue.Length;j++)
{
Regex regex = new Regex("\t");
string [] str = regex.Split(strValue[j]);
strValue[j] = "";
for(int i = 0;i<str.Length;i++)
{
if(str[i]!="")
{
strValue[j] +=str[i].ToString()+"\t";
}
}
}
}
else if(LinePer)//如果不需要使用這裡的求百分比的功能,通過該屬性來控制
{
for(int j =0;j<strValue.Length;j++)
{
Regex regex = new Regex("\t");
string [] str = regex.Split(strValue[j]);
strValue[j] = "";
for(int i = 0;i<str.Length;i++)
{
if(str[i]!="")
{
//per = Convert.ToDouble(str[i].ToString())/Sum[j];
strValue[j]+=Convert.ToDouble(str[i].ToString())/Sum[j]+"\t";
}
}
}
}
//設置坐標格式,最大值和最小值
//如果是餅圖或者百分比的圖,那么就不能給最大值加50000
if(ChartFigure!="Pie"&&!IsPer)
{
ObjChart.Axes[1].NumberFormat = "###,###";
ObjChart.Axes[1].Scaling.Maximum=maxNum+maxNum*0.1;
ObjChart.Axes[1].Scaling.Minimum=0;
ObjChart.Axes[0].Font.Size = 12;
}
if(ChartFigure == "Line")
{
ObjChart.Axes[1].NumberFormat = "0%";
ObjChart.Axes[1].Scaling.Maximum=1;
ObjChart.Axes[1].Scaling.Minimum=0;
ObjChart.Axes[0].Font.Size = 12;
}
string SName = "";
string SValue = "";
for(int i =0;i<SeriesName.Length;i++)
{
//添加series
OWC10.ChSeries ser=ObjChart.SeriesCollection.Add(i);
SName = SeriesName[i];
SValue = strValue[i];
//ObjChart.SeriesCollection.Add(i);
ObjChart.SeriesCollection[i].TipText = ChartTitleCaption;
ObjChart.SeriesCollection[i].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames,(int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral,SName);
//給定分類
ObjChart.SeriesCollection[i].SetData(OWC10.ChartDimensionsEnum.chDimCategories,(int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral,strCategory);
//給定值
ObjChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues,(int)ChartSpecialDataSourcesEnum.chDataLiteral,SValue );
//給點來點效果
ObjChart.SeriesCollection[i].Marker.Style = ChartMarkerStyleEnum.chMarkerStyleTriangle;
ObjChart.SeriesCollection[i].Marker.Size = 5;
//Series上的數據標示
ObjChart.SeriesCollection[i].DataLabelsCollection.Add();
ObjChart.SeriesCollection[i].DataLabelsCollection[0].HasValue =true;
ObjChart.SeriesCollection[i].DataLabelsCollection[0].Font.Size = 12;
//如果是StackBar設置顏色
if(ChartFigure=="Bar")
{
ObjChart.SeriesCollection[i].Interior.Color = this.GetColor(i);
}
if(!IsPer)
{
ObjChart.SeriesCollection[i].DataLabelsCollection[0].NumberFormat = "#,###";
}
else
{
ObjChart.SeriesCollection[i].DataLabelsCollection[0].NumberFormat = "0.00%";
}
}
string fileName = "";
fileName = Name+DateTime.Now.ToString("yyyy/MM/dd:HH:mm:ss");
fileName = fileName.Replace( "-", "");
fileName = fileName.Replace( "/", "");
fileName = fileName.Replace( " ", "");
fileName = fileName.Replace( ":", "");
fileName = fileName.Replace( "PM", "");
fileName = fileName.Replace( "AM", "");
fileName = fileName.Replace( "年", "");
fileName = fileName.Replace( "月", "");
fileName = fileName.Replace( "日", "");
fileName = fileName.Replace( "上午", "");
fileName = fileName.Replace( "下午", "");
fileName += ".gif";
//string Path = Server.MapPath(".")+"\\"+DateTime.Now.ToString("yyyy/MM")+"\\"+Group;
string Path = "D:\\BI_Data\\INNOLUX_FILE\\IMAGE"+"\\"+DateTime.Now.ToString("yyyy/MM",format)+"\\"+Group;
PathInfo = "\\"+DateTime.Now.ToString("yyyy/MM",format)+"\\"+Group+"\\"+fileName;
DirectoryInfo mainInfo = new DirectoryInfo(Path);
string filePath ="";
if(mainInfo.Exists)
{
filePath = Path+"\\"+fileName;
Obj.ExportPicture(filePath, "GIF", ChartWidth, ChartHeight);
}
else
{
mainInfo.Create();
filePath = Path+"\\"+fileName;
Obj.ExportPicture(filePath, "GIF", ChartWidth, ChartHeight);
}
return PathInfo;
}
#endregion
#region Set BarColor
private string GetColor(int i)
{
string color = "";
switch(i.ToString())
{
case "0":
color = "#6586E7";
break;
case "1":
color = "#F7B782";
break;
default:
color = "#C0C0FF";
break;
}
return color;
}
#endregion
#region DrawPic1 帶有指定標尺最下值得方法
/*
* 1 ChartFigure: 圖的形狀
* 2.ChartTitleCaption 圖片上端中間的Title
* 3.ChartXCaption X軸的描述,為Pie圖的時候無效
* 4.ChartYCaption Y軸的描述,為Pie圖的時候無效
* 5.SeriesName[] 有幾個系列的數組
* 6.strValue[] 每個系列對應一個strValue[i]
* 7.Column[] 每個系列取值時,對應的欄位名稱
* 8.GroupBy strCategory從DataTable取值時對應的欄位名稱
* 9.Name 保存圖片的名稱
* 10.Group 保存到硬盤上時分類文件夾的名稱
* 11.dt 傳入數據表
* 12.ChartWidth 指定圖的寬度
* 13.ChartHeight 指定圖的高度
* 14.IsPer 是否計算百分比
* 15.LinePer 是否是線狀百分比
* 16.Label 在圖例中需要加入的字符
* 17.MinMum 指定圖片Y軸的最小值
* 18.Flag 是否需要顯示數值
* 19.FontSize 指定數值的字體大小
* */
public string Draw(string ChartFigure,string ChartTitleCaption,string ChartXCaption,string ChartYCaption,
string []SeriesName,string []strValue,string [] Column,string GroupBy,string Name,string Group,DataTable dt,
int ChartWidth,int ChartHeight,bool IsPer,bool LinePer,string Label,double MinMum,bool Flag,int FontSize)
{
//創建ChartSpace對象來放置圖表
OWC10.ChartSpaceClass Obj = new ChartSpaceClass();
Obj.Clear();
Obj.Refresh();
//在ChartSpace對象中添加圖表,Add方法返回chart對象
OWC10.ChChart ObjChart = Obj.Charts.Add(0);
//設置圖形的背景顏色
ObjChart.PlotArea.Interior.Color = "LightGoldenrodYellow";
/*
// 設置漸變色
ObjChart.PlotArea.Interior.SetPresetGradient(OWC10.ChartGradientStyleEnum.chGradientDiagonalDown,OWC10.
ChartGradientVariantEnum.chGradientVariantEdges,OWC10.ChartPresetGradientTypeEnum.chGradientGoldII);
*/
//設置邊框
ObjChart.Border.Color = "Blue";
//指定圖表的類型。類型由OWC.ChartChartTypeEnum枚舉值得到
switch(ChartFigure)
{
case "Line":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypeLine ;//.chChartTypeLineStacked; //Line
break;
case "Pie":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypePieExploded; //Pie
break;
case "Bar":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypeColumnStacked;
break;
default:
break;
}
//指定圖表是否需要圖例及圖例位置
ObjChart.HasLegend= true;
ObjChart.Legend.Position=ChartLegendPositionEnum.chLegendPositionRight;
//標題及相關屬性
ObjChart.HasTitle=true;
ObjChart.Title.Caption=ChartTitleCaption;
ObjChart.Title.Font.Bold=true;
ObjChart.Title.Font.Size=12;
ObjChart.Title.Font.Color="red";
ObjChart.Title.Font.Name="Arial";
//給定x,y軸的圖示說明
/*//如果是餅圖,則下面的代碼得註釋掉 */
if(ChartFigure!="Pie")
{
ObjChart.Axes[0].HasMajorGridlines = false;
ObjChart.Axes[0].HasTitle=true;
ObjChart.Axes[0].Title.Caption=ChartXCaption;
ObjChart.Axes[0].Title.Font.Bold=true;
ObjChart.Axes[0].Title.Font.Size=12;
ObjChart.Axes[0].Title.Font.Name="Arial";
ObjChart.Axes[1].HasMajorGridlines = true; //去除圖形上的刻度線
ObjChart.Axes[1].HasTitle=true;
ObjChart.Axes[1].Title.Caption=ChartYCaption;
ObjChart.Axes[1].Title.Font.Bold=true;
ObjChart.Axes[1].Title.Font.Size=12;
ObjChart.Axes[1].Title.Font.Name="Arial";
}
string strCategory = "";
double maxNum = 0;
double [] Sum = new double [SeriesName.Length]; //用來求百分比的
double []maxNumBar = new double[SeriesName.Length]; //用來把StackBar類型的每個系列的最大值求出來,然後
string ColumnName = ""; //用來記錄會有多少欄位出現,記錄住第一個欄位的名稱,每次名稱更換的時候,將該欄位的第一個值保存進maxNumBar[]
//因為可能出現每次第一個值就是最大值,如果不給 maxNumBar[] 附值得話,後面 maxNum也會是 0,這樣就會出錯了.
if(strValue.Length>0)
{
ColumnName = Column[0].Trim().ToString(); //這裡記錄第一個欄位的名稱
for(int j=0;j<strValue.Length;j++)
{
if(dt.Rows.Count>0)
{
if(ColumnName!=Column[j].Trim().ToString())
{
maxNumBar[j] = Convert.ToDouble(dt.Rows[0][Column[j]].ToString().Trim()); //每次更換欄位的時候,都將該欄位的第一個值附值給 maxNumBar[j] 這裡的 j 是該 欄位的Index
}
maxNum = Convert.ToDouble(dt.Rows[0][Column[j]].ToString());
for(int i=0;i<dt.Rows.Count;i++)
{
strValue [j] += dt.Rows[i][Column[j]].ToString()+ "\t";
//如果需要求百分比就做求和
if(IsPer&&LinePer)
{
Sum[j] += Convert.ToDouble(dt.Rows[i][Column[j]].ToString().Trim());
}
if(maxNum<Convert.ToDouble(dt.Rows[i][Column[j]].ToString().Trim()))
{
maxNum = Convert.ToDouble(dt.Rows[i][Column[j]].ToString().Trim());
if(ChartFigure=="Bar")
{
maxNumBar[j]=maxNum;
}
}
if(j ==0)
{
if(Label!="")
{
strCategory += dt.Rows[i][GroupBy].ToString().Trim()+Label+"\t";
}
else
{
strCategory += dt.Rows[i][GroupBy].ToString().Trim()+"\t";
}
}
}
}
}
}
if(ChartFigure=="Bar")
{
maxNum = 0;
for(int i =0;i<maxNumBar.Length;i++)
{
maxNum += maxNumBar[i];
}
}
if(!IsPer)
{
for(int j =0;j<strValue.Length;j++)
{
Regex regex = new Regex("\t");
string [] str = regex.Split(strValue[j]);
strValue[j] = "";
for(int i = 0;i<str.Length;i++)
{
if(str[i]!="")
{
strValue[j] +=str[i].ToString()+"\t";
}
}
}
}
else if(LinePer)//如果不需要使用這裡的求百分比的功能,通過該屬性來控制
{
for(int j =0;j<strValue.Length;j++)
{
Regex regex = new Regex("\t");
string [] str = regex.Split(strValue[j]);
strValue[j] = "";
for(int i = 0;i<str.Length;i++)
{
if(str[i]!="")
{
//per = Convert.ToDouble(str[i].ToString())/Sum[j];
strValue[j]+=Convert.ToDouble(str[i].ToString())/Sum[j]+"\t";
}
}
}
}
//設置坐標格式,最大值和最小值
//如果是餅圖或者百分比的圖,那么就不能給最大值加50000
if(ChartFigure!="Pie"&&!IsPer)
{
ObjChart.Axes[1].NumberFormat = "###,###";
ObjChart.Axes[1].Scaling.Maximum=maxNum+maxNum*0.1;
ObjChart.Axes[1].Scaling.Minimum=MinMum;
ObjChart.Axes[0].Font.Size = 12;
}
if(ChartFigure == "Line")
{
ObjChart.Axes[1].NumberFormat = "###,###";
ObjChart.Axes[1].Scaling.Maximum=maxNum+maxNum*0.1;
ObjChart.Axes[1].Scaling.Minimum=MinMum;
ObjChart.Axes[0].Font.Size = 9;
ObjChart.Axes[0].TickLabelSpacing = 1;
}
string SName = "";
string SValue = "";
for(int i =0;i<SeriesName.Length;i++)
{
//添加series
OWC10.ChSeries ser=ObjChart.SeriesCollection.Add(i);
SName = SeriesName[i];
SValue = strValue[i];
//ObjChart.SeriesCollection.Add(i);
ObjChart.SeriesCollection[i].TipText = ChartTitleCaption;
ObjChart.SeriesCollection[i].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames,(int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral,SName);
//給定分類
ObjChart.SeriesCollection[i].SetData(OWC10.ChartDimensionsEnum.chDimCategories,(int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral,strCategory);
//給定值
ObjChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues,(int)ChartSpecialDataSourcesEnum.chDataLiteral,SValue );
//給點來點效果
ObjChart.SeriesCollection[i].Marker.Style = ChartMarkerStyleEnum.chMarkerStyleTriangle;
ObjChart.SeriesCollection[i].Marker.Size = 5;
//Series上的數據標示
ObjChart.SeriesCollection[i].DataLabelsCollection.Add();
ObjChart.SeriesCollection[i].DataLabelsCollection[0].HasValue =Flag;
ObjChart.SeriesCollection[i].DataLabelsCollection[0].Font.Size = FontSize;
//如果是StackBar設置顏色
if(ChartFigure=="Bar")
{
ObjChart.SeriesCollection[i].Interior.Color = this.GetColor(i);
}
if(!IsPer)
{
ObjChart.SeriesCollection[i].DataLabelsCollection[0].NumberFormat = "#,###";
}
else
{
ObjChart.SeriesCollection[i].DataLabelsCollection[0].NumberFormat = "0.00%";
}
}
string fileName = "";
fileName = Name+DateTime.Now.ToString("yyyy/MM/dd:HH:mm:ss");
fileName = fileName.Replace( "-", "");
fileName = fileName.Replace( "/", "");
fileName = fileName.Replace( " ", "");
fileName = fileName.Replace( ":", "");
fileName = fileName.Replace( "PM", "");
fileName = fileName.Replace( "AM", "");
fileName = fileName.Replace( "年", "");
fileName = fileName.Replace( "月", "");
fileName = fileName.Replace( "日", "");
fileName = fileName.Replace( "上午", "");
fileName = fileName.Replace( "下午", "");
fileName += ".gif";
//string Path = Server.MapPath(".")+"\\"+DateTime.Now.ToString("yyyy/MM")+"\\"+Group;
string Path = "D:\\BI_Data\\INNOLUX_FILE\\IMAGE"+"\\"+DateTime.Now.ToString("yyyy/MM",format)+"\\"+Group;
PathInfo = "\\"+DateTime.Now.ToString("yyyy/MM",format)+"\\"+Group+"\\"+fileName;
DirectoryInfo mainInfo = new DirectoryInfo(Path);
string filePath ="";
if(mainInfo.Exists)
{
filePath = Path+"\\"+fileName;
Obj.ExportPicture(filePath, "GIF", ChartWidth, ChartHeight);
}
else
{
mainInfo.Create();
filePath = Path+"\\"+fileName;
Obj.ExportPicture(filePath, "GIF", ChartWidth, ChartHeight);
}
return PathInfo;
}
#endregion
#region DrawPic
/*
* 1 ChartFigure: 圖的形狀
* 2.ChartTitleCaption 圖片上端中間的Title
* 3.ChartXCaption X軸的描述,為Pie圖的時候無效
* 4.ChartYCaption Y軸的描述,為Pie圖的時候無效
* 5.SeriesName[] 有幾個系列的數組
* 6.strValue[] 每個系列對應一個strValue[i]
* 7.Column[] 每個系列取值時,對應的欄位名稱
* 8.GroupBy strCategory從DataTable取值時對應的欄位名稱
* 9.Name 保存圖片的名稱
* 10.Group 保存到硬盤上時分類文件夾的名稱
* 11.dt 傳入數據表
* 12.ChartWidth 指定圖的寬度
* 13.ChartHeight 指定圖的高度
* 14.IsPer 是否計算百分比
* 15.LinePer 是否是線狀百分比
* 16.MaxNum 自己指定最大值
* 17.MinNum 自己指定最小值
* */
public string Draw(string ChartFigure,string ChartTitleCaption,string ChartXCaption,string ChartYCaption,
string []SeriesName,string []strValue,string [] Column,string GroupBy,string Name,string Group,DataTable dt,
int ChartWidth,int ChartHeight,bool IsPer,bool LinePer,double MaxNum,double MinNum)
{
//創建ChartSpace對象來放置圖表
OWC10.ChartSpaceClass Obj = new ChartSpaceClass();
Obj.Clear();
Obj.Refresh();
//在ChartSpace對象中添加圖表,Add方法返回chart對象
OWC10.ChChart ObjChart = Obj.Charts.Add(0);
//設置圖形的背景顏色
ObjChart.PlotArea.Interior.Color = "LightGoldenrodYellow";
/*
// 設置漸變色
ObjChart.PlotArea.Interior.SetPresetGradient(OWC10.ChartGradientStyleEnum.chGradientDiagonalDown,OWC10.
ChartGradientVariantEnum.chGradientVariantEdges,OWC10.ChartPresetGradientTypeEnum.chGradientGoldII);
*/
//設置邊框
ObjChart.Border.Color = "Blue";
//指定圖表的類型。類型由OWC.ChartChartTypeEnum枚舉值得到
switch(ChartFigure)
{
case "Line":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypeLine ;//.chChartTypeLineStacked; //Line
break;
case "Pie":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypePieExploded; //Pie
break;
case "Bar":
ObjChart.Type = OWC10.ChartChartTypeEnum.chChartTypeColumnStacked;
break;
default:
break;
}
//指定圖表是否需要圖例及圖例位置
ObjChart.HasLegend= true;
ObjChart.Legend.Position=ChartLegendPositionEnum.chLegendPositionRight;
//標題及相關屬性
ObjChart.HasTitle=true;
ObjChart.Title.Caption=ChartTitleCaption;
ObjChart.Title.Font.Bold=true;
ObjChart.Title.Font.Size=12;
ObjChart.Title.Font.Color="red";
ObjChart.Title.Font.Name="Arial";
//給定x,y軸的圖示說明
/*//如果是餅圖,則下面的代碼得註釋掉 */
if(ChartFigure!="Pie")
{
ObjChart.Axes[0].HasMajorGridlines = false;
ObjChart.Axes[0].HasTitle=true;
ObjChart.Axes[0].Title.Caption=ChartXCaption;
ObjChart.Axes[0].Title.Font.Bold=true;
ObjChart.Axes[0].Title.Font.Size=12;
ObjChart.Axes[0].Title.Font.Name="Arial";
ObjChart.Axes[1].HasMajorGridlines = false; //去除圖形上的刻度線
ObjChart.Axes[1].HasTitle=true;
ObjChart.Axes[1].Title.Caption=ChartYCaption;
ObjChart.Axes[1].Title.Font.Bold=true;
ObjChart.Axes[1].Title.Font.Size=12;
ObjChart.Axes[1].Title.Font.Name="Arial";
}
string strCategory = "";
double [] Sum = new double [SeriesName.Length]; //用來求百分比的
if(strValue.Length>0)
{
for(int j=0;j<strValue.Length;j++)
{
if(dt.Rows.Count>0)
{
for(int i=0;i<dt.Rows.Count;i++)
{
strValue [j] += dt.Rows[i][Column[j]].ToString()+ "\t";
if(j ==0)
{
strCategory += dt.Rows[i][GroupBy].ToString().Trim()+"\t";
}
}
}
}
}
if(!IsPer)
{
for(int j =0;j<strValue.Length;j++)
{
Regex regex = new Regex("\t");
string [] str = regex.Split(strValue[j]);
strValue[j] = "";
for(int i = 0;i<str.Length;i++)
{
if(str[i]!="")
{
strValue[j] +=str[i].ToString()+"\t";
}
}
}
}
else if(LinePer)//如果不需要使用這裡的求百分比的功能,通過該屬性來控制
{
for(int j =0;j<strValue.Length;j++)
{
Regex regex = new Regex("\t");
string [] str = regex.Split(strValue[j]);
strValue[j] = "";
for(int i = 0;i<str.Length;i++)
{
if(str[i]!="")
{
//per = Convert.ToDouble(str[i].ToString())/Sum[j];
strValue[j]+=Convert.ToDouble(str[i].ToString())/Sum[j]+"\t";
}
}
}
}
//設置坐標格式,最大值和最小值
//如果是餅圖或者百分比的圖,那么就不能給最大值加50000
if(ChartFigure!="Pie"&&!IsPer)
{
ObjChart.Axes[1].NumberFormat = "###,###";
ObjChart.Axes[1].Scaling.Maximum=MaxNum+MaxNum*0.1;
ObjChart.Axes[1].Scaling.Minimum=MinNum;
ObjChart.Axes[0].Font.Size = 12;
}
if(ChartFigure == "Line")
{
ObjChart.Axes[1].NumberFormat = "0%";
ObjChart.Axes[1].Scaling.Maximum=1;
ObjChart.Axes[1].Scaling.Minimum=0;
ObjChart.Axes[0].Font.Size = 12;
}
string SName = "";
string SValue = "";
for(int i =0;i<SeriesName.Length;i++)
{
//添加series
OWC10.ChSeries ser=ObjChart.SeriesCollection.Add(i);
SName = SeriesName[i];
SValue = strValue[i];
//ObjChart.SeriesCollection.Add(i);
ObjChart.SeriesCollection[i].TipText = ChartTitleCaption;
ObjChart.SeriesCollection[i].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames,(int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral,SName);
//給定分類
ObjChart.SeriesCollection[i].SetData(OWC10.ChartDimensionsEnum.chDimCategories,(int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral,strCategory);
//給定值
ObjChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues,(int)ChartSpecialDataSourcesEnum.chDataLiteral,SValue );
//給點來點效果
ObjChart.SeriesCollection[i].Marker.Style = ChartMarkerStyleEnum.chMarkerStyleTriangle;
ObjChart.SeriesCollection[i].Marker.Size = 5;
//Series上的數據標示
ObjChart.SeriesCollection[i].DataLabelsCollection.Add();
ObjChart.SeriesCollection[i].DataLabelsCollection[0].HasValue =true;
ObjChart.SeriesCollection[i].DataLabelsCollection[0].Font.Size = 12;
//如果是StackBar設置顏色
if(ChartFigure=="Bar")
{
ObjChart.SeriesCollection[i].Interior.Color = this.GetColor(i);
}
if(!IsPer)
{
ObjChart.SeriesCollection[i].DataLabelsCollection[0].NumberFormat = "#,###";
}
else
{
ObjChart.SeriesCollection[i].DataLabelsCollection[0].NumberFormat = "0.00%";
}
}
string fileName = "";
fileName = Name+DateTime.Now.ToString("yyyy/MM/dd:HH:mm:ss");
fileName = fileName.Replace( "-", "");
fileName = fileName.Replace( "/", "");
fileName = fileName.Replace( " ", "");
fileName = fileName.Replace( ":", "");
fileName = fileName.Replace( "PM", "");
fileName = fileName.Replace( "AM", "");
fileName = fileName.Replace( "年", "");
fileName = fileName.Replace( "月", "");
fileName = fileName.Replace( "日", "");
fileName = fileName.Replace( "上午", "");
fileName = fileName.Replace( "下午", "");
fileName += ".gif";
//string Path = Server.MapPath(".")+"\\"+DateTime.Now.ToString("yyyy/MM")+"\\"+Group;
string Path = "D:\\BI_Data\\INNOLUX_FILE\\IMAGE"+"\\"+DateTime.Now.ToString("yyyy/MM",format)+"\\"+Group;
PathInfo = "\\"+DateTime.Now.ToString("yyyy/MM",format)+"\\"+Group+"\\"+fileName;
DirectoryInfo mainInfo = new DirectoryInfo(Path);
string filePath ="";
if(mainInfo.Exists)
{
filePath = Path+"\\"+fileName;
Obj.ExportPicture(filePath, "GIF", ChartWidth, ChartHeight);
}
else
{
mainInfo.Create();
filePath = Path+"\\"+fileName;
Obj.ExportPicture(filePath, "GIF", ChartWidth, ChartHeight);
}
return PathInfo;
}
#endregion
}
}