C# 使用NPlot绘图
首先要将下载的NPlot.dll加到工具箱里,拖一个控件到窗体上,声明using NPlot。
一、入门
1. 对所绘的图进行打印与保存
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);
}
}
}
2. 放大缩小
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();
}
3. 各种绘图
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();
}
二、图表控件NPlot的基本用法
图表控件一直是很难找的,特别是免费又强大的。NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使用起来也非常符合程序员的习惯。
唯一的缺点就是文档特别难找,难读。通过对其文档的阅读和对示例程序源代码的分析,现在将NPlot的基本概念整理如下:
NPlot的命名空间包括NPlot,NPlot.Bitmap,NPlot.Web,NPlot.Web.Design,NPlot.Windows等,其中最核心的,管理各种图表的类都属于NPlot命名空间,NPlot.Bitmap针对位图的管理,NPlot.Web,NPlot.W
eb.Design和NPlot.Windows则可视为NPlot图表在Web Form和Windows Form上的容器(PlotSurface2D)。这些容器可以拖到Form上,也可以位于其他容器之中。
图表控件NPlot下载
Visual Studio上的配置和使用
要在应用程序中应用NPlot控件,首先要把所下载的NPlot.dll添加到.Net工程中。并将其添加到工具箱托盘中。添加方式为:在工具箱上单击右键,选择“选择项”,会出现“选择工具箱项”对话框,在“.Net Framew
orks组件”属性页,选择浏览,找到NPlot.dll添加到工具箱项。这时工具箱中会出现NPlot控件。在设计应用程序界面时,可以将其拖入应用程序界面,系统会在代码中自动创建一个PlotSurface2D对象。PlotSurface2D对象是NPlot图表的容器,所有的图表图形,坐标,标题(都继承IDrawable接口)等各种信息都可以被加入PlotSurface2D。
PlotSurface2D拥有一个非常重要的方法:Add。各种图表图形,坐标,标题都可以通过Add加入PlotSurface2D对象,plot:为控件名称,并引入空间:using NPlot;
点状图代码:
//plot.Clear();//清空
//Grid mygrid = new Grid(); //加入网格
//plot.Add(mygrid); ////Marker m = new Marker(Marker.MarkerType.FilledCircle, 6, new Pen(Color.Blue, 2.0F));//点状图的类型,实心圆点
//Marker m = new Marker(Marker.MarkerType.Cross1, 6, new Pen(Color.Blue, 2.0F));//点状图的类型,叉形
//PointPlot pp = new PointPlot(m);
//int[] a = new int[] { 0, 1 };
//pp.OrdinateData = a;
//StartStep b = new StartStep(-500.0, 10.0);//根据第一个数,可以得到相差10的两个数
//pp.AbscissaData = b;
//pp.Label = "Random";
//plot.Add(pp); //plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
//plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
//plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true)); //plot.XAxis1.IncreaseRange(0.1);
//plot.YAxis1.IncreaseRange(0.1); //缩小到合适大小
//plot.Refresh();
蜡烛图代码:
//plot.Clear();//清空
//int[] opens = { 1, 2, 1, 2, 1, 3 };//圆柱底坐标
//double[] closes = { 2, 2, 2, 1, 2, 1 };//圆柱顶坐标
//float[] lows = { 0, 1, 1, 1, 0, 0 };//下线坐标
//System.Int64[] highs = { 3, 2, 3, 3, 3, 4 };//上线坐标
//int[] times = { 0, 1, 2, 3, 4, 5 };//X轴位置
//CandlePlot cp = new CandlePlot();
//cp.CloseData = closes;
//cp.OpenData = opens;
//cp.LowData = lows;
//cp.HighData = highs;
//cp.AbscissaData = times;
//plot.Add(cp);
//plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
//plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
//plot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true)); //plot.XAxis1.IncreaseRange(0.1);
//plot.YAxis1.IncreaseRange(0.1); //缩小到合适大小
//plot.Refresh();
阶梯状图代码:
//StepPlot sp1 = new StepPlot();
//sp1.OrdinateData = new int[] { 0, 1, 2 };
//sp1.AbscissaData = new int[] { 4, 5, 6 };
//sp1.Label = "高度";
//sp1.Pen.Width = 2;
//sp1.Pen.Color = Color.Blue;
//plot.Add(sp1);
柱状图累加图代码:
//HistogramPlot hp3 = new HistogramPlot();
//hp3.AbscissaData = new int[] { 0, 1, 2 };
//hp3.OrdinateData = new int[] { 4, 5, 6 };
//hp3.BaseWidth = 0.6f;
//hp3.RectangleBrush = RectangleBrushes.Vertical.FaintBlueFade;//纵向渐变
//hp3.Filled = true;
//hp3.Label = "一月";
//HistogramPlot hp4 = new HistogramPlot();
//hp4.AbscissaData = new int[] { 0, 1, 2 };
//hp4.OrdinateData = new int[] { 7, 81, 9 };
//hp4.Label = "二月";
//hp4.RectangleBrush = RectangleBrushes.Horizontal.FaintGreenFade;//横向渐变
//hp4.Filled = true;
//hp4.StackedTo(hp3);
//plot.Add(hp3);
//plot.Add(hp4);
参考文章
2. NPlot开源画图类
C# 使用NPlot绘图的更多相关文章
- C# 使用NPlot绘图技巧
原文 C# 使用NPlot绘图技巧 图表控件一直是很难找的,特别是免费又强大的.NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使 ...
- .Net 开源控件 NPlot使用小结
NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使用起来也非常符合程序员的习惯.授权方式为BSD许可证. 下载链接: http:/ ...
- 利用R语言进行交互数据可视化(转)
上周在中国R语言大会北京会场上,给大家分享了如何利用R语言交互数据可视化.现场同学对这块内容颇有兴趣,故今天把一些常用的交互可视化的R包搬出来与大家分享. rCharts包 说起R语言的交互包,第一个 ...
- 利用R语言制作出漂亮的交互数据可视化
利用R语言制作出漂亮的交互数据可视化 利用R语言也可以制作出漂亮的交互数据可视化,下面和大家分享一些常用的交互可视化的R包. rCharts包 说起R语言的交互包,第一个想到的应该就是rCharts包 ...
- C#下如何用NPlot绘制期货股票K线图(1)?
[简介] 作为一名专业程序化交易者,编程是一个程序员的基本功,本文是作者在做的一个期货CTP项目中有关K线绘图的一部分,偿试类MT4中图表 设计而写,在编写绘图时,查阅了相关资料,感觉还是用NPlot ...
- Canvas绘图之平移translate、旋转rotate、缩放scale
画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...
- 用html5的canvas和JavaScript创建一个绘图程序
本文将引导你使用canvas和JavaScript创建一个简单的绘图程序. 创建canvas元素 首先准备容器Canvas元素,接下来所有的事情都会在JavaScript里面. <canvas ...
- echarts+php+mysql 绘图实例
最近在学习php+mysql,因为之前画图表都是直接在echart的实例demo中修改数据,便想着两相结合练习一下,通过ajax调用后台数据画图表. 我使用的是echart3,相比较第二版,echar ...
- html5 canvas常用api总结(二)--绘图API
canvas可以绘制出很多奇妙的样式和美丽的效果,通过几个简单的api就可以在画布上呈现出千变万化的效果,还可以制作网页游戏,接下来就总结一下和绘图有关的API. 绘画的时候canvas相当于画布,而 ...
随机推荐
- JavaWeb项目开发案例精粹-第6章报价管理系统-07View层
1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...
- 实记JLink刷固件方法
最近J-Link调试程序,不知何故,USB报无法连接,试了别的电脑也是这样.文件存于“百度网盘/05.组装维修/4.单片机/1.JLink-ARM仿真器”下.所有的操作基于WIN7 32位系统. 1. ...
- mongo中查询Array类型的字段中元素个数
I have a MongoDB collection with documents in the following format: { "_id" : ObjectId(&qu ...
- iOS 深拷贝和浅拷贝
注意,本文只说结论不说过程 1. 对于系统的非容器类对象,我们可以认为,如果对一不可变对象复制,copy是指针复制(浅拷贝)和mutableCopy就是对象复制(深拷贝).如果是对可变对象复制,都是深 ...
- 自己用反射写的一个request.getParameter工具类
适用范围:当我们在jsp页面需要接收很多值的时候,如果用request.getParameter(属性名)一个一个写的话那就太麻烦了,于是我想是 否能用反射写个工具类来简化这样的代码,经过1个小时的代 ...
- linux 命令案例学习——文件搜索
两个搜索文件的工具 locate ——仅仅通过文件名查找文件 find ——依据文件的各种属性在既定目录(包括子目录)里查找 一个通常与文件搜索命令一起使用.处理搜索结果文件列表的命令 xa ...
- mysql插入中文数据报错:incorrect string value
我是用的source指令来执行sql脚本文件时发现的这个错误 我的数据库用创建的时候create Database If Not Exists XXXXXX Character Set UTF8; 那 ...
- framwork NHibernate
NHibernate 一.NHibernate 1.HQL curd语句总结 . 查询整个映射对象所有字段 ? //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String ...
- C#题型补充
让用户输入一个奇数,打印菱形,最长的行内容个数为用户输入的个数,并且由英文字母拼接而成 Console.Write("请输入一个数字:"); try { int a = Conve ...
- Eclipse 修改debug当前行的颜色
window --preferences--general--editors--text editors--annotations--debug current instruction pointer