OWC(Microsoft Office Web Components)是 Microsoft Office 使用的数据绑定 ActiveX 控件,安装office就有了,其实也可以单独安装的(google下载),使用OWC做了一个饼状图和柱状图,感觉比较简单,开发难度小,效率比较高,OWC的主要原理是按照所传入的数据生成相应的图片,然后开发人员将该图片加载到相应的位置即可实现效果,个人感觉生成的图比较粗糙,但是真的封装的很好,只需按照函数要求输入数据就OK了,可以设置图片的背景色,字体,大小等等。。。。。
OWC提供面积图、柱状图、条形图、折线图、平滑曲线图、饼图、圆环图、股价图、散点图、雷达图。
在vs2005/2008里面开发的时候先要右键--》添加引用--》Com-》Microsoft Office Web components 11(这是安装了office2007,2003好像直接叫Microsoft OWc 什么的)
饼状图:
代码:
protected void pieBtn_Click(object sender, EventArgs e)
{
//创建X坐标的值,表示月份
int[] month ={ 1, 2, 3 };
//创建Y坐标的值,表示销售额
double[] count ={ 120, 240, 220 };
string strDataName = "";
string strData = "";
//创建图表空间
ChartSpace mychartSpace = new ChartSpace();
mychartSpace.Border.Color = "White";
//在图表空间内添加一个图表对象
ChChart mychart = mychartSpace.Charts.Add(0);
//设置每块饼的数据
for(int i=0;i<count.Length;i++)
{
strDataName += month[i].ToString()+ "\t";
strData += count[i].ToString()+ "\t";
}
//设置图表类型,本例使用饼
mychart.Type = ChartChartTypeEnum.chChartTypePie;
//设置图表的一些属性
//是否需要图例
mychart.HasLegend = true;
//是否需要主题
mychart.HasTitle = true;
//主题内容
mychart.Title.Caption ="饼状图测试";
mychart.Title.Font.Size = 10;
mychart.Title.Font.Bold = false;
mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
mychart.Legend.Interior.Color = "f9f9f9";
mychart.Legend.Font.Size = 9;
mychart.Legend.Border.Color = "White";
//添加图表块
mychart.SeriesCollection.Add(0);
//设置图表块的属性
//分类属性
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,
(int)ChartSpecialDataSourcesEnum.chDataLiteral, strDataName);
//值属性
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues,
(int)ChartSpecialDataSourcesEnum.chDataLiteral, strData);
for (int j = 0; j < mychart.SeriesCollection[0].Points.Count; j++)
{
mychart.SeriesCollection[0].Points[j].Border.Color = "White";
}
//显示百分比
ChDataLabels mytb = mychart.SeriesCollection[0].DataLabelsCollection.Add();
mytb.HasPercentage = true;
//mytb.Border.Color = "White";
mytb.HasValue = true;
//生成图片
mychartSpace.ExportPicture(Server.MapPath(".") + @"\stat.gif", "gif", 700, 400);
//加载图片
this.ig.ImageUrl = "stat.gif" + "?temp=" + new Random().Next(1, 100) + ""; ;
}
柱状图:
代码
protected void columnarBtn_Click(object sender, EventArgs e)
{
int[] month ={ 1, 2, 3 };
//创建Y坐标的值,表示销售额
double[] count ={ 120, 240, 220 };
//创建图表空间
ChartSpace mychartSpace = new ChartSpace();
//在图表空间内添加一个图表对象
ChChart mychart = mychartSpace.Charts.Add(0);
mychartSpace.Border.Color = "White";
//设置图表类型,本例使用柱形
mychart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;
//设置图表的一些属性
//是否需要图例
mychart.HasLegend = true;
//是否需要主题
mychart.HasTitle = true;
//主题内容
mychart.Title.Caption ="柱状图测试";
mychart.Title.Font.Size = 10;
mychart.Title.Font.Bold = false;
mychart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
mychart.Legend.Interior.Color = "f9f9f9";
mychart.Legend.Font.Size = 9;
mychart.Legend.Border.Color = "White";
//设置x,y坐标
mychart.Axes[1].HasTitle = true;
mychart.Axes[1].Title.Caption = "坐标";
mychart.Axes[1].Title.Font.Size = 9;
for (int i = 0; i < count.Length; i++)
{
mychart.SeriesCollection.Add(i);
mychart.SeriesCollection[i].Caption = count[i].ToString();
mychart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, month[i]);
mychart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, count[i]);
}
mychartSpace.ExportPicture(Server.MapPath(".") + @"\stat1.gif", "gif", 700, 400);
//加载图片
this.ig.ImageUrl = "stat1.gif" + "?temp=" + new Random().Next(1, 100) + ""; ;
}
这样子就已经OK了,可以按照自己的实际情况传入相应的数据即可,也可以按照自己的喜好来设置颜色字体等等

OWC是Microsoft Office提供的数据绑定ActiveX控件,用于生成图表。通过OWC,开发者可以方便地创建饼状图和柱状图,尽管图像质量可能不高,但其封装良好,开发简单高效。在Visual Studio 2005/2008中,需要添加引用Microsoft Office Web Components 11来使用。文章介绍了如何使用OWC生成不同类型的图表。
&spm=1001.2101.3001.5002&articleId=6668743&d=1&t=3&u=64ddfb768d0d4a61ad23325e4ed8299b)
1万+

被折叠的 条评论
为什么被折叠?



