C# 使用NPlot绘图

首先要将下载的NPlot.dll加到工具箱里,拖一个控件到窗体上,声明using NPlot。

一、入门

1. 对所绘的图进行打印与保存

  View Code

 

2. 放大缩小

1
2
3
4
5
6
7
8
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. 各种绘图

  View Code

 

二、图表控件­­­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下载

  Download Link

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);
复制代码

 

转载于:https://www.cnblogs.com/caizhao/p/10760870.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值