C# OWC生成图表
时间:2021-07-01 10:21:17
帮助过:8人阅读
1、如何安装OWC组件
OWC是Office Web Compents的缩写,即Microsoft的Office Web组件,包含SpreadSheet组件、Chart组件、PioTable组件和Data Source组件。
只要装了 Office 办公软件 ,在 C:\Program Files\MSECache\owc11_12 中会有一个安装文件: OWC11.msi (offic 2003)
2、安装完成后,新建一个工程,再添加引用...-->com--> Microsoft Office Web components 11.0 在bin文件夹中:Interop.OWC10.dll
引用空间:using Microsoft.Office.Interop.Owc11;
代码如下:
private void MakeLineChart()
{
//Y坐标轴
string[] DataName = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
//第一条曲线的数据
int[] Data = { 0, 10, 20, 100, 40, 50, 60 };
//第二条曲线的数据
int[] Data1 = { 100, 50, 41, 86 };
//第三条曲线的数据
int[] Data2 = { 10, 50, 100, 30, 50, 60 };
string strValue1 = "";
string strValue = "";
string strValue2 = "";
string strCateory = "";
//循环取得数据并格式化为OWC10需要的格式,(加'\t')
for (int i = 0; i < DataName.Length; i++)
{
strCateory += DataName[i] + '\t';
}
for (int i = 0; i < Data.Length; i++)
{
strValue += Data[i].ToString() + '\t';
}
for (int i = 0; i < Data1.Length; i++)
{
strValue1 += Data1[i].ToString() + '\t';
}
for (int i = 0; i < Data2.Length; i++)
{
strValue2 += Data2[i].ToString() + '\t';
}
OWC10.ChartSpaceClass mySpace = new OWC10.ChartSpaceClass();//创建ChartSpace对象来放置图表
OWC10.ChChart myChart = mySpace.Charts.Add(0);//在ChartSpace对象中添加图表,Add方法返回chart对象
myChart.Type = OWC10.ChartChartTypeEnum.chChartTypeColumnClustered;//指定图表的类型为线性图
myChart.HasLegend = true;//指定图表是否需要图例
myChart.HasTitle = true;//给定标题
myChart.Title.Caption = "交易曲线图"; //图表名称
//给定X\Y轴的图示说明
myChart.Axes[0].HasTitle = true;
myChart.Axes[0].Title.Caption = "数量"; //横轴名称
myChart.Axes[1].HasTitle = true;
myChart.Axes[1].Title.Caption = "日期"; //纵轴名称
//添加一个series(序列)
myChart.SeriesCollection.Add(0);
//给定series的名字
myChart.SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, "购买");
//给定series的分类
myChart.SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimCategories, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strCateory);
//给定具体值
myChart.SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimValues, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strValue);
//添加一个series
myChart.SeriesCollection.Add(1);
//给定series的名字
myChart.SeriesCollection[1].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, "出售");
//给定series的分类
myChart.SeriesCollection[1].SetData(OWC10.ChartDimensionsEnum.chDimCategories, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strCateory);
//给定具体值
myChart.SeriesCollection[1].SetData(OWC10.ChartDimensionsEnum.chDimValues, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strValue1);
//添加一个series
myChart.SeriesCollection.Add(2);
//给定series的名字
myChart.SeriesCollection[2].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, "总成交");
//给定series的分类
myChart.SeriesCollection[2].SetData(OWC10.ChartDimensionsEnum.chDimCategories, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strCateory);
//给定具体值
myChart.SeriesCollection[2].SetData(OWC10.ChartDimensionsEnum.chDimValues, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strValue2);
//输出成GIF文件
string strAbsolutePath = (Server.MapPath(".")) + @"\Images\tempChart.gif";
Response.Write(strAbsolutePath);
mySpace.ExportPicture(strAbsolutePath, "GIF", 300, 300); //输出图表
//创建GIF文件的相对路径
string strRelativePath = "Images/tempChart.gif";
//把图片添加到Image
Image1.ImageUrl = strRelativePath;
}
//图表类型枚举
OWC11.ChartChartTypeEnum[] chartTypes = new OWC11.ChartChartTypeEnum[]{
ChartChartTypeEnum.chChartTypeColumnClustered,
ChartChartTypeEnum.chChartTypeColumn3D,
ChartChartTypeEnum.chChartTypeBarClustered,
ChartChartTypeEnum.chChartTypeBar3D,
ChartChartTypeEnum.chChartTypeArea,
ChartChartTypeEnum.chChartTypeArea3D,
ChartChartTypeEnum.chChartTypeDoughnut,
ChartChartTypeEnum.chChartTypeLineStacked,
ChartChartTypeEnum.chChartTypeLine3D,
ChartChartTypeEnum.chChartTypeLineMarkers,
ChartChartTypeEnum.chChartTypePie,
ChartChartTypeEnum.chChartTypePie3D,
ChartChartTypeEnum.chChartTypeRadarSmoothLine,
ChartChartTypeEnum.chChartTypeSmoothLine};
string[] chartTypesCh = new string[] { "垂直柱状统计图", "3D垂直柱状统计图", "水平柱状统计图", "3D水平柱状统计图", "区域统计图", "3D区域统计图", "中空饼图", "折线统计图", "3D折线统计图", "折线带点统计图", "饼图", "3D饼图", "网状统计图", "弧线统计图" };