C# 使用NPlot绘图技巧 转

C# 使用NPlot绘图技巧

(2010-07-05 19:14:05)
标签:

c

nplot

绘图

it

分类: 学习

///首先要将下载的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 (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 = 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(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 = 2;
    sp1.Pen.Color = Color.Blue;
    this.myPlot.Add(sp1);
    /////点状图////////   
    Marker m = new Marker(Marker.MarkerType.Cross1, 6, 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[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(new NPlot.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(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(2, 4),360-(30-90), "Arrow");
    a.HeadOffset = 5;
    a.ArrowColor = Color.Red;
    a.TextColor = Color.Purple;
    this.myPlot.Add(a);

    this.myPlot.Refresh();
}

posted on 2012-11-04 09:37  武胜-阿伟  阅读(6405)  评论(0编辑  收藏  举报