コード例 #1
0
        public void PlotMockup()
        {
            string lines ="THE TEST (can your charting library handle this?) - xuzhenzhen.com.chart demonstrates it can handle real world charting requirements.";
            infoBox.Text = lines;
            // first of all, generate some mockup data.
            DataTable info = new DataTable( "Store Information" );
            info.Columns.Add( "Index", typeof(int) );
            info.Columns.Add( "IndexOffsetLeft", typeof(float) );
            info.Columns.Add( "IndexOffsetRight", typeof(float) );
            info.Columns.Add( "StoreName", typeof(string) );
            info.Columns.Add( "BarBase", typeof(float) );
            info.Columns.Add( "StoreGrowth", typeof(float) );
            info.Columns.Add( "AverageGrowth", typeof(float) );
            info.Columns.Add( "ProjectedSales", typeof(float) );

            float barBase = 185.0f;
            Random r = new Random();
            for (int i=0; i<18; ++i)
            {
                DataRow row = info.NewRow();
                row["Index"] = i;
                row["IndexOffsetLeft"] = (float)i - 0.1f;
                row["IndexOffsetRight"] = (float)i + 0.1f;
                row["StoreName"] = "Store " + (i+1).ToString();
                row["BarBase"] = barBase;
                row["StoreGrowth"] = barBase + ( (r.NextDouble() - 0.1) * 20.0f );
                row["AverageGrowth"] = barBase + ( (r.NextDouble() - 0.1) * 15.0f );
                row["ProjectedSales"] = barBase + ( r.NextDouble() * 15.0f );
                info.Rows.Add( row );
                barBase += (float)r.NextDouble() * 4.0f;
            }

            plotSurface.Clear();

            // generate the grid
            Grid grid = new Grid();
            grid.VerticalGridType = Grid.GridType.Coarse;
            grid.HorizontalGridType = Grid.GridType.None;
            grid.MajorGridPen = new Pen( Color.Black, 1.0f );
            plotSurface.Add( grid );

            // generate the trendline
            LinePlot trendline = new LinePlot();
            trendline.DataSource = info;
            trendline.AbscissaData = "Index";
            trendline.OrdinateData = "BarBase";
            trendline.Pen = new Pen( Color.Black, 3.0f );
            trendline.Label = "Trendline";
            plotSurface.Add( trendline );

            // draw store growth bars
            BarPlot storeGrowth = new BarPlot();
            storeGrowth.DataSource = info;
            storeGrowth.AbscissaData = "IndexOffsetLeft";
            storeGrowth.OrdinateDataTop = "StoreGrowth";
            storeGrowth.OrdinateDataBottom = "BarBase";
            storeGrowth.Label = "Store Growth";
            storeGrowth.FillBrush = xuzhenzhen.com.chart.RectangleBrushes.Solid.Black;
            //storeGrowth.BorderPen = new Pen( Color.Black, 2.0f );
            plotSurface.Add( storeGrowth );

            // draw average growth bars
            BarPlot averageGrowth = new BarPlot();
            averageGrowth.DataSource = info;
            averageGrowth.AbscissaData = "IndexOffsetRight";
            averageGrowth.OrdinateDataBottom = "BarBase";
            averageGrowth.OrdinateDataTop = "AverageGrowth";
            averageGrowth.Label = "Average Growth";
            averageGrowth.FillBrush = xuzhenzhen.com.chart.RectangleBrushes.Solid.Gray;
            //averageGrowth.BorderPen = new Pen( Color.Black, 2.0f );
            plotSurface.Add( averageGrowth );

            // generate the projected sales step line.
            StepPlot projected = new StepPlot();
            projected.DataSource = info;
            projected.AbscissaData = "Index";
            projected.OrdinateData = "ProjectedSales";
            projected.Pen = new Pen( Color.Orange, 3.0f );
            projected.HideVerticalSegments = true;
            projected.Center = true;
            projected.Label = "Projected Sales";
            projected.WidthScale = 0.7f;
            plotSurface.Add( projected );

            // generate the minimum target line.
            HorizontalLine minimumTargetLine = new HorizontalLine( 218, new Pen( Color.Green, 3.5f ) );
            minimumTargetLine.Label = "Minimum Target";
            minimumTargetLine.LengthScale = 0.98f;
            minimumTargetLine.ShowInLegend = true; // off by default for lines.
            plotSurface.Add( minimumTargetLine );

            // generate the preferred target line.
            HorizontalLine preferredTargetLine = new HorizontalLine( 228, new Pen( Color.Blue, 3.5f ) );
            preferredTargetLine.Label = "Preferred Target";
            preferredTargetLine.LengthScale = 0.98f;
            preferredTargetLine.ShowInLegend = true; // off by default for lines.
            plotSurface.Add( preferredTargetLine );

            // make some modifications so that chart matches requirements.
            // y axis.
            plotSurface.YAxis1.TicksIndependentOfPhysicalExtent = true;
            plotSurface.YAxis1.TickTextNextToAxis = false;
            plotSurface.YAxis1.TicksAngle = 3.0f * (float)Math.PI / 2.0f;
            ((LinearAxis)plotSurface.YAxis1).LargeTickStep = 10.0;
            ((LinearAxis)plotSurface.YAxis1).NumberOfSmallTicks = 0;

            // x axis
            plotSurface.XAxis1.TicksIndependentOfPhysicalExtent = true;
            plotSurface.XAxis1.TickTextNextToAxis = false;
            plotSurface.XAxis1.TicksAngle = (float)Math.PI / 2.0f;
            LabelAxis la = new LabelAxis( plotSurface.XAxis1 );
            for (int i=0; i<info.Rows.Count; ++i)
            {
                la.AddLabel( (string)info.Rows[i]["StoreName"], Convert.ToInt32(info.Rows[i]["Index"]) );
            }
            la.TicksLabelAngle = (float)90.0f;
            la.TicksBetweenText = true;
            plotSurface.XAxis1 = la;

            plotSurface.XAxis2 = (Axis)plotSurface.XAxis1.Clone();
            plotSurface.XAxis2.HideTickText = true;
            plotSurface.XAxis2.LargeTickSize = 0;

            Legend l = new Legend();
            l.NumberItemsVertically = 2;
            l.AttachTo( xuzhenzhen.com.chart.PlotSurface2D.XAxisPosition.Bottom, xuzhenzhen.com.chart.PlotSurface2D.YAxisPosition.Left );
            l.HorizontalEdgePlacement = xuzhenzhen.com.chart.Legend.Placement.Outside;
            l.VerticalEdgePlacement = xuzhenzhen.com.chart.Legend.Placement.Inside;
            l.XOffset = 5;
            l.YOffset = 50;
            l.BorderStyle = xuzhenzhen.com.chart.LegendBase.BorderType.Line;

            plotSurface.Legend = l;

            plotSurface.Title =
                "Sales Growth Compared to\n" +
                "Average Sales Growth by Store Size - Rank Order Low to High";
            plotSurface.XAxis1.LabelOffset = 0;
            plotSurface.YAxis1.LabelOffset = 0;
            plotSurface.Refresh();
        }
