第8章 图形化报表

报表作用:将繁杂的业务数据简单化
常用的图形化报表组件:Highcharts,水晶报表,jqChart,MSChart,XtraReports.
我们着重使用MSChart和Highcharts组件.

MSChart支持point(点图),Line(折线图),pie(饼图)等多种类型,其常用属性如下:
 Series:表示图表序列
 Titles:图表标题集合
 Width.Height图表控件的宽度和高度
 ImageType:图像类型(Png,Bmp,Jpeg,Emf)

示例1:统计学员C#考试成绩等级情况

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

namespace test
{
    public partial class Demo4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sql = @"select (case when intScore<60 then '不及格' when intScore<80 then '及格' when intScore<90 then '良' when intScore<=100 then '优秀' end) as Grade,count(*) as 'Count' from TblScore
                            group by (case when intScore<60 then '不及格' when intScore<80 then '及格' when intScore<90 then '良' when intScore<=100 then '优秀' end)";
            DataSet ds = SqlHelper.GetDataSet(sql);

        //为图表添加序列
            this.Chart1.Series.Clear();
            this.Chart1.Series.Add("Score");
        //设置序列的图表类型
            this.Chart1.Series["Score"].ChartType = SeriesChartType.Column;
            this.Chart1.BackColor = Color.Azure;
        //设置图表边框样式
            this.Chart1.BorderlineColor = Color.Green;
            this.Chart1.BorderlineWidth = 5;
            this.Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
            this.Chart1.Titles.Add("C#成绩统计图");

            this.Chart1.Series["Score"].LabelForeColor = Color.Gray;
            this.Chart1.Series["Score"].Font = new Font("宋体", 14);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
        //定义数据点
                DataPoint point = new DataPoint();
                point.YValues = new double[] { Convert.ToDouble(row["Count"]) };
        //设置每个数据点标签的文本值为百分比
                point.Label = string.Format("{0}人", Convert.ToDouble(row["Count"]));
                point.AxisLabel=row["Grade"].ToString();
        //将数据点添加到序列中
                this.Chart1.Series["Score"].Points.Add(point);
        //设置数据点被点击后的回发值,该值可以在Click事件的ImageMapEventArgs参数中获取
                point.PostBackValue = string.Format("{0} | {1}",row["Grade"],row["Count"]);
            }
        //将数据点标签显示到图形外侧
            Chart1.Series["Score"]["PieLabelStyle"]="Outside";
            //将第一个数据点展开
            this.Chart1.Series["Score"].Points[0]["Exploded"] = "true";
        }

        protected void Chart1_Click(object sender, ImageMapEventArgs e)
        {
            Response.Write(e.PostBackValue);
        }
    }
}

示例二:

 

posted @ 2013-12-22 12:26  mmww  阅读(150)  评论(0)    收藏  举报