前几天给一位朋友解决一个OWC的问题,开始一年难度倒是没有什么,就接下来了。第二天给她答复。待自已动手以后。才感到这个问题非常棘手。要求是生成"线—柱图",这个类型在OWC 的ChartChartTypeEnum枚举类型中是不存在的。这一下可傻了眼,感到无从下手了。不停的尝试,不停的找资料,哎,承诺的后果还真严重,折腾了近四个小时。终于有点眉目,不给整个Chart 对象指定类型,给表下的Series对象指定类型,先用一个Series对象画出柱图,然后再加入一个Series对象。指定类型为线型。问题解决。代码如下:
using
Microsoft.Office.Interop;
using
OWC
=
Microsoft.Office.Interop.Owc11;
public
partial
class
Default212 : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//创建ChartSpace对象来放置图表
OWC.ChartSpace objCSpace = new OWC.ChartSpaceClass();
![](/Images/OutliningIndicators/InBlock.gif)
//是否显示图例
objCSpace.HasChartSpaceLegend = true;
![](/Images/OutliningIndicators/InBlock.gif)
//在ChartSpace对象中添加图表,Add方法返回chart对象
OWC.ChChart objChart = objCSpace.Charts.Add(0);
![](/Images/OutliningIndicators/InBlock.gif)
//指定图为柱形图
![](/Images/OutliningIndicators/InBlock.gif)
//给定标题
objChart.HasTitle = true;
objChart.Title.Caption = "上半年分布图";
![](/Images/OutliningIndicators/InBlock.gif)
//给定x,y轴的图示说明
objChart.Axes[0].HasTitle = true;
objChart.Axes[0].Title.Caption = "Y : 数量";
objChart.Axes[1].HasTitle = true;
objChart.Axes[1].Title.Caption = "X : 月份";
![](/Images/OutliningIndicators/InBlock.gif)
//计算数据
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*categories 和 values 可以用tab分割的字符串来表示*/
string strSeriesName = "我的测试";
string strSeriesName1 = "LAR";
string strCategory = "1" + '\t' + "2" + '\t' + "3" + '\t' + "4" + '\t' + "5" + '\t' + "6" + '\t';
string strValue = "9" + '\t' + "8" + '\t' + "4" + '\t' + "10" + '\t' + "12" + '\t' + "6" + '\t';
string strValue1 = "0.038%" + '\t' + ".034%" + '\t' + ".039%" + '\t' + ".036%" + '\t' + ".040%" + '\t' + ".033%" + '\t';
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*柱形图*/
//添加一个series
objChart.SeriesCollection.Add(0);
objChart.SeriesCollection[0].Type = OWC.ChartChartTypeEnum.chChartTypeColumnClustered;
//给定series的名字
objChart.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimSeriesNames,
+(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
![](/Images/OutliningIndicators/InBlock.gif)
//给定分类
objChart.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimCategories,
+(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, strCategory);
![](/Images/OutliningIndicators/InBlock.gif)
//给定objChart[0]的值
OWC.ChSeries sDispDppm = objChart.SeriesCollection[0];
sDispDppm.SetData
(OWC.ChartDimensionsEnum.chDimValues,
(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, strValue);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*折线图*/
//再增加一个Series
OWC.ChSeries sDispLar = objChart.SeriesCollection.Add(0);
sDispLar.Ungroup(true);
OWC.ChAxis MyAxis = objChart.Axes.Add(sDispLar.get_Scalings(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues));
MyAxis.Position = OWC.ChartAxisPositionEnum.chAxisPositionRight;
MyAxis.HasMinorGridlines = false;
MyAxis.NumberFormat = "0.00%";
MyAxis.HasTitle = true;
MyAxis.Title.Caption = "报废率(%)";
//给定series的名字
sDispLar.SetData(OWC.ChartDimensionsEnum.chDimSeriesNames,
+(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName1);
![](/Images/OutliningIndicators/InBlock.gif)
//给定objChart[1]的值
sDispLar.SetData
(OWC.ChartDimensionsEnum.chDimValues,
(int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, strValue1);
![](/Images/OutliningIndicators/InBlock.gif)
//Ungroup series
sDispLar.Type = OWC.ChartChartTypeEnum.chChartTypeLineMarkers;
![](/Images/OutliningIndicators/InBlock.gif)
//输出成GIF文件.
string strAbsolutePath = (Server.MapPath(".")) + "\\Temp\\test.gif";
objCSpace.ExportPicture(strAbsolutePath, "GIF", 600, 350);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//创建GIF文件的相对路径.
string strRelativePath = "./Temp/test.gif";
![](/Images/OutliningIndicators/InBlock.gif)
//显示图片
Image1.ImageUrl = strRelativePath.ToString();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
}
转载于:https://www.cnblogs.com/sw22225458/archive/2008/04/08/1142932.html