打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
asp.net统计图控件dotnetCHARTING用法

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;
using dotnetCHARTING;


public partial class tongjitu2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

      if(!isPostBack)

        {

          Drawing();

         }
     }

private void Drawing()
    {
        Charting c = new Charting();
        c.Title = "商品价格走势图";
        c.XTitle = "月份";
        c.YTitle = "价格(元)";
        c.PicHight = 350;
        c.PicWidth = 650;
        c.SeriesName = "合计";//仅对于DataTable类型做数据源时,此属性有效
        c.PhaysicalImagePath = "ChartImages";//统计图片存放的文件夹名称,缺少对应的文件夹生成不了统计图片
        c.FileName = "Statistics";
        c.Type = SeriesType.Spline;//折线型
        c.Use3D = false;
        c.DataSource = GetDataSource();
        c.CreateStatisticPic(this.Chart1);

    }

    private DataTable GetDataSource()
    {
        DataTable MyTable = new DataTable();
        try
        {
            SqlConnection conn = new SqlConnection("server=.;database=demo;uid=sa;pwd=;");
            string sqlstr = "select month ,xiaoliang from t_result ";
            conn.Open();
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sqlstr;
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, conn);
            sqlda.Fill(ds);
            if (ds != null && ds.Tables.Count > 0)
            {
                MyTable = ds.Tables[0];
            }
            return MyTable;

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            throw;
        }
    }


}

==============================================================================

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using dotnetCHARTING;

/// <summary>
/// Charting的摘要说明
/// </summary>
public class Charting
{
    protected string _phaysicalimagepath;
    protected string _title;
    protected string _xtitle;
    protected string _ytitle;
    protected string _seriesname;
    protected int _picwidth;
    protected int _pichight;
    protected SeriesType _type;
    protected bool _use3d;
    protected DataTable _dt;
    protected string _filename;

public Charting()
{
   //
   // TODO: 在此处添加构造函数逻辑
   //
}
        /// <summary>
        /// 图片存放路径
        /// </summary>
        public string PhaysicalImagePath
        {
            set { _phaysicalimagepath = value; }
            get { return _phaysicalimagepath; }
        }
        /**/
        /// <summary>
        /// 图片标题
        /// </summary>
        public string Title
        {
            set { _title = value; }
            get { return _title; }
        }
        /**/
        /// <summary>
        /// 图片x座标名称
        /// </summary>
        public string XTitle
        {
            set { _xtitle = value; }
            get { return _xtitle; }
        }
        /**/
        /// <summary>
        /// 图片y座标名称
        /// </summary>
        public string YTitle
        {
            set { _ytitle = value; }
            get { return _ytitle; }
        }

        /**/
        /// <summary>
        /// 图例名称
        /// </summary>
        public string SeriesName
        {
            set { _seriesname = value; }
            get { return _seriesname; }
        }
        /**/
        /// <summary>
        /// 图片宽度
        /// </summary>
        public int PicWidth
        {
            set { _picwidth = value; }
            get { return _picwidth; }
        }
        /**/
        /// <summary>
        /// 图片高度
        /// </summary>
        public int PicHight
        {
            set { _pichight = value; }
            get { return _pichight; }
        }

        /// <summary>
        /// 统计图类型(柱形,线形等)
        /// </summary>
        public SeriesType Type
        {
            set { _type = value; }
            get { return _type; }
        }

        /// <summary>
        /// 是否将输出的图片显示成三维
        /// </summary>
        public bool Use3D
        {
            set { _use3d = value; }
            get { return _use3d; }
        }

        /// <summary>
        /// 对比图形数据源
        /// </summary>
        public DataTable DataSource
        {

            set { _dt = value; }
            get { return _dt; }
        }

        /// <summary>
        /// 生成统计图片的名称
        /// </summary>
        public string FileName
        {
            set { _filename = value; }
            get { return _filename; }
        }

        /// <summary>
        /// 生成单一统计图片
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="type">图形类别,如柱状,折线型</param>
        public void CreateStatisticPic(dotnetCHARTING.Chart chart)
        {
            chart.Title = this.Title;
            chart.XAxis.Label.Text = this.XTitle;
            chart.YAxis.Label.Text = this.YTitle;
            chart.TempDirectory = this.PhaysicalImagePath;
            chart.FileManager.FileName = this.FileName;
            chart.Width = this.PicWidth;
            chart.Height = this.PicHight;
            chart.Type = ChartType.Combo;
            chart.Series.Type = this.Type;
            chart.Series.Name = this.SeriesName;
            chart.Series.Data = this.DataSource;
            chart.SeriesCollection.Add();
            chart.DefaultSeries.DefaultElement.ShowValue = true;
            chart.ShadingEffect = true;
            chart.Use3D = this.Use3D;
            chart.Series.DefaultElement.ShowValue = true;
        }
}

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing;

/// <summary>
/// DrawingCurve 的摘要说明
/// </summary>
public class DrawingCurve
{
   
    public int intXLong = 800;   //图片大小 长
    public int intYLong = 600;   //图片大小 高
    public int intXMultiple = 1;    //零刻度的值 X
    public int intYMultiple = 0;    //零刻度的值 Y
    public int intXMax = 12;    //最大刻度(点数) X
    public int intYMax = 30;    //最大刻度(点数) Y

    public int intLeft = 50;   //左边距
    public int intRight = 120; //右边距
    public int intTop = 30;    //上边距
    public int intEnd = 50;    //下边距

    public string strXText = "时间(单位:月)";    //单位 X
    public string strYText = "数量(单位:个)";    //单位 Y
    public string strTitle = "趋势线图";    //标题
    public DataTable tbData;    //要统计的数据


    private int intXScale = 30;    //一刻度长度 X
    private int intYScale = 30;    //一刻度高度 Y
    //private int intX = 0;   //0点 X坐标
    //private int intY = 0;   //0点 Y坐标
    public int intData = 0;    //记录数

    public DrawingCurve()
    {

        intXScale = (intXLong - intLeft - intRight) / (intXMax + 1);//一刻度长度 X
        intYScale = (intYLong - intTop - intEnd) / (intYMax + 1);//一刻度高度 Y

        //intX = intXLong - intLeft;   //0点 X坐标
        //intY = intYLong - intEnd;   //0点 Y坐标
    }

    public Bitmap DrawingImg()
    {

        Bitmap img = new Bitmap(intXLong, intYLong); //图片大小
        Graphics g = Graphics.FromImage(img);
        g.Clear(Color.Snow);
        g.DrawString(strTitle, new Font("宋体", 14), Brushes.Black, new Point(5, 5));
        g.DrawLine(new Pen(Color.Black, 2), intLeft, intYLong - intEnd, intXLong - intRight, intYLong - intEnd); //绘制横向
        g.DrawLine(new Pen(Color.Black, 2), intLeft, intTop, intLeft, intYLong - intEnd);   //绘制纵向

        //绘制纵坐标
        g.DrawString(strYText, new Font("宋体", 12), Brushes.Black, new Point(intLeft, intTop));//Y 单位
        Point p1 = new Point(intLeft - 10, intYLong - intEnd);
        for (int j = 0; j <= intYMax; j++)
        {
            p1.Y = intYLong - intEnd - j * intYScale;
            Point pt = new Point(p1.X + 10, p1.Y);
            //绘制纵坐标的刻度和直线
            g.DrawLine(Pens.Black, pt, new Point(p1.X + 15, p1.Y));
            //绘制纵坐标的文字说明
            g.DrawString(Convert.ToString(j + intYMultiple), new Font("宋体", 12), Brushes.Black, new Point(p1.X - 25, p1.Y - 8));
        }

        //绘制横坐标
        g.DrawString(strXText, new Font("宋体", 12), Brushes.Black, new Point(intXLong - intRight, intYLong - intEnd));//X 单位
        Point p = new Point(intLeft, intYLong - intEnd);
        for (int i = 0; i < intXMax; i++)
        {
            p.X = intLeft + i * intXScale;
            //绘制横坐标刻度和直线
            g.DrawLine(Pens.Black, p, new Point(p.X, p.Y - 5));
            //绘制横坐标的文字说明
            g.DrawString(Convert.ToString(i + intXMultiple), new Font("宋体", 12), Brushes.Black, p);
        }


        intData = tbData.Rows.Count;
        if (intData > 0)
        {
            //趋势线图
            for (int i = 0; i < intData - 1; i++)
            {
                DataRow Row1 = tbData.Rows[i];
                DataRow Row2 = tbData.Rows[i + 1];
                //定义起点
                Point rec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row1[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row1[1].ToString()) - intYMultiple) * intYScale));
                //定义终点
                Point dec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row2[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row2[1].ToString()) - intYMultiple) * intYScale));
                //绘制趋势折线
                g.DrawLine(new Pen(Color.Red), rec, dec);
            }
        }

        return img;
    }

    //转换数字
    private double TurnNumber(string str)
    {
        double dubReturn;
        try
        {
            dubReturn = Convert.ToDouble(str);
        }
        catch
        {
            dubReturn = 0;
        }
        return dubReturn;

    }

}

----------------------------------调用------------------------------------------------

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable MyTable = new DataTable();
       

        //string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/db.mdb");
        //string cmdstr = "SELECT 月份,销量 FROM Result";
        //OleDbConnection con = new OleDbConnection(constr);
        //con.Open();
        //OleDbCommand cmd = new OleDbCommand(cmdstr, con);
        //OleDbDataAdapter da = new OleDbDataAdapter(cmdstr, con);
        //da.Fill(MyTable);

      //sql数据库
        try
        {
            SqlConnection conn = new SqlConnection("server=.;database=demo;uid=sa;pwd=;");
            string sqlstr = "select month ,xiaoliang from t_result ";
            conn.Open();
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sqlstr;
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, conn);
            sqlda.Fill(ds);
            if (ds != null && ds.Tables.Count > 0)
            {
                MyTable = ds.Tables[0];
            }
          
           
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            throw;
        }

        DrawingCurve MyDc = new DrawingCurve();
        MyDc.tbData = MyTable;

        Bitmap img = new Bitmap(100, 100);
        img = MyDc.DrawingImg();
        Graphics g = Graphics.FromImage(MyDc.DrawingImg());

        //显示图形
        img.Save(Response.OutputStream, ImageFormat.Jpeg);;
        g.Dispose();
        Response.Write("<br>" + MyDc.intData.ToString());
    }



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
asp.net常用javascript封装代码
省市级联(framework3.5以上可用)用户控件
CarlosAg.ExcelXmlWriter实现.net下Excel导出功能(服务端不需要安装Excel程序)
C#数据库操作类
asp.net错误处理封装
发送邮件的方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服