コード例 #2
0
        public void PlotParticles()
        {
            string lines = "Particles Example. Demonstrates -  * How to chart multiple data sets against multiple axes at the same time.";
            infoBox.Text = lines;

            plotSurface.Clear();

            Grid mygrid = new Grid();
            mygrid.HorizontalGridType = Grid.GridType.Fine;
            mygrid.VerticalGridType = Grid.GridType.Fine;
            plotSurface.Add( mygrid );

            // in this example we synthetize a particle distribution
            // in the x-x' phase space and plot it, with the rms Twiss
            // ellipse and desnity distribution
            const int Particle_Number = 500;
            float [] x = new float[Particle_Number];
            float [] y = new float[Particle_Number];
            // Twiss parameters for the beam ellipse
            // 5 mm mrad max emittance, 1 mm beta function
            float alpha, beta, gamma, emit;
            alpha = -2.0f;
            beta = 1.0f;
            gamma = (1.0f + alpha * alpha) / beta;
            emit = 4.0f;

            float da, xmax, xpmax;
            da = -alpha / gamma;
            xmax = (float)Math.Sqrt(emit / gamma);
            xpmax = (float)Math.Sqrt(emit * gamma);

            Random rand = new Random();

            // cheap randomizer on the unit circle
            for (int i = 0; i<Particle_Number; i++)
            {
                float r;
                do
                {
                    x[i] = (float)(2.0f * rand.NextDouble() - 1.0f);
                    y[i] = (float)(2.0f * rand.NextDouble() - 1.0f);
                    r = (float)Math.Sqrt(x[i] * x[i] + y[i] * y[i]);
                } while (r > 1.0f);
            }

            // transform to the tilted twiss ellipse
            for (int i =0; i<Particle_Number; ++i)
            {
                y[i] *= xpmax;
                x[i] = x[i] * xmax + y[i] * da;
            }
            plotSurface.Title = "Beam Horizontal Phase Space and Twiss ellipse";

            PointPlot pp = new PointPlot();
            pp.OrdinateData = y;
            pp.AbscissaData = x;
            pp.Marker = new Marker(Marker.MarkerType.FilledCircle ,4, new Pen(Color.Blue));
            plotSurface.Add(pp, PlotSurface2D.XAxisPosition.Bottom, PlotSurface2D.YAxisPosition.Left);

            // set axes
            LinearAxis lx = (LinearAxis) plotSurface.XAxis1;
            lx.Label = "Position - x [mm]";
            lx.NumberOfSmallTicks = 2;
            LinearAxis ly = (LinearAxis) plotSurface.YAxis1;
            ly.Label = "Divergence - x' [mrad]";
            ly.NumberOfSmallTicks = 2;

            // Draws the rms Twiss ellipse computed from the random data
            float [] xeli=new float [40];
            float [] yeli=new float [40];

            float a_rms, b_rms, g_rms, e_rms;

            Twiss(x, y, out a_rms, out b_rms, out g_rms, out e_rms);
            TwissEllipse(a_rms, b_rms, g_rms, e_rms, ref xeli, ref yeli);

            LinePlot lp = new LinePlot();
            lp.OrdinateData = yeli;
            lp.AbscissaData = xeli;
            plotSurface.Add(lp, PlotSurface2D.XAxisPosition.Bottom, PlotSurface2D.YAxisPosition.Left);
            lp.Pen = new Pen( Color.Red, 2.0f );
            // Draws the ellipse containing 100% of the particles
            // for a uniform distribution in 2D the area is 4 times the rms
            float [] xeli2 = new float [40];
            float [] yeli2 = new float [40];
            TwissEllipse(a_rms, b_rms, g_rms, 4.0F * e_rms, ref xeli2, ref yeli2);

            LinePlot lp2 = new LinePlot();
            lp2.OrdinateData = yeli2;
            lp2.AbscissaData = xeli2;
            plotSurface.Add( lp2, PlotSurface2D.XAxisPosition.Bottom, PlotSurface2D.YAxisPosition.Left );
            Pen p2 = new Pen( Color.Red, 2.0f );
            lp2.Pen = p2;

            // now bin the particle position to create beam density histogram
            float range, min, max;
            min = (float)lx.WorldMin;
            max = (float)lx.WorldMax;
            range = max - min;

            const int Nbin = 30;
            float dx = range / Nbin;
            float [] xbin = new float[Nbin+1];
            float [] xh = new float[Nbin+1];

            for (int j=0; j<=Nbin; ++j)
            {
                xbin[j] = min + j * range;
                if (j < Nbin) xh[j] = 0.0F;
            }
            for (int i =0; i<Particle_Number; ++i)
            {
                if (x[i] >= min && x[i] <= max)
                {
                    int j;
                    j = Convert.ToInt32(Nbin * (x[i] - min) / range);
                    xh[j] += 1;
                }
            }
            StepPlot sp= new StepPlot();
            sp.OrdinateData = xh;
            sp.AbscissaData = new StartStep( min, range / Nbin );
            sp.Center = true;
            plotSurface.Add(sp, PlotSurface2D.XAxisPosition.Bottom, PlotSurface2D.YAxisPosition.Right);
            // axis formatting
            LinearAxis ly2 = (LinearAxis)plotSurface.YAxis2;
            ly2.WorldMin = 0.0f;
            ly2.Label = "Beam Density [a.u.]";
            ly2.NumberOfSmallTicks = 2;
            sp.Pen = new Pen( Color.Green, 2 );
            plotSurface.XAxis1.LabelOffset = 0;
            plotSurface.YAxis1.LabelOffset = 0;
            // Finally, refreshes the plot
            plotSurface.Refresh();
        }
