使用ZedGraph画曲线柱状图-程序员宅基地

技术标签: c#  ui  数据库  

   刚接触到ZedGraph,到网上搜素到的方法基本上都是使用临时文件来存储图片,然后再显示,但是临时图片太多的话会占用大量的空间。很不划算。最后看到有人说把RenderMode="RawImage"就可以了 ,但是会出现乱码。如何解决呢?下面是我的方法。

    新建一个目录,命名为bin,把文件ZedGraph.Web.dll,ZedGraph.dll拷到bin目录下面。

建立文件tuppian.aspx。其内容为:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="tuppian.aspx.cs" Inherits="tuppian" %>

<%@ Register assembly="ZedGraph.Web" namespace="ZedGraph.Web" tagprefix="cc1" %>

<%--

特别注意了:本页面不要有HTML代码,和asp.net代码。不然会出现乱码,RenderMode="RawImage"一定要设置RawImage,不然会报错。

--%>

<cc1:ZedGraphWeb ID="ZedGraphWeb1" runat="server" RenderMode="RawImage">

</cc1:ZedGraphWeb>

tuppian.aspx.cs
为:

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.Drawing; 
using ZedGraph; 
using ZedGraph.Web; 

public partial class tuppian : System.Web.UI.Page 

    DarwGrapClass dg = new DarwGrapClass(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 
    protected override void OnInit(EventArgs e) 
    { 
        InitializeComponent(); 
        base.OnInit(e); 
    } 

    private void InitializeComponent() 
    { 
        string id = Request.QueryString["id"]; 
        switch (id) 
        { 
            case "1": 
                DrawLine(); 
                break; 
            case "2": 
                DrawPie(); 
                break; 
            default: 
                DrawBar(); 
                break; 
        } 
    } 

    private void DrawBar() 
    { 
        dg.Type = AnalyticsType.Bar; 
        dg.Title = "
用户访问柱状图"; 
        dg.XAxisTitle = "月份
"; 
        dg.YAxisTitle = "用户访问数量
"; 
        Random rand = new Random(); 
        string[] aa = { "企业1", "企业2", "企业
3" }; 
        for (int i = 0; i < 2; i++) 
        { 
            ZedGraph.PointPairList ppl = new ZedGraph.PointPairList(); 
            for (int j = 0; j < 12; j++) 
            { 
                double x = rand.Next(10); 
                double y = rand.NextDouble() * 100; 
                ppl.Add(x, y); 
                //dg.NameList.Add((j + 1).ToString() + "月
"); 
                //ppl.Add(j+1,j+1);以此递增
 
                //dg.NameList.Add("第" + j.ToString() + "月份
"); 
            } 
            dg.DataSource.Add(ppl); 
            dg.LabelList.Add("企业
" + i.ToString()); 
            //dg.NameList.Add((i + 1).ToString() + "月
"); 
        } 
        for (int k = 0; k < 12; k++) 
        { 
            dg.NameList.Add((k + 1).ToString() + "月
"); 
        } 
        dg.y_step = 5; 
        dg.DarwGrap(ZedGraphWeb1); 
    } 

    private void DrawPie() 
    { 
        dg.Type = AnalyticsType.Pie; 
        dg.Title = "用户访问饼图
"; 
        Random rand = new Random(); 
        for (int i = 0; i < 3; i++) 
        { 
            dg.ScaleData.Add((i + 2) * rand.Next(100)); 
            dg.NameList.Add("企业:" + i.ToString());//各个部分所代表的含义
 
        } 
        dg.DarwGrap(ZedGraphWeb1); 
    } 
    private void DrawLine() 
    { 
        dg.Type = AnalyticsType.Line; 
        dg.Title = "用户访问曲线图
"; 
        dg.XAxisTitle = "月份
"; 
        dg.YAxisTitle = "用户访问数量
"; 
        Random rand = new Random(); 
        for (int i = 0; i < 2; i++) 
        { 
            ZedGraph.PointPairList ppl = new ZedGraph.PointPairList(); 
            //数据源添加
 
            for (double x = 0; x < 12; x += 1.0) 
            { 
                double y = rand.NextDouble() * 100; 
                ppl.Add(x, y); 
            } 
            //从数据库中取得
 
            //for (int i = 0; i < this.dt.Rows.Count; i++)  //这个循环主要是取到里面的说明文字,用了一个数组的方法
 
            //{ 
            //    ppl.Add(i,this.dt.Rows[i].Cells[1].Text.Trim()); 
            //} 
            //dg.NameList.Add("第" + i.ToString() + "月份
"); 
            dg.DataSource.Add(ppl); 
            dg.NameList.Add("企业:
" + i.ToString()); 
        } 
        //改变x组的显示字符,当然也可以绑定数据库,从数据库中取得。
 
        for (int k = 0; k < 12; k++) 
        { 
            dg.LabelList.Add((k + 1).ToString() + "月
"); 
        } 
        //for (int i = 0; i < this.dt.Rows.Count; i++)  //这个循环主要是取到里面的说明文字,用了一个数组的方法
 
        //{ 
        //    dg.LabelList.Add(this.dt.Rows[i].Cells[0].Text.Trim()); 
        //} 
        dg.DarwGrap(ZedGraphWeb1); 

}
新建一个类DarwGrapClass.cs,放在App_Code目录下面。其内容为:


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

public enum AnalyticsType 

    Line, //
折线图 
    Line2,//带阴影区域的折线图
 
    Curve,//带星的折线图
 
    Curve2,//带阴影区域的星行折线图
 
    Bar, //柱状图
 
    Graph, 
    Pie //饼图
 
}; 
public class DarwGrapClass 

    public DarwGrapClass() 
    { 
        // 
        //TODO: 在此处添加构造函数逻辑
 
        // 
    } 
    #region Private Attribute 
    /**/ 
    ///  
    /// 默认颜色种类
 
    ///  
    private List<Color> defaultColors = new List<Color>(); 
    /**/ 
    ///  
    /// 统计的个数
 
    ///  
    private int Count; 
    #endregion 

    //Public Property; 
    #region Public Property 
    /**/ 
    ///  
    /// 统计图的名称
 
    ///  
    public string Title; 
    /**/ 
    ///  
    /// 横轴的名称(饼图不需要)
 
    ///  
    public string XAxisTitle; 
    /**/ 
    ///  
    /// 纵轴的名称(饼图不需要)
 
    ///  
    public string YAxisTitle; 
    /**/ 
    ///  
    /// 显示的曲线类型:
Line,Bar,Pie 
    ///  
    public AnalyticsType Type; 
    /**/ 
    ///  
    /// 折线图和柱状图的数据源
 
    ///  
    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 double y_step; 
    public double x_step; 
    #endregion 
    public void DarwGrap(ZedGraphWeb ZedGraph) 
    { 
        ZedGraph.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(zedGraphControl_RenderGraph); 

    } 

    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); 
    } 
    /**/ 
    ///  
    /// 如果属性为空则初始化属性数据
 
    ///  
    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 = 1; i < DataSource[0].Count + 1; i++) 
                { 
                    NameList.Add("第" + i.ToString() + "组
"); 
                } 
            } 
            else 
            { 
                for (int i = 1; i < Count + 1; i++) 
                { 
                    NameList.Add("第" + i.ToString() + "组
"); 
                } 
            } 
        } 
        if (LabelList.Count == 0) 
        { 
            for (int i = 0; i < Count; i++) 
            { 
                LabelList.Add("含义
" + i.ToString()); 
            } 
        } 
        if (x_step == 0.0) 
            x_step = 5; 
        if (y_step == 0.0) 
            y_step = 5; 

    } 
    /**/ 
    ///  
    /// 画图
 
    ///  
    ///  
    ///  
    ///  
    private void zedGraphControl_RenderGraph(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane) 
    { 
        InitProperty(); 

        GraphPane myPane = masterPane[0]; 

        myPane.Title.Text = Title; 
        myPane.XAxis.Title.Text = XAxisTitle; 
        myPane.YAxis.Title.Text = YAxisTitle; 

        //if (true) 
        //{ 
        // DrawMessage(myPane, "yiafdhaskjhfasfksahfasdlhfaslf lasgfasglgsadi"); 
        // pane.AxisChange(g); 
        // return; 
        //} 

        switch (Type) 
        { 
            case AnalyticsType.Line: 
                DrawLine(myPane); 
                break; 
            case AnalyticsType.Bar: 
                DrawBar(myPane); 
                break; 
            case AnalyticsType.Pie: 
                DrawPie(myPane); 
                break; 
            case AnalyticsType.Line2: 
                DrawLine2(myPane); 
                break; 
            case AnalyticsType.Curve: 
                DrawCurve(myPane); 
                break; 
            case AnalyticsType.Curve2: 
                DrawCurve2(myPane); 
                break; 
            default: 
                break; 
        } 
        masterPane.AxisChange(g); 
    } 

    #region Draw 
    /**/ 
    ///  
    /// 画折线图
 
    ///  
    ///  
    private void DrawLine(GraphPane graphPane) 
    { 
        for (int i = 0; i < Count; i++) 
        { 
            graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None); 
            string[] labels = LabelList.ToArray(); 
            graphPane.XAxis.Scale.TextLabels = labels; 
            graphPane.XAxis.Type = AxisType.Text; 
            graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F); 
            graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255)); 
            graphPane.YAxis.Scale.MajorStep = y_step; 
        } 
    } 
    /**/ 
    ///  
    /// 画折线图,带阴影区域
 
    ///  
    ///  
    private void DrawLine2(GraphPane graphPane) 
    { 
        for (int i = 0; i < Count; i++) 
        { 
            graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None).Line.Fill = new Fill(Color.White, Colors[i], 90F); 
            string[] labels = LabelList.ToArray(); 
            graphPane.XAxis.Scale.TextLabels = labels; 
            graphPane.XAxis.Type = AxisType.Text; 
            graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F); 
            graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255)); 
            graphPane.YAxis.Scale.MajorStep = y_step; 
        } 
    } 
    /**/ 
    ///  
    /// 画星行折线图
 
    ///  
    ///  
    private void DrawCurve(GraphPane graphPane) 
    { 
        for (int i = 0; i < Count; i++) 
        { 
            graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.Star); 
            string[] labels = LabelList.ToArray(); 
            graphPane.XAxis.Scale.TextLabels = labels; 
            graphPane.XAxis.Type = AxisType.Text; 
            graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F); 
            graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255)); 
            graphPane.YAxis.Scale.MajorStep = y_step; 
        } 
    } 
    /**/ 
    ///  
    /// 画星行折线图,带阴影区域
 
    ///  
    ///  
    private void DrawCurve2(GraphPane graphPane) 
    { 
        for (int i = 0; i < Count; i++) 
        { 
            graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.Star).Line.Fill = new Fill(Color.White, Colors[i],90F); 
            string[] labels = LabelList.ToArray(); 
            graphPane.XAxis.Scale.TextLabels = labels; 
            graphPane.XAxis.Type = AxisType.Text; 
            graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F); 
            graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255)); 
            graphPane.YAxis.Scale.MajorStep = y_step; 
        } 
    } 

    /**/ 
    ///  
    /// 画柱状图
 
    ///  
    ///  
    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]); 
            //.Line.Fill = new Fill(Color.White, Color.Red, 45F); 
            //.Line.Fill = new Fill(Color.White, Color.Blue, 45F); 
        } 
        graphPane.XAxis.MajorTic.IsBetweenLabels = true; 
        string[] labels = NameList.ToArray(); 
        graphPane.XAxis.Scale.TextLabels = labels; 
        graphPane.XAxis.Type = AxisType.Text; 
        graphPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F); 
        //graphPane.Fill = new Fill(Color.FromArgb(250, 250, 255)); 
        graphPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f); 
        //graphPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); 
        graphPane.YAxis.Scale.MajorStep = y_step; 
        //graphPane.BaseDimension =8; 


    } 
    /**/ 
    ///  
    /// 画饼图
 
    ///  
    ///  
    private void DrawPie(GraphPane graphPane) 
    { 
        graphPane.Fill = new Fill(Color.White, Color.Silver, 45.0f); 

        graphPane.Legend.Position = LegendPos.Float; 
        graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top); 
        graphPane.Legend.FontSpec.Size = 20f; 
        graphPane.Legend.IsHStack = false; 
        for (int i = 0; i < Count; i++) 
        { 
            graphPane.AddPieSlice(ScaleData[i], Colors[i], Color.White, 45f, 0, NameList[i]); 
        } 
    } 
    /**/ 
    ///  
    /// 如果系统出错,显示错误信息
 
    ///  
    ///  
    ///  
    private void DrawMessage(GraphPane graphPane, string message) 
    { 
        TextObj text = new TextObj(message, 200, 200); 
        text.Text = message; 
        graphPane.GraphObjList.Add(text); 

    } 
    #endregion 




