NPlot的基本用法
1、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对象,plot:为控件名称,并引入空间:using NPlot;我们可以通过下面简单的代码示例来了解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 = \
StepPlot line = new StepPlot(); line.Pen = new Pen (Color.Red, 1);
line.OrdinateData = new int [] {0, 1, 0, 1, 1, 0}; line.HideVerticalSegments = false; plot.Add(line); plot.Refresh(); return; }
Grid对象和StepPlot对象都是NPlot命名空间中的对象,都继承于NPlot.IDrawable,都可以作为PlotSurface2D.Add函数调用,在NPlot中画图,就是把网格,坐标,图形等各种对象加入PlotSurface2D对象中,一切就那么简单!
NPlot的下载地址:http://netcontrols.org/nplot/wiki/index.php?n=Main.HomePage NPlot的开发文档:http://www.andrewtheken.com/nplot/
E_mail:kissyougoodby@163.com QQ:55774088 public void plotline(double[] x,double[] y) {
// --- Plotting --- plot.Clear(); // --- Grid Code --- Grid mygrid = new Grid();
mygrid.HorizontalGridType = Grid.GridType.None; mygrid.VerticalGridType = Grid.GridType.Fine; plot.Add(mygrid); plot.Title = \×××\ LinePlot lp = new LinePlot(); /////横坐标赋值
lp.AbscissaData = x;//new double[] { 0, 25,125 }; /////纵坐标赋值
lp.OrdinateData = y;//new double[] { 200, 200, 0};
//StepPlot line = new StepPlot(); //line.Pen = new Pen(Color.Red, 1);
//line.OrdinateData = new double[] { 5.5, 4.0, 1.5, 2.5, 3.5 }; //line.HideVerticalSegments = false; plot.Add(lp); plot.Refresh(); return; }
2、Nplot用法举例
myPlot.Clear();
//加入网格
Grid mygrid = new Grid(); myPlot.Add(mygrid); myPlot.Title = \测试\
LinePlot lp = new LinePlot();
double[] a = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; string[] b = new string[] { \\
double[] ab = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ArrayList dates = new ArrayList();
dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\
dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ dates.Add(Convert.ToDateTime(\ lp.AbscissaData = dates;
//柱状
BarPlot storeGrowth = new BarPlot(); storeGrowth.AbscissaData = dates; storeGrowth.OrdinateDataTop = a;
storeGrowth.OrdinateDataBottom = ab;
storeGrowth.FillBrush = NPlot.RectangleBrushes.Solid.Red; //storeGrowth.BorderPen = new Pen( Color.Black, 2.0f ); myPlot.Add(storeGrowth);
//标签
LabelPointPlot tp1 = new LabelPointPlot();
tp1.AbscissaData = dates; tp1.OrdinateData = a; tp1.TextData = b;
tp1.LabelTextPosition = LabelPointPlot.LabelPositions.Above; tp1.Marker = new Marker(Marker.MarkerType.None, 10); myPlot.Add(tp1);
//lp.
lp.DataSource = a;
lp.Pen = new Pen(Color.Blue, 3.0f); lp.Label = \ myPlot.Add(lp);
myPlot.XAxis1.Label = \时间\ myPlot.YAxis1.Label = \次数\
//myPlot.XAxis1.WorldLength = 60 * 60 * 24;
//设置网格距离
//myPlot.XAxis1.WorldMin += myPlot.XAxis1.WorldLength/ 12;//WorldLength=n*24*60*60;
//myPlot.XAxis1.WorldMax -= myPlot.XAxis1.WorldLength / 2;
//myPlot.XAxis1.
//myPlot.XAxis1.WorldLength = 10; //myPlot.
//让日期斜45度。
myPlot.XAxis1.TicksLabelAngle = 45;
//下面代码可让窗体移动begin; myPlot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
//myPlot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
myPlot.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true));
myPlot.Refresh(); 3、Nplot用法
///首先要将下载的NPlot.dll加到工具箱里,拖一个控件到窗体上,声明using NPlot;
////////对所绘的图进行打印与保存//////////
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(1, 1, w, h); saveFileDialog1.RestoreDirectory = true; saveFileDialog1.CreatePrompt = true; myPlot.DrawToBitmap(bm, rt);
if (saveFileDialog1.FilterIndex == 1) {
bm.Save(saveFileDialog1.FileName);
相关推荐: