OWC控件的使用学习札记(三)
线性图示例:
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;
}
}
}
补充:Web开发 , ASP.Net ,