最后,注意当画饼图时,有时注释会把图片遮住,这时只要设置图片长和高的比例就可以了。曲线图和直方图的x組的说明文字如果太多的话,就会屏蔽掉一些,这是也只要设置长和高的比例就可以解决问题了。





本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/202157,如需转载请自行联系原作者

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34400525/article/details/90000509

智能推荐

使用nginx解决浏览器跨域问题_nginx不停的xhr-程序员宅基地

文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr

在 Oracle 中配置 extproc 以访问 ST_Geometry-程序员宅基地

文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc

Linux C++ gbk转为utf-8_linux c++ gbk->utf8-程序员宅基地

文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8

IMP-00009: 导出文件异常结束-程序员宅基地

文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束

python程序员需要深入掌握的技能_Python用数据说明程序员需要掌握的技能-程序员宅基地

文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求

Spring @Service生成bean名称的规则(当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致)_@service beanname-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname

随便推点

二叉树的各种创建方法_二叉树的建立-程序员宅基地

文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include&lt;stdio.h&gt;#include&lt;string.h&gt;#include&lt;stdlib.h&gt;#include&lt;malloc.h&gt;#include&lt;iostream&gt;#include&lt;stack&gt;#include&lt;queue&gt;using namespace std;typed_二叉树的建立

解决asp.net导出excel时中文文件名乱码_asp.net utf8 导出中文字符乱码-程序员宅基地

文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码

笔记-编译原理-实验一-词法分析器设计_对pl/0作以下修改扩充。增加单词-程序员宅基地

文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词

android adb shell 权限,android adb shell权限被拒绝-程序员宅基地

文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限

投影仪-相机标定_相机-投影仪标定-程序员宅基地

文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定

Wayland架构、渲染、硬件支持-程序员宅基地

文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland

推荐文章

热门文章

相关标签