zedgraph绘图(修改)

转自原文 zedgraph绘图(修改)

首先先下载 zedgraph.dll和zedgraph.web.DLL两个文件

添加项目并引用

首先添加一个用户控件 WebUserDrawGrap.ascx

html页面:


1

2

3

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserDrawGrap.ascx.cs" Inherits="CraigBlog.Net.zedGraph.WebUserDrawGrap" %>

<%@ Register TagPrefix="zgw" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %>

<ZGW:ZEDGRAPHWEB id="zedGraphControl" runat="server" width="600" Height="375" RenderMode="ImageTag"/>

代码

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 System.Drawing;
using ZedGraph;
using ZedGraph.Web;
using System.Collections.Generic;
namespace CraigBlog.Net.zedGraph
{
    /// <summary>
    /// 显示统计图形类型
    /// </summary>
    public enum AnalyticsType
    {
        Line,   //折线图
        Bar,    //柱状图
        Pie     //饼图
    };
    public partial class WebUserDrawGrap : System.Web.UI.UserControl
    {
        private List<Color> defaultColors = new List<Color>();/// 默认颜色种类

        private int Count;/// 统计的个数

        public string Title;/// 统计图的名称

        public string XAxisTitle;///横轴的名称(饼图不需要)

        public string YAxisTitle;/// 纵轴的名称(饼图不需要)

        public AnalyticsType Type;/// 显示的曲线类型:Line,Bar,Pie

        public List<PointPairList> DataSource = new List<PointPairList>();/// 折线图和柱状图的数据源

        public List<double> ScaleData = new List<double>();/// 饼图的数据源

        public List<Color> Colors = new List<Color>();/// 各段数据的颜色

        public List<string> NameList = new List<string>();/// 各段数据的名称

        public List<string> LabelList = new List<string>(); /// 用于柱状图,每个圆柱体表示的含义

        public List<double> ValueDouble = new List<double>();//用于定义柱形表示的值

        private void InitDefaultColors()
        {
            defaultColors.Add(Color.Red);
            defaultColors.Add(Color.Green);
            defaultColors.Add(Color.Blue);
            defaultColors.Add(Color.Yellow);
            defaultColors.Add(Color.YellowGreen);
            defaultColors.Add(Color.Brown);
            defaultColors.Add(Color.Aqua);
            defaultColors.Add(Color.Cyan);
            defaultColors.Add(Color.DarkSeaGreen);
            defaultColors.Add(Color.Indigo);
        }

