由于需要用,自己查一些资料,写的一个OWC画图表的方法,希望有用
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 生成图表
/// </summary>
/// <param name="strSeriesName">图表名称</param>
/// <param name="strCategory">类型</param>
/// <param name="strValue">值</param>
/// <param name="Caption">图标标题</param>
/// <param name="Type">类型</param>
/// <param name="Longth">图片长</param>
/// <param name="Width">图片宽</param>
public string GengerateChart(string strSeriesName, string strCategory, string strValue, string Caption, string Type, int Longth, int Width)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string chartSeriesName = strSeriesName;
string chartCategory = strCategory;
string chartValue = strValue;
string chartCaption = Caption;
string chartType = Type;
int chartLongth = Longth;
int chartWidth = Width;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建ChartSpace对象来放置图表
ChartSpace laySpace = new ChartSpaceClass();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//在ChartSpace对象中添加图表
ChChart InsertChart = laySpace.Charts.Add(0);
switch (chartType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "Pie"://饼状图
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InsertChart.Type = ChartChartTypeEnum.chChartTypePie;
InsertChart.HasLegend = true;
InsertChart.HasTitle = true;//为图表添加标题
InsertChart.Title.Caption = chartCaption;//标题名称
InsertChart.Title.Font.Color = "Blue";
InsertChart.Title.Font.Name = "黑体";
//指定图表是否需要图例标注
InsertChart.HasLegend = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "Line"://折线图
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;
//指定图表是否需要图例标注
InsertChart.HasLegend = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
InsertChart.HasTitle = true;//为图表添加标题
InsertChart.Title.Caption = chartCaption;//标题名称
InsertChart.Title.Font.Color = "Blue";
InsertChart.Title.Font.Name = "黑体";
}
break;
case "BarClustered"://条形图
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;
//指定图表是否需要图例标注
InsertChart.HasLegend = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
InsertChart.HasTitle = true;//为图表添加标题
InsertChart.Title.Caption = chartCaption;//标题名称
InsertChart.Title.Font.Color = "Blue";
InsertChart.Title.Font.Name = "黑体";
}
break;
case "Area"://面积图
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;
//指定图表是否需要图例标注
InsertChart.HasLegend = true;
InsertChart.HasTitle = true;//为图表添加标题
InsertChart.Title.Caption = chartCaption;//标题名称
InsertChart.Title.Font.Color = "Blue";
InsertChart.Title.Font.Name = "黑体";
}
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加一个series系列
InsertChart.SeriesCollection.Add(0);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//给定series系列的名字
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, chartSeriesName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//给定分类
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, chartCategory);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//给定值
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, chartValue);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (chartType == "Pie")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//增加数据值标签
InsertChart.SeriesCollection[0].DataLabelsCollection.Add();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
InsertChart.SeriesCollection[0].DataLabelsCollection[0].HasValue = true;
InsertChart.SeriesCollection[0].DataLabelsCollection[0].HasPercentage = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//输出文件.
string imageName = System.DateTime.Now.ToShortDateString().Replace("-", "").Trim() + System.DateTime.Now.ToLongTimeString().Replace(":", "").Trim();
imageName = imageName + System.DateTime.Now.Millisecond + "Chart.gif";
string strAbsolutePath = (Server.MapPath(".")) + "\\" + imageName;
laySpace.ExportPicture(strAbsolutePath, "GIF", chartLongth, chartWidth);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建GIF文件的相对路径.
string strRelativePath = "./" + imageName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//把图片添加到placeholder中,并在页面上显示
string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
//this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
return strRelativePath;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
效果图如下: