C# 使用NPlot绘图技巧
图表控件一直是很难找的,特别是免费又强大的。NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使用起来也非常符合程序员的习惯。唯一的缺点就是文档特别难找,难读。通过对其文档的阅读和对示例程序源代码的分析,现在将NPlot的基本概念整理如下:
NPlot的命名空间包括NPlot,NPlot.Bitmap,NPlot.Web,NPlot.Web.Design,NPlot.Windows等,其中最核心的,管理各种图表的类都属于NPlot命名空间,NPlot.Bitmap针对位图的管理,NPlot.Web,NPlot.Web.Design和NPlot.Windows则可视为NPlot图表在Web Form和Windows Form上的容器(PlotSurface2D)。这些容器可以拖到Form上,也可以位于其他容器之中。
要在应用程序中应用NPlot控件,首先要把所下载的NPlot.dll添加到.Net工程中。并将其添加到工具箱托盘中。添加方式为:在工具箱上单击右键,选择“选择项”,会出现“选择工具箱项”对话框,在“.Net Frameworks组件”属性页,选择浏览,找到NPlot.dll添加到工具箱项。这时工具箱中会出现NPlot控件。在设计应用程序界面时,可以将其拖入应用程序界面,系统会在代码中自动创建一个PlotSurface2D对象。
PlotSurface2D对象是NPlot图表的容器,所有的图表图形,坐标,标题(都继承IDrawable接口)等各种信息都可以被加入PlotSurface2D。PlotSurface2D拥有一个非常重要的方法:Add。各种图表图形,坐标,标题都可以通过Add加入PlotSurface2D对象。
我们可以通过下面简单的代码示例来了解NPlot的基本用法:
public void plotline()
{
// --- Plotting ---
plot.Clear();
// --- Grid Code ---
Grid mygrid = new Grid();
mygrid.HorizontalGridType = Grid.GridType.None;
mygrid.VerticalGridType = Grid.GridType.Fine;
plot.Add(mygrid);
plot.Title = "Test";
StepPlot line = new StepPlot();
line.Pen = new Pen (Color.Red, );
line.OrdinateData = new int [] {, , , , , };
line.HideVerticalSegments = false;
plot.Add(line);
plot.Refresh();
return;
}
Grid对象和StepPlot对象都是NPlot命名空间中的对象,都继承于NPlot.IDrawable,都可以作为PlotSurface2D.Add函数调用,在NPlot中画图,就是把网格,坐标,图形等各种对象加入PlotSurface2D对象中,一切就那么简单!
下面给出更多代码例子
////////对所绘的图进行打印与保存//////////
private void print()
{
myPlot.Print(true);
}
private void save()
{
saveFileDialog1.Filter = "位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg;*.jpeg;*,jpe|Gif(*.gif)|*.gif|Tiff(*.tiff)|*.tiff|Png(*.png)|*.png|Exif(*.exif)|*.exif|所有文件(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
int h = myPlot.Size.Height;
int w = myPlot.Size.Width;
Bitmap bm = new Bitmap(w, h);
Bitmap bm1 = new Bitmap(w, h);
Rectangle rt = new Rectangle(, , w, h);
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.CreatePrompt = true;
myPlot.DrawToBitmap(bm, rt);
if (saveFileDialog1.FilterIndex == )
{
bm.Save(saveFileDialog1.FileName);
}
if (saveFileDialog1.FilterIndex == )
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
}
if (saveFileDialog1.FilterIndex == )
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Gif);
}
if (saveFileDialog1.FilterIndex == )
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Tiff);
}
if (saveFileDialog1.FilterIndex == )
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Png);
}
if (saveFileDialog1.FilterIndex == )
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Exif);
}
}
catch (Exception MyEx)
{
MessageBox.Show(MyEx.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
} ///放大缩小
private void changeSize()
{
this.myPlot.XAxis1.IncreaseRange(0.1);
this.myPlot.YAxis1.IncreaseRange(0.1); //缩小
this.myPlot.XAxis1.IncreaseRange(-0.1);
this.myPlot.YAxis1.IncreaseRange(-0.1); //放大
this.myPlot.Refresh();
}
/////////各种绘图//////////
private void plot()
{
this.myPlot.Clear(
////////标签//////////
string[] strLabel = new string[leng];
for (int i = ; i < leng; i++)
strLabel[i] = Convert.ToString(p[i]);
LabelPointPlot labp = new LabelPointPlot();
labp.AbscissaData = X;
labp.OrdinateData = p;
labp.TextData = strLabel;
labp.LabelTextPosition = LabelPointPlot.LabelPositions.Above;
labp.Marker = new Marker(Marker.MarkerType.Square, );
labp.Marker.Color = Color.Blue;
myPlot.Add(labp);
myPlot.Refresh();
////////网格//////////
Grid mygrid = new Grid();
mygrid.HorizontalGridType = Grid.GridType.Fine;
mygrid.VerticalGridType = Grid.GridType.Fine;
this.myPlot.Add(mygrid);
////////曲线,双坐标轴//////////
///////水平线//////////
HorizontalLine line = new HorizontalLine(1.2);
line.LengthScale = 0.89f;
this.myPlot.Add(line, -);
///////垂直线///////////
VerticalLine line2 = new VerticalLine(1.2);
line2.LengthScale = 0.89f;
this.myPlot.Add(line2);
///////普通的线///////////
LinePlot lp3 = new LinePlot();
lp3.OrdinateData = yPW;
lp3.AbscissaData = x;
lp3.Pen = new Pen(Color.Orange);
lp3.Pen.Width = ;
lp3.Label = " 价格";
this.myPlot.Add(lp3);
LinearAxis linx = (LinearAxis)myPlot.XAxis1;
this.myPlot.XAxis1 = linx;
LinearAxis liny = (LinearAxis)myPlot.YAxis1;
liny.Label = "价格";
liny.AxisColor = Color.Orange;
liny.LabelColor = Color.Orange;
liny.TickTextColor = Color.Orange;
this.myPlot.YAxis1 = liny; LinePlot lp4 = new LinePlot();
lp4.OrdinateData = yUw;
lp4.AbscissaData = x;
lp4.Pen = new Pen(Color.Green);
lp4.Pen.Width = ;
lp4.Label = "销售量";
this.myPlot.Add(lp4, PlotSurface2D.XAxisPosition.Top, PlotSurface2D.YAxisPosition.Right);
LinearAxis liny2 = (LinearAxis)myPlot.YAxis2;
liny2.WorldMax = 1.2;
liny2.WorldMin = ;
liny2.Label = "销售量";
liny2.AxisColor = Color.Green;
liny2.LabelColor = Color.Green;
liny2.TickTextColor = Color.Green;
this.myPlot.YAxis2 = liny2;
///////图例//////////
this.myPlot.Legend = new Legend();
this.myPlot.Legend.AttachTo(PlotSurface2D.XAxisPosition.Top, PlotSurface2D.YAxisPosition.Right);
this.myPlot.Legend.NumberItemsHorizontally = ;
this.myPlot.Legend.HorizontalEdgePlacement = Legend.Placement.Inside;
this.myPlot.Legend.VerticalEdgePlacement = Legend.Placement.Inside;
this.myPlot.Legend.YOffset = ;
this.myPlot.Legend.XOffset = -;
///////窗体移动//////////
this.myPlot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
this.myPlot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
this.myPlot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true));
//////累加的柱状图////////
HistogramPlot hp3 = new HistogramPlot();
hp3.AbscissaData = x;
hp3.OrdinateData = yCC1;
hp3.BaseWidth = 0.6f;
hp3.RectangleBrush = RectangleBrushes.Vertical.FaintBlueFade;
hp3.Filled = true;
hp3.Label = "一月";
HistogramPlot hp4 = new HistogramPlot();
hp4.AbscissaData = x;
hp4.OrdinateData = yCC2;
hp4.Label = "二月";
hp4.RectangleBrush = RectangleBrushes.Horizontal.FaintGreenFade;
hp4.Filled = true;
hp4.StackedTo(hp3);
this.myPlot.Add(hp3);
this.myPlot.Add(hp4);
//////阶状图////////
StepPlot sp1 = new StepPlot();
sp1.OrdinateData = yCH1;
sp1.AbscissaData = x;
sp1.Label = "高度";
sp1.Pen.Width = ;
sp1.Pen.Color = Color.Blue;
this.myPlot.Add(sp1);
/////点状图////////
Marker m = new Marker(Marker.MarkerType.Cross1, , new Pen(Color.Blue, 2.0F));
PointPlot pp = new PointPlot(m);
pp.OrdinateData = a;
pp.AbscissaData = new StartStep(-500.0, 10.0);
pp.Label = "Random";
this.myPlot.Add(pp);
/////Image图////////
double[,] map = new double[, ];
for (int i = ; i < ; ++i)
{
for (int j = ; j < ; ++j)
{
map[i, j] = Convert.ToDouble(tokens[i * + j], new
System.Globalization.CultureInfo("en-US"));
}
}
ImagePlot ip = new ImagePlot(map, -9.0f, 1.0f, -9.0f, 1.0f);
ip.Gradient = new LinearGradient(Color.Gold, Color.Black);
this.myPlot.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
this.myPlot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.RubberBandSelection());
this.myPlot.Add(ip);
///////蜡烛图///////////
int[] opens = { , , , , , };
double[] closes = { , , , , , };
float[] lows = { , , , , , };
System.Int64[] highs = { , , , , , };
int[] times = { , , , , , };
CandlePlot cp = new CandlePlot();
cp.CloseData = closes;
cp.OpenData = opens;
cp.LowData = lows;
cp.HighData = highs;
cp.AbscissaData = times;
this.myPlot.Add(cp);
/////对数坐标轴//////// // x axis
LogAxis logax = new LogAxis(plotSurface.XAxis1);
logax.WorldMin = xmin;
logax.WorldMax = xmax;
logax.AxisColor = Color.Red;
logax.LabelColor = Color.Red;
logax.TickTextColor = Color.Red;
logax.LargeTickStep = 1.0f;
logax.Label = "x";
this.myPlot.XAxis1 = logax;
// y axis
LogAxis logay = new LogAxis(plotSurface.YAxis1);
logay.WorldMin = ymin;
logay.WorldMax = ymax;
logay.AxisColor = Color.Red;
logay.LabelColor = Color.Red;
logay.TickTextColor = Color.Red;
logay.LargeTickStep = 1.0f;
logay.Label = "x^2";
this.myPlot.YAxis1 = logay;
/////字符坐标轴////////
LabelAxis la1 = new LabelAxis(this.myPlot.XAxis1);
string[] sX = new string [];
for (int i = ; i < ; i++)
{
la1.AddLabel(sX[i].ToString(), i);
}
la1.Label = "时间";
la1.TickTextFont = new Font("Courier New", );
la1.TicksBetweenText = true;
this.myPlot.XAxis1 = la1;
/////区域着色////////
FilledRegion fr = new FilledRegion(new VerticalLine(1.2),new VerticalLine(2.4));
//两条线之间的区域: FilledRegion fr = new FilledRegion(lp1, lp2);
fr.Brush = Brushes.BlanchedAlmond;
this.myPlot.Add(fr);
//////画箭头//////////
ArrowItem a = new ArrowItem(new PointD(, ),-(-), "Arrow");
a.HeadOffset = ;
a.ArrowColor = Color.Red;
a.TextColor = Color.Purple;
this.myPlot.Add(a); this.myPlot.Refresh();
}
C# 使用NPlot绘图技巧的更多相关文章
- C# 使用NPlot绘图
首先要将下载的NPlot.dll加到工具箱里,拖一个控件到窗体上,声明using NPlot. 一.入门 1. 对所绘的图进行打印与保存 private void print() { myPlot.P ...
- cocos2d-x游戏引擎核心之六——绘图原理和绘图技巧
一.OpenGL基础 游戏引擎是对底层绘图接口的包装,Cocos2d-x 也一样,它是对不同平台下 OpenGL 的包装.OpenGL 全称为 Open Graphics Library,是一个开放的 ...
- MATLAB之折线图、柱状图、饼图以及常用绘图技巧
MATLAB之折线图.柱状图.饼图以及常用绘图技巧 一.折线图 参考代码: %图1:各模式直接成本预测 %table0-table1为1*9的数组,记录关键数据 table0 = data_modol ...
- CAD绘图效率低?教你4个CAD绘图技巧,绘图效率提升十倍
CAD绘图一直是一个谜一样的存在,说它简单吧,很多人都无法完全精通,说它难吧,很多人也都自学成才了. 如何学好CAD绘图是个难题,但是老话说的好,只要思想不滑坡,办法总比困难多,掌握以下这些CAD绘图 ...
- CSS 奇思妙想 | Single Div 绘图技巧
经常能看到有关 CSS 绘图的文章,譬如使用纯 HTML + CSS 绘制一幅哆啦 A 梦图画.实现的方式就是通过堆叠 div,一步一步实现图画中的一块一块.这种技巧本身没有什么问题,但是就是少了一些 ...
- Python绘图技巧
转自:https://www.cnblogs.com/zhizhan/p/5615947.html Python--matplotlib绘图可视化知识点整理 强烈推荐ipython 原文:http:/ ...
- qt 2D绘图技巧
2D绘图 Qt4中的2D绘图部分称为Arthur绘图系统.它由3个类支撑整个框架,QPainter,QPainterDevice和QPainterEngine.QPainter用来执行具体的绘图相关操 ...
- 数据分析 - Excel 配色, 绘图, 技巧
美学 配色 画图本身是美学的展示, 出色的配色是必须的 虽然本身美学并不是数据分析的必要, 但是也不能太low 如果做的太丑展示也是很尴尬 配色网站 点击这里 配置 现版本的 excel 中已存在较为 ...
- VC 绘图技巧--自定义形状图形
自定义形状图形,定义几个点围城的图形,然后进行描边和填充: if (m_memDC.m_hDC!=NULL) { CPoint point[4]; point[0].x=nLeft+(int)(0.1 ...
随机推荐
- BASM遵循的规则
任何情况下,在寄存器的使用上,BASM遵循如下的规则: ASM 语句执行过程中,必须保存EDI.ESI.ESP.EBP.EBX 的值(5个寄存器,意思是可以用,但最后得恢复成原模原样). ASM ...
- Java:IO流之字符流Reader、Writer详解
java.io包中:字符流 字符流的两个抽象基类: Reader Writer 文件的读取:Reader抽象类(java.io包中) 直接子类的构造方法: FileRead ...
- Spring_MVC_教程_快速入门_深入分析
Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门 资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf Spring ...
- 使用Windows Azure PowerShell远程管理Windows Azure虚拟机
对于Windows Azure,如果你还在使用windowsazure.com门户来管理虚拟机,那就显得不怎么高上大了.Windows Azure PowerShell 是一个功能强大的脚本环境,可用 ...
- 短信发送AZDG加密算法
public static string passport_encrypt(string txt, string key) { // 使用随机数发生器产生 ...
- java获取当前月第一天和最后一天,上个月第一天和最后一天
package com.test.packager; import java.text.ParseException; import java.text.SimpleDateFormat; impor ...
- asp开发微信扫码支付
这个任务已经给了.现在正在学习开发中.主要注意的是2点. 1:返回参数的验证. 2:通知后业务处理和处理后返回财付通.大部分操作,api中已经处理好. 现在需要的业务逻辑部分. 需要正确3个参数 r ...
- Web Servers in Visual Studio for ASP.NET Web Projects
https://msdn.microsoft.com/en-us/library/58wxa9w5(v=vs.120).aspx When you develop web projects in Vi ...
- 反编译android APK
我们经常会在如下的情况使用反编译 1.看到别人应用中的酷炫功能,想知道是如何实现的 2.别人应用的素材排版好漂亮,想套用模仿 百度一下就已经有一大堆反编译的教程了,我还是坚持学习记录一下. A ...
- UVa (BFS) The Monocycle
题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方 ...