        /// <summary>
        /// 如果属性为空则初始化属性数据
        /// </summary>
        private void InitProperty()
        {
            InitDefaultColors();
            if (string.IsNullOrEmpty(Title))
            {
                Title = "未命名统计图";
            }
            if (string.IsNullOrEmpty(XAxisTitle))
            {
                XAxisTitle = "横轴";
            }
            if (string.IsNullOrEmpty(YAxisTitle))
            {
                YAxisTitle = "纵轴";
            }
            if (Type == AnalyticsType.Pie)
            {
                Count = ScaleData.Count;
            }
            else
            {
                Count = DataSource.Count;
            }
            if (Colors.Count == 0 || Colors.Count != Count)
            {
                Random r = new Random();
                int tempIndex = 0;
                List<int> tempIndexList = new List<int>();
                for (int i = 0; i < Count; i++)
                {
                    tempIndex = r.Next(defaultColors.Count);
                    if (tempIndexList.Contains(tempIndex))
                    {
                        i--;
                    }
                    else
                    {
                        tempIndexList.Add(tempIndex);
                        Colors.Add(defaultColors[tempIndex]);
                    }
                }
            }
            if (NameList.Count == 0)
            {
                if (Type == AnalyticsType.Bar)
                {
                    for (int i = 0; i < DataSource[0].Count; i++)
                    {
                        NameList.Add("第" + i.ToString() + "组");
                    }
                }
                else
                {
                    for (int i = 0; i < Count; i++)
                    {
                        NameList.Add("第" + i.ToString() + "组");
                    }
                }
            }
            if (LabelList.Count == 0)
            {
                for (int i = 0; i < Count; i++)
                {
                    LabelList.Add("含义" + i.ToString());
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            zedGraphControl.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(zedGraphControl_RenderGraph);
        }
        /**/
        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="webObject"></param>
        /// <param name="g"></param>
        /// <param name="pane"></param>
        private void zedGraphControl_RenderGraph(System.Drawing.Graphics g, ZedGraph.MasterPane pane)
        {
            InitProperty();

            GraphPane myPane = pane[0];
            myPane.Title.Text = Title;
            myPane.XAxis.Title.Text = XAxisTitle;
            myPane.YAxis.Title.Text = YAxisTitle;

            switch (Type)
            {
                case AnalyticsType.Line:
                    DrawLine(myPane);
                    break;
                case AnalyticsType.Bar:
                    DrawBar(myPane);
                    break;
                case AnalyticsType.Pie:
                    DrawPie(myPane);
                    break;
                default:
                    break;
            }
            pane.AxisChange(g);
            System.IO.MemoryStream st = new System.IO.MemoryStream();
            myPane.GetImage().Save(st, System.Drawing.Imaging.ImageFormat.Jpeg);//得到图片流

            此处是得到该图片的图片流-可以将该流扩展到excel表格中(注意:需在项目目录中新建一个存放图片的文件夹ZedGraphImages)
        }

        #region Draw

        /// <summary>
        /// 画折线图
        /// </summary>
        /// <param name="graphPane"></param>
        private void DrawLine(GraphPane graphPane)
        {
            for (int i = 0; i < Count; i++)
            {
                graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None);
            }
            CreateBarLabels(graphPane, "f0", ValueDouble);
            graphPane.XAxis.Scale.TextLabels = NameList.ToArray();
            graphPane.XAxis.Type = AxisType.Text;
            graphPane.YAxis.Scale.MajorStep = 20;
            graphPane.YAxis.MinorGrid.IsVisible = true;
            graphPane.YAxis.MinorGrid.DashOff = 0;
            graphPane.YAxis.Title.FontSpec.Angle = 90;
            graphPane.YAxis.Title.FontSpec.FontColor = defaultColors[0];

        }

        /// <summary>
        /// 画柱状图
        /// </summary>
        /// <param name="graphPane"></param>
        private void DrawBar(GraphPane graphPane)
        {
            for (int i = 0; i < Count; i++)
            {
               graphPane.AddBar(LabelList[i], DataSource[i], Colors[i]).Bar.Fill = new Fill(Colors[i], Color.White, Colors[i]);

            }
            CreateBarLabels(graphPane, "f0", ValueDouble);
            graphPane.XAxis.MajorTic.IsBetweenLabels = true;
            string[] labels = NameList.ToArray();
            graphPane.XAxis.Scale.TextLabels = labels;//x轴的显示的文本集合
            graphPane.XAxis.Type = AxisType.Text;
            graphPane.XAxis.MajorGrid.IsVisible = false;//x轴栅格线是否可见
            graphPane.XAxis.MajorGrid.DashOff = 0;//栅格线的效果,同下
            graphPane.YAxis.Scale.BaseTic = 0;//刻度的初始开始值
            graphPane.YAxis.Scale.MajorStep = 20;//设置刻度的步进值
            graphPane.YAxis.MajorGrid.IsVisible = true; //栅格线是否可见
            graphPane.YAxis.MajorGrid.DashOff = 0;//设置的栅格线的效果。0表示为实线
            graphPane.YAxis.MajorGrid.PenWidth = 1;//设置栅格线的线条的宽度
            graphPane.YAxis.Title.FontSpec.Angle = 90;//设置标题的显示,顺时针旋转90度
            graphPane.Fill = new Fill(Color.White, Color.FromArgb(50, Color.Beige), 45.0f);
            graphPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);

        }
        /// <summary>
        /// 画饼图
        /// </summary>
        /// <param name="graphPane"></param>
        private void DrawPie(GraphPane graphPane)
        {
            graphPane.Fill = new Fill(Color.White, Color.Silver, 45.0f);
            graphPane.YAxis.IsVisible = false;
            graphPane.XAxis.IsVisible = false;
            graphPane.Chart.Fill.Type = FillType.None;
            graphPane.Legend.Position = LegendPos.Float;
            graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
            graphPane.Legend.FontSpec.Size = 16f;
            graphPane.Legend.IsHStack = false;
            for (int i = 0; i < Count; i++)
            {
                PieItem pieitme = graphPane.AddPieSlice(ScaleData[i], Colors[i], Color.Wheat, 45f, 0, NameList[i] + ScaleData[i]);
                pieitme.LabelType = PieLabelType.Percent;//设置显示的类型、Percent(百分比)
            }

        }

        /// <summary>
        /// 如果系统出错,显示错误信息
        /// </summary>
        /// <param name="graphPane"></param>
        /// <param name="message"></param>
        private void DrawMessage(GraphPane graphPane, string message)
        {
            TextObj text = new TextObj(message, 200, 200);
            text.Text = message;
            graphPane.GraphObjList.Add(text);

        }

        /// <summary>
        /// 为柱状图添加标签
        /// </summary>
        /// <param name="graphPane"></param>
        /// <param name="valueFormat"></param>
        /// <param name="valueDouble"></param>
        private void CreateBarLabels(GraphPane graphPane, string valueFormat, List<double> valueDouble)
        {
            for (int j = 0; j < valueDouble.Count; j++)
            {
                PointPair pt = new PointPair(j + 1, valueDouble[j]);
                TextObj text = new TextObj(pt.Y.ToString(valueFormat), pt.X, pt.Y>(double)10?pt.Y-10:pt.Y, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);
                text.ZOrder = ZOrder.A_InFront;
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1; //数值字体倾斜度
                text.FontSpec.Size = 16;
                text.FontSpec.FontColor = Color.Black;
                text.FontSpec.IsBold = true;
                text.Location.CoordinateFrame = CoordType.AxisXY2Scale;
                text.Location.AlignH = AlignH.Center;
                text.Location.AlignV = AlignV.Center;
                graphPane.GraphObjList.Add(text);
            }
        }
        #endregion

    }
}

--然后新建一个aspx页面:ZDrawGrap.aspx

将用户控件拖到页面

ZDrawGrap.aspx .cs程序如下:

代码

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 System.Collections.Generic;
using System.Drawing;
using ZedGraph;

namespace CraigBlog.Net.zedGraph
{
    public partial class ZDrawGrap : System.Web.UI.Page
    {
        Dictionary<string, int> dic = new Dictionary<string, int>(); //创建数据源

        protected void Page_Load(object sender, EventArgs e)
        {
            dic.Add("类别一", 20); dic.Add("类别二", 10); dic.Add("类别三", 25);
            dic.Add("类别四", 6); dic.Add("类别五", 13); dic.Add("类别六", 95);
            //柱状图
            DrawBar(DrawGrap1);
            //饼图
            DrawPie(DrawGrap2);
            //曲线图
            DrawLine(DrawGrap3);

        }
        private void DrawBar(WebUserDrawGrap DrawGrap1)
        {
            string Ytitle = "用户访问量";
            DrawGrap1.Type = AnalyticsType.Bar;
            DrawGrap1.Title = "用户访问柱状图";
            DrawGrap1.XAxisTitle = "类别";
            DrawGrap1.YAxisTitle = Ytitle;
           // DrawGrap1.YAxisTitle = "用\n户\n访\n问\n数\n量";//设置标题呈现的样式
            char[] ArrayChar = Ytitle.ToCharArray();
            DrawGrap1.YAxisTitle = ForeachChar(ArrayChar);
            ZedGraph.PointPairList list = new ZedGraph.PointPairList();
            for (int i = 0; i < dic.Count; i++)
            {
                KeyValuePair<string, int> keyPair = dic.ElementAt(i);
                list.Add((double)i, (double)keyPair.Value);//绘制柱形
                DrawGrap1.NameList.Add(ForeachChar(keyPair.Key.ToCharArray()));
                DrawGrap1.ValueDouble.Add((double)keyPair.Value);
            }
            DrawGrap1.LabelList.Add("Color Items");
            DrawGrap1.DataSource.Add(list);
        }

        private string ForeachChar(char[] array)
        {
            string temp = string.Empty;
            foreach (char item in array)
            {
                temp += item.ToString() +"\n";
            }
            return temp;
        }

        private void DrawPie(WebUserDrawGrap DrawGrap1)
        {
            DrawGrap1.Type = AnalyticsType.Pie;
            DrawGrap1.Title = "用户访问饼图";
            for (int i = 0; i < dic.Count; i++)
            {
                KeyValuePair<string, int> keyPair = dic.ElementAt(i);
                DrawGrap1.ScaleData.Add((double)keyPair.Value);
                DrawGrap1.NameList.Add(keyPair.Key);
            }
        }

        private void DrawLine(WebUserDrawGrap DrawGrap1)
        {
            DrawGrap1.Type = AnalyticsType.Line;
            DrawGrap1.Title = "用户访问曲线图";
            DrawGrap1.XAxisTitle = "类别";
            DrawGrap1.YAxisTitle = "用\n户\n访\n问\n数\n量"; //y轴标题竖着排
            ZedGraph.PointPairList list = new ZedGraph.PointPairList();
            for (int i = 0; i < dic.Count; i++)
            {
                KeyValuePair<string, int> keyPair = dic.ElementAt(i);
                list.Add(new PointPair((double)i,(double)keyPair.Value));
                DrawGrap1.ValueDouble.Add((double)keyPair.Value);
                DrawGrap1.NameList.Add(keyPair.Key);
            }
            DrawGrap1.LabelList.Add("Color Items");
            DrawGrap1.DataSource.Add(list);

        }
    }
}

时间: 2024-08-14 23:50:40

zedgraph绘图(修改)的相关文章

单行文本如何转换为多行文本?

我们在日常的CAD绘图工作中,常常会遇到现有的文字文本不满足新的CAD制图修改工作,需要再次进行编辑修改.例如,为了更加精准地绘图修改,我们常常需要把单行文本转换为多行文本.今天小编就给大家分享一些文本编辑的一些小方法.具体操作如下: 单行文本 在专业的制图软件--迅捷CAD编辑器专业版里操作如下: 1.点击单行文字编辑图标,按照相应的命令指示,我们输入一串单行文本文字. 2.或是直接在命令框里输入"DTEXT"命令字符:按照相应的命令指示,我们输入一串单行文本文字. 单行文本到多行文

学习力

学习力的基础 定义: 学习知识. 管理知识和 长久坚持的能力. 常见错误: 顺序式学习:按部就班地学习 缺乏整体概念 容易中途放弃 案例式学习 优点 目的性强 见效很快 成就感强 缺点 不成体系 容易遗忘 范围狭小 冲刺式学习 期末冲刺~!! 容易遗忘 很难学透 效率较低 学习力提升框架: 生理 锻炼 作息 规律 心理 道德许可 正向激励 方法论 刻意学习 三步快速学习法 知识体系的构建和管理 学习资料的搜索.判断和筛选 学习的基本过程 学习的八个步骤 方向 资料 资料获取 筛选 资料筛选 认知

CAD制图系列一之绘图、标注、修改、视图

