NPlot的基本用法



NPlot的基本用法

1NPlot的基本用法
 圖表控件一直是很難找的,特別是免費又強大的。NPlot是一款非常難得的.Net平臺下的圖表控件,能做各種曲線圖,柱狀圖,餅圖,散點圖,股票圖等,而且它免費又開源,使用起來也非常符合程序員的習慣。唯一的缺點就是文檔特別難找,難讀。通過對其文檔的閱讀和對示例程序源代碼的分析,現在將NPlot的基本概念整理如下:
   NPlot
的命名空間包括NPlot,NPlot.Bitmap,NPlot.Web,NPlot.Web.Design,NPlot.Windows等,其中最核心的,管理各種圖表的類都屬於NPlot命名空間,NPlot.Bitmap針對位圖的管理,NPlot.Web,NPlot.Web.DesignNPlot.Windows則可視爲NPlot圖表在Web FormWindows Form上的容器(PlotSurface2D)。這些容器可以拖到Form上,也可以位於其他容器之中。要在應用程序中應用NPlot控件,首先要把所下載的NPlot.dll添加到.Net工程中。並將其添加到工具箱托盤中。添加方式爲:在工具箱上單擊右鍵,選擇選擇項,會出現選擇工具箱項對話框,在“.Net Frameworks組件屬性頁,選擇瀏覽,找到NPlot.dll添加到工具箱項。這時工具箱中會出現NPlot控件。在設計應用程序界面時,可以將其拖入應用程序界面,系統會在代碼中自動創建一個PlotSurface2D對象。
  PlotSurface2D
對象是NPlot圖表的容器,所有的圖表圖形,座標,標題(都繼承IDrawable接口)等各種信息都可以被加入PlotSurface2DPlotSurface2D擁有一個非常重要的方法: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 = "Test";
  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[email protected] 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 = newLinePlot();
        /////
橫座標賦值
        lp.AbscissaData = x;//newdouble[] { 0, 25,125 };
        /////
縱座標賦值
        lp.OrdinateData = y;//newdouble[] { 200, 200, 0};

        //StepPlot line = newStepPlot();
        //line.Pen = newPen(Color.Red, 1);
        //line.OrdinateData = newdouble[] { 5.5, 4.0, 1.5, 2.5, 3.5 };
        //line.HideVerticalSegments= false;
        plot.Add(lp);
        plot.Refresh();
        return;
    }

2Nplot用法舉例

              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[] {"1", "2", "3", "4", "5","6", "7", "8", "9", "10","11", "12", };

                double[] ab = new double[] { 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                ArrayList dates = newArrayList();

               dates.Add(Convert.ToDateTime("2007-01-01"));

               dates.Add(Convert.ToDateTime("2008-01-02 "));

               dates.Add(Convert.ToDateTime("2008-01-03 "));

               dates.Add(Convert.ToDateTime("2008-01-04 "));

               dates.Add(Convert.ToDateTime("2008-01-05 "));

                dates.Add(Convert.ToDateTime("2008-01-06"));

               dates.Add(Convert.ToDateTime("2008-01-07 "));

               dates.Add(Convert.ToDateTime("2008-01-08 "));

               dates.Add(Convert.ToDateTime("2008-01-09 "));

                dates.Add(Convert.ToDateTime("2008-01-10"));

               dates.Add(Convert.ToDateTime("2008-01-11 "));

               dates.Add(Convert.ToDateTime("2008-02-12 "));

                lp.AbscissaData = dates;

 

                //柱狀

                BarPlot storeGrowth = newBarPlot();

                storeGrowth.AbscissaData =dates;

                storeGrowth.OrdinateDataTop =a;

                storeGrowth.OrdinateDataBottom= ab;

                storeGrowth.FillBrush =NPlot.RectangleBrushes.Solid.Red;

                //storeGrowth.BorderPen = newPen( Color.Black, 2.0f );

                myPlot.Add(storeGrowth);

 

                //標籤

                LabelPointPlot tp1 = newLabelPointPlot();

                tp1.AbscissaData = dates;

                tp1.OrdinateData  = a;

                tp1.TextData = b;

                tp1.LabelTextPosition =LabelPointPlot.LabelPositions.Above;

                tp1.Marker = newMarker(Marker.MarkerType.None, 10);

                myPlot.Add(tp1);

 

 

                //lp.

                lp.DataSource = a;

                lp.Pen = new Pen(Color.Blue,3.0f);

                lp.Label = "GaussianFunction";

                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(newNPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());

                //myPlot.AddInteraction(newNPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());

                myPlot.AddInteraction(newNPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true));

 

                myPlot.Refresh();

