コード例 #1
0
ファイル: PlotParticles.cs プロジェクト: hwthomas/XwPlot
        public PlotParticles()
        {
            infoText = "";
            infoText += "Particles Example. Demonstrates - \n";
            infoText += " * How to chart multiple data sets against multiple axes at the same time.";

            plotCanvas.Clear();

            Grid mygrid = new Grid ();
            mygrid.HorizontalGridType = Grid.GridType.Fine;
            mygrid.VerticalGridType = Grid.GridType.Fine;
            plotCanvas.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;
            double [] x = new double [Particle_Number];
            double [] y = new double [Particle_Number];
            // Twiss parameters for the beam ellipse
            // 5 mm mrad max emittance, 1 mm beta function
            double alpha, beta, gamma, emit;
            alpha = -2.0;
            beta = 1.0;
            gamma = (1.0 + alpha * alpha) / beta;
            emit = 4.0;

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

            Random rand = new Random ();

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

            // 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;
            }
            plotCanvas.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, Colors.Blue);
            plotCanvas.Add (pp, XAxisPosition.Bottom, YAxisPosition.Left);

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

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

            double 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;
            plotCanvas.Add (lp, XAxisPosition.Bottom, YAxisPosition.Left);
            lp.LineColor = Colors.Red;
            lp.LineWidth = 2;
            // Draws the ellipse containing 100% of the particles
            // for a uniform distribution in 2D the area is 4 times the rms
            double [] xeli2 = new double [40];
            double [] yeli2 = new double [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;
            plotCanvas.Add (lp2, XAxisPosition.Bottom, YAxisPosition.Left);
            double[] pattern = new double[] { 5, 20 };
            lp2.LineDash = pattern;
            lp2.LineColor = Colors.Red;

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

            const int Nbin = 30;
            double[] xbin = new double[Nbin+1];
            double[] xh = new double [Nbin+1];

            for (int j=0; j<=Nbin; ++j){
                xbin[j] = min + j * range;
                if (j < Nbin) xh[j] = 0.0;
            }
            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;
            plotCanvas.Add (sp, XAxisPosition.Bottom, YAxisPosition.Right);
            // axis formatting
            LinearAxis ly2 = (LinearAxis)plotCanvas.YAxis2;
            ly2.WorldMin = 0.0f;
            ly2.Label = "Beam Density [a.u.]";
            ly2.NumberOfSmallTicks = 2;
            sp.Color = Colors.Green;

            PackStart (plotCanvas.Canvas, true);
            Label la = new Label (infoText);
            PackStart (la);
        }
コード例 #2
0
ファイル: StepPlotSample.cs プロジェクト: hwthomas/XwPlot
        public StepPlotSample()
            : base()
        {
            infoText = "";
            infoText += "Sound Wave Example. Demonstrates - \n";
            infoText += " * StepPlot (centered) and HorizontalLine IDrawables \n";
            infoText += " * Vertical ColorGradient plotBackground \n";
            //infoText += " * AxisDrag Interaction - try left clicking and dragging X or Y axes \n";
            //infoText += " * Vertical & Horizontal GuideLines - without fragmentation problems! \n";
            //infoText += " * Rubberband Selection - click and drag to zoom an area of the plot \n";
            //infoText += " * Key actions : +,- zoom, left/right/up/down pan, Home restores original scale and origin";

            Assembly asm = Assembly.GetExecutingAssembly ();

            Stream file = asm.GetManifestResourceStream ("Samples.Resources.sound.wav");

            byte[] a = new byte[10000];
            System.Int16[] v = new short[5000];
            System.Int16[] w = new short[1000];

            file.Read (a, 0, 10000);
            for (int i=100; i<5000; ++i) {
                v[i] = BitConverter.ToInt16 (a,i*2);
            }
            file.Close();
            // Select only every 5th sample, so data size = 1000 points
            for (int i=1; i<1000; ++i) {
                w[i] = v[i*5];
            }

            plotCanvas.Clear();

            plotCanvas.AddInteraction (new KeyActions ());
            //plotCanvas.AddInteraction (new AxisDrag ());
            //plotCanvas.AddInteraction (new PlotSelection (Color.Gray));
            //plotCanvas.AddInteraction (new VerticalGuideline (Color.Gray));
            //plotCanvas.AddInteraction (new HorizontalGuideline (Color.Gray));

            plotCanvas.Add (new HorizontalLine (2500.0, Colors.LightBlue));

            StepPlot sp = new StepPlot ();
            sp.DataSource = w;
            sp.Color = Colors.Black;
            sp.Center = true;
            plotCanvas.Add (sp);

            plotCanvas.YAxis1.FlipTickText = true;

            plotCanvas.Canvas.BackgroundColor = new Color (0.375, 0.375, 0.375);

            ColorGradient g = new ColorGradient ();
            g.StartColor = new Color (0.5, 0.5, 1);
            g.EndColor = new Color (0.5, 1, 0.5);
            plotCanvas.PlotBackGradient = g;

            plotCanvas.XAxis1.LineColor = Colors.White;
            plotCanvas.YAxis1.LineColor = Colors.White;

            PackStart (plotCanvas.Canvas, true);
            Label la = new Label (infoText);
            PackStart (la);
        }