asp.net 显示柱状图 饼状图
先来个饼状图的多样化:http://www.cnblogs.com/wengjinbao/articles/743594.html
Page_Load
protected void Page_Load(object sender, EventArgs e)
{
// //连接数据库并获取特定字符串
string strSeriesName = "图例1";
string ConnectString = "Server=(local);DataBase=web;Uid=sa;Pwd=123";
string Sql = "SELECT month,Allcount FROM Chart";
SqlConnection myConn = new SqlConnection(ConnectString);
myConn.Open();
SqlDataAdapter Da = new SqlDataAdapter(Sql, myConn);
DataSet ds = new DataSet();
Da.Fill(ds);
if (!Directory.Exists(Server.MapPath("Temp")))
{
Directory.CreateDirectory(Server.MapPath("Temp"));
}
string strAbsolutePath = Server.MapPath("Temp\\") + "test.gif";
string[] strTemp = new string[12];
int[] nTemp = new int[12];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strTemp[i] = ds.Tables[0].Rows[i][0].ToString();
nTemp[i] =Convert.ToInt32( ds.Tables[0].Rows[i][1].ToString());
}
CreateGif(strTemp, nTemp, PlaceHolder1,1, strAbsolutePath,strSeriesName);
}
CreateGif方法
protected void CreateGif(string[] strX, int[] nY, PlaceHolder SChart,int charttp, string strAbsolutePath, string strSeriesName)
{
//为x轴指定特定字符串,以便显示数据
string strXdata = String.Empty;
int nMax = 0;
foreach (string strData in strX)
{
strXdata += strData + "\t";
}
strXdata = strXdata.Remove(strXdata.Length - 1, 1);
//为y轴指定特定的字符串,以便与x轴相对应
string strYdata = String.Empty;
foreach (int nValue in nY)
{
if (nMax < nValue)
nMax = nValue;
strYdata = strYdata + nValue.ToString() + "\t";
}
strYdata = strYdata.Remove(strYdata.Length - 1, 1);
//创建ChartSpace对象来放置图表
ChartSpace laySpace = new ChartSpaceClass();
//在ChartSpace对象中添加图表
ChChart InsertChart = laySpace.Charts.Add(0);
//指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
if (charttp == 0)
{
InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图
}
else
{
InsertChart.Type = ChartChartTypeEnum.chChartTypePie;//饼图
}
//指定图表是否需要图例标注
if (charttp == 0)
{
InsertChart.HasLegend = false; //指定图表是否需要图例标注
InsertChart.HasTitle = true;//为图表添加标题
}
else
{
InsertChart.HasLegend = true;
InsertChart.HasTitle = true;
}
//为图表添加标题
if (charttp == 0)
{
InsertChart.Title.Caption = strSeriesName;//标题名称
}
else
{
InsertChart.Title.Caption = strSeriesName;
}
//为x,y轴添加图示说明
if (charttp == 0)
{
InsertChart.Axes[0].HasTitle = true;
InsertChart.Axes[0].Title.Caption = "月份";
InsertChart.Axes[1].HasTitle = true;
InsertChart.Axes[1].Scaling.Maximum = nMax + 10;
InsertChart.Axes[1].Title.Caption = "数量";
}
//添加一个series系列
InsertChart.SeriesCollection.Add(0);
//给定series系列的名字
if (charttp == 0)
{
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, "评价");
}
else
{
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, "评价");
}
//给定分类
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
//给定值
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
//输出文件.
laySpace.ExportPicture(strAbsolutePath, "GIF", 500, 375);//图的大小
//创建GIF文件的相对路径.
string strRelativePath = "./Temp" + strAbsolutePath.Substring(strAbsolutePath.LastIndexOf('\\'));
//把图片添加到placeholder中,并在页面上显示
string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
SChart.Controls.Add(new LiteralControl(strImageTag));
SChart.Dispose();
}
源文件
<body>
<form id="form1" runat="server">
<div>
<table style="width: 600px">
<tr>
<td colspan="3" style="height: 20px">
<strong>怎么样在ASP.NET2.0中使用OWC组件画图</strong></td>
</tr>
<tr>
<td colspan="3" style="height: 21px">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
</tr>
</table>
</div>
</form>
</body>