ASP.NET ServerSide Charting With OWC11——饼状图

     今天做的是ASP.NET饼状报表. 之前想用VS自带的MSChart控件来绘制,但是用MSChart制作不是很方便(项目中),不过有MSChart绘制的示例,希望大家能够指点下。   而且也就是因为这,所以我发现了一个绘制饼状图更好的插件: OWC;大家可以从http://www.codeproject.com/KB/aspnet/owc11article.aspx下载插件. (试了一下,PC上面如果安装了Office 版本是2003的话,必须安装这个插件)。

    好了,不罗嗦了 ,直接粘贴关键源码吧:

   

public void CreatePinReport()
{
//创建之前删除生成的文件
string deleteFileName = "stat.gif";//要删除的文件名称
try
{
string[] rootDirs = Directory.GetDirectories("http://images.cnblogs.com/"); //当前目录的子目录:
string[] rootFiles = Directory.GetFiles("http://images.cnblogs.com/"); //当前目录下的文件:

foreach (string s2 in rootFiles)
{
if (s2.Contains(deleteFileName))
{
Console.WriteLine(s2);
File.Delete(s2);
//删除文件
}
}
}
catch (Exception)
{

}






//创建X坐标的值,表示月份

string[] month = GetNop_DictionaryWineType();
//创建Y坐标的值,表示销售额
 List<double> list = new List<double>();
            
            for (int i = 0; i < GetNop_DictionaryValueWineType().Length; i++)
            {
                //count = GetSumOrderByProductCategoryID(Convert.ToInt32(GetNop_DictionaryValueWineType()[i]));
                list.Add(GetSumOrderByProductCategoryID(Convert.ToInt32(GetNop_DictionaryValueWineType()[i])));
            }
            double[] count = list.ToArray();

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]+ "\t";
strData
+= count[i] + "\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("../images/") + @"stat.gif", "gif", 700, 400);
//加载图片
this.ig.ImageUrl = "http://images.cnblogs.com/stat.gif";
}

以下是代码显示效果图:

代码很简单,而且有详细的注释,希望大家多多指教..


作者:Stephen-kzx
出处:http://www.cnblogs.com/axing/
公众号:会定时分享写工作中或者生活中遇到的小游戏和小工具源码。有兴趣的帮忙点下关注!感恩!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/axing/p/OWC11_ASPNET.html