笔记内容: 缩放.平移.键盘操作 绘图:直线.矩形 修改:删除.修剪.延时 标注:线型.对齐.半径.折弯.直径.角度 知识点 鼠标中键上下滚动 平移:先全部选中,然后点击中间的空格,随便移动 重点:空格的使用: 结束当前使用的命令 重复上一次使用的命令 删除的两种方式: 1)使用"删除"命令--修改里面-删除 2)使用Delete键--选中要删除的对象,使用键盘上的Delete键 框选技巧: 右下到左上:内部+交叉模式:只要有一点关系都会被选中 左上到右下:内部模式:只有全部在里面才有

如何修改CAD绘图线宽,多线样式管理器

如何修改CAD绘图线宽,多线样式管理器.今天小编就和大家简单探讨一下具体的操作步骤.具体演示步骤如下: 步骤一:多线样式管理器(M) 首先,我们先运行迅捷CAD编辑器专业版软件,点击"格式-多线样式管理器(M)",这时候会弹出弹窗. 步骤二:在多线样式管理器里操作以下2个步骤: 在"多线样式管理器"里,我们点击"standard"右侧的"新建"创建新的多线样式: 在"创建新多线样式"弹窗里,我们点击样式名命

ZedGraph控件的使用

http://blog.chinaunix.net/uid-20776117-id-1847015.html 在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状图都是很好的表现统计的直观形式.这个时候,ZedGraph控件给我们带来了极大的方便. 1.下载ZedGraph.dll 2.在项目中引用这个控件 (: 首先,在项目解决方案里添加,然后在工具箱中点击右键,选择项,COM,浏览,打开,我们就 会在工具箱的最下方发现这个控件了,确定.然后就可以在一个W

ZedGraph控件的使用 --归类(转帖)

在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状图都是很好的表现统计的直观形式.这个时候,ZedGraph控件给我们带来了极大的方便. 1.下载ZedGraph.dll 2.在项目中引用这个控件 (: 首先,在项目解决方案里添加,然后在工具箱中点击右键,选择项,COM,浏览,打开,我们就 会在工具箱的最下方发现这个控件了,确定.然后就可以在一个WINFORM中直接加入这个控件(在FORM上就出现了了图 了),具体的统计数据我们是在其添加X,Y坐标时候处理

php绘图(一)

绘图要单独有个php文件,图画好后,HTML里面引入 image src gd_info()查找gd库是否开启 //print_r(gd_info()); if(function_exists('gd_info')){ echo '<pre>'; print_r(gd_info()); }else{ echo '没有开启gd库'; } 如果没有开启进入phpini文件把 extension=php_gd2.dll 前面的分号可以去掉 imagecreate()创建画布 imagecolorll

Android自定义组件系列【12】——非UI线程绘图SurfaceView

一.SurfaceView的介绍 在前面我们已经会自定义View,使用canvas绘图,但是View的绘图机制存在一些缺陷. 1.View缺乏双缓冲机制. 2.程序必须重绘整个View上显示的图片,比较耗资源. 3.非UI线程无法更新View组件,所以会占用主线程资源,当需要在主线程中处理逻辑的时候会很慢. 在Android中为我们提供了一个SurfaceView来替代View实现绘制图形,一般在游戏绘图方面应用较广,所以如果是比较复杂的绘图建议使用SurfaceView. 二.SurfaceV

paper 139:qt超强绘图控件qwt - 安装及配置

qwt是一个基于LGPL版权协议的开源项目, 可生成各种统计图.它为具有技术专业背景的程序提供GUI组件和一组实用类,其目标是以基于2D方式的窗体部件来显示数据, 数据源以数值,数组或一组浮点数等方式提供, 输出方式可以是Curves(曲线),Slider(滚动条),Dials(圆盘),Compasses(仪表盘)等等,目前已经应用到许多工业领域,同时qwt也致力于3d的开发.但许多时候,qwt的安装配置难到了许多人,我曾经也在这里原地踏步很久,为了给大家开发方便,把我的安装经验分享给大家,避免