62,074
社区成员
发帖
与我相关
我的任务
分享
//饼状图,统计不同角色的用户数量的百分比
protected void Page_Load(object sender, EventArgs e)
{
//存储所有角色
List<string> roles = new List<string>();
//存储对应不同角色的用户数量
List<int> userquantity_uniquerole = new List<int>();
#region 数据库动态读出的数据赋给以上俩个List<int>变量
string query_roles_sql = "select distinct role from user";
try
{
MySqlConnection myConn = new MySqlConnection(conn_sql);
myConn.Open();
MySqlCommand myCommand = new MySqlCommand(query_roles_sql, myConn);
//myCommand.ExecuteNonQuery();
MySqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
roles.Add(myReader["role"].ToString());
userquantity_uniquerole.Add(getCount(myReader["role"].ToString()));
}
myReader.Dispose();
myCommand.Dispose();
myConn.Close();
}
catch (MySqlException ee)
{
Console.Write("<script>alert(MySQL connection error:'" + ee.Message.ToString() + "')</script>");
}
#endregion
string strDataName = "";
string strData = "";
//创建图表空间
ChartSpace mychartSpace = new ChartSpace();
mychartSpace.Border.Color = "White";
//在图表空间内添加一个图表对象
ChChart mychart = mychartSpace.Charts.Add(0);
//设置每块饼的数据
for (int i = 0; i < userquantity_uniquerole.Count(); i++)
{
strDataName += roles[i].ToString() + "\t";
strData += userquantity_uniquerole[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(".") + @"\chart\pie_chart.gif", "gif", 400, 400);
//加载图片
Image1.ImageUrl = "chart/pie_chart.gif" + "?temp=" + new Random().Next(1, 100) + "";
}
for (int j = 0; j < mychart.SeriesCollection[0].Points.Count; j++)
{
myChart.SeriesCollection[j].Interior.Color = myColor[j]; //这个myColor是颜色数组
}