转自原文 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 == || Colors.Count != Count)
{
Random r = new Random();
int tempIndex = ;
List<int> tempIndexList = new List<int>();
for (int i = ; i < Count; i++)
{
tempIndex = r.Next(defaultColors.Count);
if (tempIndexList.Contains(tempIndex))
{
i--;
}
else
{
tempIndexList.Add(tempIndex);
Colors.Add(defaultColors[tempIndex]);
}
}
}
if (NameList.Count == )
{
if (Type == AnalyticsType.Bar)
{
for (int i = ; i < DataSource[].Count; i++)
{
NameList.Add("第" + i.ToString() + "组");
}
}
else
{
for (int i = ; i < Count; i++)
{
NameList.Add("第" + i.ToString() + "组");
}
}
}
if (LabelList.Count == )
{
for (int i = ; 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[];
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 = ; 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 = ;
graphPane.YAxis.MinorGrid.IsVisible = true;
graphPane.YAxis.MinorGrid.DashOff = ;
graphPane.YAxis.Title.FontSpec.Angle = ;
graphPane.YAxis.Title.FontSpec.FontColor = defaultColors[]; } /// <summary>
/// 画柱状图
/// </summary>
/// <param name="graphPane"></param>
private void DrawBar(GraphPane graphPane)
{
for (int i = ; 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 = ;//栅格线的效果,同下
graphPane.YAxis.Scale.BaseTic = ;//刻度的初始开始值
graphPane.YAxis.Scale.MajorStep = ;//设置刻度的步进值
graphPane.YAxis.MajorGrid.IsVisible = true; //栅格线是否可见
graphPane.YAxis.MajorGrid.DashOff = ;//设置的栅格线的效果。0表示为实线
graphPane.YAxis.MajorGrid.PenWidth = ;//设置栅格线的线条的宽度
graphPane.YAxis.Title.FontSpec.Angle = ;//设置标题的显示,顺时针旋转90度
graphPane.Fill = new Fill(Color.White, Color.FromArgb(, 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 = ; i < Count; i++)
{
PieItem pieitme = graphPane.AddPieSlice(ScaleData[i], Colors[i], Color.Wheat, 45f, , 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, , );
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 = ; j < valueDouble.Count; j++)
{
PointPair pt = new PointPair(j + , valueDouble[j]);
TextObj text = new TextObj(pt.Y.ToString(valueFormat), pt.X, pt.Y>(double)?pt.Y-: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 = ; //数值字体倾斜度
text.FontSpec.Size = ;
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("类别一", ); dic.Add("类别二", ); dic.Add("类别三", );
dic.Add("类别四", ); dic.Add("类别五", ); dic.Add("类别六", );
//柱状图
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 = ; 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 = ; 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 = ; 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); }
}
}

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

  1. TimeSeriesEditor时间序列编辑软件之实战ReoGrid表格控件和Zedgraph绘图控件

    最近用ReoGrid表格控件和Zedgraph绘图控件写了一个TimeSeriesEditor时间序列编辑软件,如下图. 目的就是体验一下这两个空间的用法,感觉还是挺好用的, 关于软件的使用说明可以访 ...

  2. cacti监控系统

    cacti 1. cacti安装 IP: 172.25.44.1 环境: Red Hat 6.5 镜像: rhel-server-6.5-x86_64-dvd.iso 火墙: /etc/init.d/ ...

  3. C# zedgraph利用另一窗口取得的串口数据绘图

    C# zedgraph利用另一窗口获得的串口数据绘图第一次用zedgraph,非常不熟悉,网上很多内容看的云里雾里... 这个程序主界面接收串口数据,而另外一个窗口进行实时曲线绘图,要怎么样实现for ...

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

    笔记内容: 缩放.平移.键盘操作 绘图:直线.矩形 修改:删除.修剪.延时 标注:线型.对齐.半径.折弯.直径.角度 知识点 鼠标中键上下滚动 平移:先全部选中,然后点击中间的空格,随便移动 重点:空 ...

  5. 【R绘图】当图例映射color/shape等多个属性时,如何修改图例标题?

    一般而言,我们修改ggplot2图例标题,常用以下三种方法: + guides(fill=guide_legend(title="New Legend Title")) + lab ...

  6. C#.NET常见问题(FAQ)-如何使用2D绘图控件ZedGraph绘制坐标轴和坐标曲线

    添加数据:示例添加了一条sin曲线和一条cos曲线,注意cos曲线比sin曲线点更密集(可以用这种方式控制点的采样疏密程度)   默认显示效果如下图所示,可以框选一个部分看放大效果   右击某个点可以 ...

  7. ZedGraph控件的使用

    http://blog.chinaunix.net/uid-20776117-id-1847015.html 在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状 ...

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

    在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状图都是很好的表现统计的直观形式.这个时候,ZedGraph控件给我们带来了极大的方便. 1.下载ZedGrap ...

  9. 转:ZedGraph 各属性含义(中文)

    简介:ZedGraph 是一个开源的.NET图表类库, 全部代码都是用C#开发的.它可以利用任意的数据集合创建2D的线性和柱形图表. 属性名称 属性值.作用 MasterPane 一个类对象管理多个G ...

随机推荐

  1. 【剑指offer】二叉树中和为某一值的路径,C++实现

    原创文章,转载请注明出处! 博客文章索引地址 1.题目 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径由结点和有向边组成,从根结点到叶节点. // 二叉树结点的定义 st ...

  2. Java [Leetcode 384]Shuffle an Array

    题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...

  3. 每天一个linux命令:【转载】more命令

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  4. LOJ2500 NOIP2014 飞扬的小鸟 【背包DP】*

    LOJ2500 NOIP2014 飞扬的小鸟 LINK 题目大意就是说有n个柱子,在每一秒你可以选择不点下降高度y和点p次上升x∗p,若果当前位置加上x∗p大于上界m,就会停在m. 如果可以成功穿越所 ...

  5. BZOJ4128 Matrix 【BSGS】

    BZOJ4128 Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  6. 几种设置UITableView的cell动态高度的方法

    1.UITableView加载的顺序是先得到表的行的高度,也就是先调用heightForRowAtIndexPath方法,然后再调用cellForRowAtIndexPath,所以我们有两个办法实现自 ...

  7. 细说VS MSBuild 和 Framework 的区别

    如今已经是 VS2017 横行的时代,而据我所知,大部分人还停留在使用 VS2015 VS2013 或更低的版本,主要是因为他们参与的项目基本使用这几个VS的版本开发的.眼红VS2017却不敢升级,主 ...

  8. memsql 基本安装试用

    备注:使用docker 进行安装 1. 基本准备 a. 环境检查(必须,不然会有服务启动异常的问题) docker run --rm memsql/quickstart check-system b. ...

  9. 笔记:webpack 打包参数 mode development

    webpack 打包参数 mode development 在开发时使用 webpack 打包后不压缩,所以只需要在 webpack 打包命令中加上 --mode mode development 即 ...

  10. Oracle 11gR2 RAC集群服务启动与关闭总结

    引言:这写篇文章的出处是因为我的一名学生最近在公司搭建RAC集群,但对其启动与关闭的顺序和原理不是特别清晰,我在教学工作中也发现了很多学员对RAC知识了解甚少,因此我在这里就把RAC里面涉及到的最常用 ...