コード例 #3
0
        public void PlotWave()
        {
            string lines = "Sound Wave Example. Demonstrates -  * StepPlot (centered) and HorizontalLine IDrawables * How to set colors of various things. * A few plot interactions. Try left clicking and dragging (a) the axes (b) in the plot region. * In the future I plan add a plot interaction for axis drag that knows if the ctr key is down. This will select between drag/scale";
            infoBox.Text = lines;

            //FileStream fs = new FileStream( @"c:\light.wav", System.IO.FileMode.Open );
            //System.IO.Stream file =Assembly.GetExecutingAssembly().GetManifestResourceStream("Resources.light.wav");

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PlotSurface2DDemo));
            System.Int16[] w = new short[5000];
            byte[] a = (Byte[])resources.GetObject("light");
            //file.Read( a, 0, 10000 );
            for (int i=100; i<5000; ++i)
            {
                w[i] = BitConverter.ToInt16(a,i*2);
            }

            //file.Close();

            plotSurface.Clear();
            plotSurface.AddInteraction(new xuzhenzhen.com.chart.Windows.PlotSurface2D.Interactions.VerticalGuideline());
            plotSurface.AddInteraction(new xuzhenzhen.com.chart.Windows.PlotSurface2D.Interactions.HorizontalRangeSelection(3));
            plotSurface.AddInteraction(new xuzhenzhen.com.chart.Windows.PlotSurface2D.Interactions.AxisDrag(true));

            plotSurface.Add(new HorizontalLine(0.0, Color.LightBlue));

            StepPlot sp = new StepPlot();
            sp.DataSource = w;
            sp.Color = Color.Yellow;
            sp.Center = true;
            plotSurface.Add( sp );

            plotSurface.YAxis1.FlipTicksLabel = true;

            plotSurface.PlotBackColor = Color.DarkBlue;
            plotSurface.BackColor = Color.Black;
            plotSurface.XAxis1.Color = Color.White;
            plotSurface.YAxis1.Color = Color.White;
            plotSurface.XAxis1.LabelOffset = 0;
            plotSurface.YAxis1.LabelOffset = 0;
            plotSurface.Refresh();
        }