3Nplot用法

///首先要將下載的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);
           
 
}
           
 
if (saveFileDialog1.FilterIndex == 2)
           
 
{
               
 
bm.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
           
 
}
           
 
if (saveFileDialog1.FilterIndex == 3)
           
 
{
               
 
bm.Save(saveFileDialog1.FileName, ImageFormat.Gif);
           
 
}
           
 
if (saveFileDialog1.FilterIndex == 4)
           
 
{
               
 
bm.Save(saveFileDialog1.FileName, ImageFormat.Tiff);
           
 
}
           
 
if (saveFileDialog1.FilterIndex == 5)
           
 
{
               
 
bm.Save(saveFileDialog1.FileName, ImageFormat.Png);
           
 
}
           
 
if (saveFileDialog1.FilterIndex == 6)
           
 
{
               
 
bm.Save(saveFileDialog1.FileName, ImageFormat.Exif);
           
 
}
       
 
}
       
 
catch (ExceptionMyEx)
       
 
{
           
 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 = 0; 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, 8);
   
 
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, -10);
   
 ///////垂直線///////////

   
 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 = 2;
   
 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 = 2;
   
 lp4.Label = "銷售量
";
   
 
this.myPlot.Add(lp4, PlotSurface2D.XAxisPosition.Top,PlotSurface2D.YAxisPosition.Right);
   
 
LinearAxis liny2 = (LinearAxis)myPlot.YAxis2;
   
 
liny2.WorldMax = 1.2;
   
 
liny2.WorldMin = 0;
   
 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 = 2;
   
 
this.myPlot.Legend.HorizontalEdgePlacement =Legend.Placement.Inside;
   
 
this.myPlot.Legend.VerticalEdgePlacement =Legend.Placement.Inside;
   
 
this.myPlot.Legend.YOffset = 5;
   
 
this.myPlot.Legend.XOffset = -5;
  
  ///////窗體移動//////////
   
 this.myPlot.AddInteraction(newNPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag());
   
 
this.myPlot.AddInteraction(newNPlot.Windows.PlotSurface2D.Interactions.VerticalDrag());
   
 
this.myPlot.AddInteraction(newNPlot.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 = 2;
   
 
sp1.Pen.Color = Color.Blue;
   
 
this.myPlot.Add(sp1);
    /////點狀圖////////   
   
 
Marker m = new Marker(Marker.MarkerType.Cross1, 6, newPen(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[19, 19];
   
 
for (int i = 0; i < 19; ++i)
   
 
{
       
 
for (int j = 0;j < 19; ++j)
       
 
{
           
 
map[i, j] = Convert.ToDouble(tokens[i * 19 + 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(newNPlot.Windows.PlotSurface2D.Interactions.RubberBandSelection());
   
 
this.myPlot.Add(ip);
    ///////蠟燭圖///////////
   
 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 };
   
 
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 [15];
   
 
for (int i = 0; i < 15; i++)
   
 
{
       
 
la1.AddLabel(sX[i].ToString(),i);
   
 
}
   
 la1.Label = "時間
";
   
 
la1.TickTextFont = new Font("Courier New", 10);
   
 
la1.TicksBetweenText = true;
   
 
this.myPlot.XAxis1 = la1;
   
 
/////區域着色////////          
   
 
FilledRegion fr = new FilledRegion(newVerticalLine(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(2, 4),360-(30-90),"Arrow");
   
 
a.HeadOffset = 5;
   
 
a.ArrowColor = Color.Red;
   
 
a.TextColor = Color.Purple;
   
 this.myPlot.Add(a);

    this.myPlot.Refresh();
}

 

發佈了18 篇原創文章 · 獲贊 37 · 訪問量 98萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章