MsChart使用经历记录
最近要做统计,找了半天找到了MSCHART,资料很少,但感觉用起来简单,所以就用他了.
这是第一个找到的资料 http://www.cnblogs.com/Nazarite/archive/2010/06/19/1760974.html ,相同的我就不去写了.
--前台代码--
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="glory3.aspx.cs" Inherits="Admini_zzz_glory3" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Chart ID="Chart1" runat="server" OnPreRender="Chart1_PreRender" Width="500" Height="300">
<Series>
<asp:Series Name="Default" ChartType="Pie"> //默认是饼图
</asp:Series>
</Series>
<Legends>
<asp:Legend BackColor="Transparent" Alignment="Center" Docking="Bottom" Font="Trebuchet MS, 8.25pt, style=Bold"
IsTextAutoFit="False" Name="Default" LegendStyle="Table"> //图例
</asp:Legend>
</Legends>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" Area3DStyle-Enable3D="false">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
<asp:DropDownList ID="DropDownList1" runat="server" Style="width: 100px"
AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="Pie">饼型</asp:ListItem>
<asp:ListItem Value="Column">柱状型</asp:ListItem>
<asp:ListItem Value="Area">区域型</asp:ListItem>
<asp:ListItem Value="Bar">栏栅型</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
--后台代码--
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;
using System.Data.OracleClient;
using BLL;
using Model;
using System.Collections.Generic;
using System.Drawing;
public partial class Admini_zzz_glory3 : System.Web.UI.Page
{
private OracleConnection con;
Random _rnd = new Random();
protected void Page_Load(object sender, EventArgs e)
{
ConnectConn c = new ConnectConn();
con = c.Connect();
if (!IsPostBack == true)
{
BindData();
}
}
protected void Chart1_PreRender(object sender, EventArgs e)
{
string[] str;
for (int i = 0; i < Chart1.Series["Default"].Points.Count; i++)
{
str = (i == Chart1.Series["Default"].Points.Count - 1 & Chart1.Series["Default"].Points.Count > 8) ? this.SetColor(1).Split(',') : str = SetColor(i).Split(',');
//这里的150是ALPHA值,让图例半透明,255就是不透明了.
Chart1.Series["Default"].Points[i].Color = System.Drawing.Color.FromArgb(150, Convert.ToInt16(str[0]), Convert.ToInt16(str[1]), Convert.ToInt16(str[2]));
Chart1.Series["Default"].BorderColor = System.Drawing.Color.FromArgb(35, 30, 30, 30);
}
}
private void BindData()
{
DataTable _dt = DepartmentManager.GetSchoolTypes(con, (Session["Employee"] as Employee).Department.Id, "");
double _sum = 0;
Dictionary<string, string> _dic = new Dictionary<string, string>();
for (int j = 0; j < _dt.Rows.Count; j++)
{
DataRow _dr = _dt.Rows[j];
_sum += Convert.ToDouble(_dt.Rows[j]["COUNT"].ToString());
_dic.Add(_dt.Rows[j]["NAME"].ToString(), _dt.Rows[j]["COUNT"].ToString());
}
int[] yValues = {
Calc(_dic,"中心校",_sum),
Calc(_dic,"完全小学",_sum),
Calc(_dic,"村小",_sum),
Calc(_dic,"完全中学",_sum),
Calc(_dic,"高级中学",_sum),
Calc(_dic,"初级中学",_sum),
Calc(_dic,"九年一贯制学校",_sum),
Calc(_dic,"十二年一贯制学校",_sum),
};
string[] xValues = { "中心校", "完全小学", "村小", "完全中学", "高级中学", "初级中学", "九年一贯制学校", "十二年一贯制学校" };
Chart1.Series["Default"].Points.DataBindXY(xValues, yValues); //这里可以选择DataBindXY(),或绑定到DataSource,然后设置它的X和Y ValueMember就行
for (int i = 0; i < Chart1.Series["Default"].Points.Count; i++)
{
Chart1.Series["Default"].LabelForeColor = Color.Black;
if (Chart1.Series[0].ChartType == System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie)
{
if (yValues[i] != 0)
{
Chart1.Series["Default"].Points[i].Label = yValues[i].ToString();
}
else
{
Chart1.Series["Default"].Points[i].Label = " ";
}
}
else
{
Chart1.Series["Default"].Points[i].Label = yValues[i].ToString();
Chart1.Series["Default"].LabelForeColor = Color.White;
}
Chart1.Series["Default"].Points[i].LegendText = xValues[i].ToString();
}
Chart1.Series["Default"].CustomProperties = "PointWidth=0.3"; //这里是固定柱状图的宽度,如果只有2根柱,则会变得非常胖.设置这个就好了.
//如果X轴上的 数据太多,被自动省略了之后,加入如下代码则可显示全部
Chart1.ChartAreas[0].AxisX.Interval = 1; //设置X轴坐标的间隔为1
Chart1.ChartAreas[0].AxisX.IntervalOffset = 1; //设置X轴坐标偏移为1
Chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true; //设置是否交错显示,比如数据多的时间分成两行来显示
}
protected string SetColor(int no)
{
int no1 = no % 8;
string str = "";
if (no1 == 0)
str = "220,54,7";
else if (no1 == 1)
str = "55,130,238";
else if (no1 == 2)
str = "252,172,55";
else if (no1 == 3)
str = "99,189,94";
else if (no1 == 4)
str = "184,88,58";
else if (no1 == 5)
str = "3,89,136";
else if (no1 == 6)
str = "255,0,255";
else if (no1 == 7)
str = "117,235,239";
return str;
}
private int Calc(Dictionary<string, string> _dic, string p_key, double p_sum)
{
try
{
return Convert.ToInt32(Convert.ToDouble(_dic[p_key]) / p_sum * 100); //计算每种学校占总学校数的百分比
}
catch
{
return 0;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Chart1.Series[0].ChartType = (System.Web.UI.DataVisualization.Charting.SeriesChartType)Enum.Parse(typeof(System.Web.UI.DataVisualization.Charting.SeriesChartType),
DropDownList1.SelectedValue.Trim()); //切换图例
BindData();
}
}



浙公网安备 33010602011771号