Beispiel #1
0
        public HistogramSample()
            : base()
        {
            infoText = "";
            infoText += "Gaussian Example. Demonstrates - \n";
            infoText += "  * HistogramPlot and LinePlot" ;

            plotCanvas.Clear();

            System.Random r = new Random ();

            int len = 35;
            double[] a = new double [len];
            double[] b = new double [len];

            for (int i=0; i<len; ++i) {
                a[i] = Math.Exp (-(double)(i-len/2)*(double)(i-len/2)/50.0);
                b[i] = a[i] + (r.Next(10)/50.0f)-0.05f;
                if (b[i] < 0) {
                    b[i] = 0;
                }
            }

            HistogramPlot sp = new HistogramPlot ();
            sp.DataSource = b;
            sp.BorderColor = Colors.DarkBlue;
            sp.Filled = true;
            sp.FillColor = Colors.Gold; //Gradient (Colors.Lavender, Color.Gold );
            sp.BaseWidth = 0.5;
            sp.Label = "Random Data";

            LinePlot lp = new LinePlot ();
            lp.DataSource = a;
            lp.LineColor = Colors.Blue;
            lp.LineWidth = 3;
            lp.Label = "Gaussian Function";
            plotCanvas.Add (sp);
            plotCanvas.Add (lp);
            plotCanvas.Legend = new Legend ();
            plotCanvas.YAxis1.WorldMin = 0.0;
            plotCanvas.Title = "Histogram Plot";

            PackStart (plotCanvas.Canvas, true);
            Label la = new Label (infoText);
            PackStart (la);
        }
Beispiel #2
0
        public PlotLogo()
        {
            infoText = "";
            infoText += "ABC (logo for australian broadcasting commission) Example. Demonstrates - \n";
            infoText += " * How to set the background of a plotCanvas as an image. \n";
            infoText += " * EqualAspectRatio axis constraint \n";

            plotCanvas.Clear();
            const int size = 200;
            double [] xs = new double [size];
            double [] ys = new double [size];
            for (int i=0; i<size; i++) {
                xs[i] = Math.Sin ((double)i/(size-1)*2.0*Math.PI);
                ys[i] = Math.Cos ((double)i/(size-1)*6.0*Math.PI);
            }

            LinePlot lp = new LinePlot ();
            lp.OrdinateData = ys;
            lp.AbscissaData = xs;
            lp.LineColor = Colors.Yellow;
            lp.LineWidth = 2;
            plotCanvas.Add (lp);
            plotCanvas.Title = "AxisConstraint.EqualScaling in action...";

            // Image downloaded from http://squidfingers.com. Thanks!
            Assembly asm = Assembly.GetExecutingAssembly ();
            Stream file = asm.GetManifestResourceStream ("Samples.Resources.LogoBackground.jpg" );

            Image im = Image.FromStream (file);
            plotCanvas.PlotBackImage = im.ToBitmap ();

            plotCanvas.AddAxesConstraint (new AxesConstraint.AspectRatio (1.0, XAxisPosition.Top, YAxisPosition.Left) );

            plotCanvas.XAxis1.WorldMin = plotCanvas.YAxis1.WorldMin;
            plotCanvas.XAxis1.WorldMax = plotCanvas.YAxis1.WorldMax;

            PackStart (plotCanvas.Canvas, true);
            Label la = new Label (infoText);
            PackStart (la);
        }
Beispiel #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="lp1">LinePlot that provides bounds to filled region [upper or lower]</param>
 /// <param name="lp2">LinePlot that provides bounds to filled region [upper or lower]</param>
 /// <remarks>TODO: make this work with other plot types.</remarks>
 public FilledRegion(LinePlot plot1, LinePlot plot2)
 {
     lp1 = plot1;
     lp2 = plot2;
 }
Beispiel #4
0
        public TradingSample()
            : base()
        {
            infoText = "";
            infoText += "Stock Dataset Sample. Demonstrates - \n";
            infoText += " * CandlePlot, FilledRegion, LinePlot and ArrowItem IDrawables \n";
            infoText += " * DateTime axes \n";
            infoText += " * Horizontal Drag Interaction - try dragging the plot surface \n";
            infoText += " * Axis Drag Interaction - try dragging in the horizontal and vertical Axis areas";

            plotCanvas.Clear ();
            // [NOTIMP] plotCanvas.DateTimeToolTip = true;

            // obtain stock information from xml file
            DataSet ds = new DataSet();
            System.IO.Stream file =
                Assembly.GetExecutingAssembly ().GetManifestResourceStream ("Samples.Resources.asx_jbh.xml");
            ds.ReadXml (file, System.Data.XmlReadMode.ReadSchema);
            DataTable dt = ds.Tables[0];
            // DataView dv = new DataView( dt );

            // create CandlePlot.
            CandlePlot cp = new CandlePlot ();
            cp.DataSource = dt;
            cp.AbscissaData = "Date";
            cp.OpenData = "Open";
            cp.LowData = "Low";
            cp.HighData = "High";
            cp.CloseData = "Close";
            cp.BearishColor = Colors.Red;
            cp.BullishColor = Colors.Green;
            cp.Style = CandlePlot.Styles.Filled;

            // calculate 10 day moving average and 2*sd line
            ArrayList av10 = new ArrayList();
            ArrayList sd2_10 = new ArrayList();
            ArrayList sd_2_10 = new ArrayList();
            ArrayList dates = new ArrayList();
            for (int i=0; i<dt.Rows.Count-10; ++i) {
                float sum = 0.0f;
                for (int j=0; j<10; ++j) {
                    sum += (float)dt.Rows[i+j]["Close"];
                }
                float average = sum / 10.0f;
                av10.Add (average);
                sum = 0.0f;
                for (int j=0; j<10; ++j) {
                    sum += ((float)dt.Rows[i+j]["Close"]-average)*((float)dt.Rows[i+j]["Close"]-average);
                }
                sum /= 10.0f;
                sum = 2.0f * (float)Math.Sqrt (sum);
                sd2_10.Add (average + sum);
                sd_2_10.Add (average - sum);
                dates.Add ((DateTime)dt.Rows[i+10]["Date"]);
            }

            // and a line plot of close values.
            LinePlot av = new LinePlot ();
            av.OrdinateData = av10;
            av.AbscissaData = dates;
            av.LineColor = Colors.DarkGray;
            av.LineWidth = 2.0;

            LinePlot top = new LinePlot ();
            top.OrdinateData = sd2_10;
            top.AbscissaData = dates;
            top.LineColor = Colors.LightBlue;
            top.LineWidth = 2.0;

            LinePlot bottom = new LinePlot ();
            bottom.OrdinateData = sd_2_10;
            bottom.AbscissaData = dates;
            bottom.LineColor = Colors.LightBlue;
            bottom.LineWidth = 2.0;

            FilledRegion fr = new FilledRegion (top, bottom);
            fr.FillColor = Colors.LightGreen;

            // Note: order of adding FilledRegion, Plots, etc is important for visibility
            plotCanvas.Add (fr);
            plotCanvas.Add (new Grid());

            plotCanvas.Add (av);
            plotCanvas.Add (top);
            plotCanvas.Add (bottom);
            plotCanvas.Add (cp);

            // now make an arrow...
            ArrowItem arrow = new ArrowItem (new Point (((DateTime)dt.Rows[60]["Date"]).Ticks, 2.28), -80, "An interesting flat bit");
            arrow.ArrowColor = Colors.DarkBlue;
            arrow.PhysicalLength = 50;

            plotCanvas.Add (arrow);

            plotCanvas.Title = "Stock Prices";
            plotCanvas.XAxis1.Label = "Date / Time";
            plotCanvas.XAxis1.WorldMin += plotCanvas.XAxis1.WorldLength / 4.0;
            plotCanvas.XAxis1.WorldMax -= plotCanvas.XAxis1.WorldLength / 2.0;
            plotCanvas.YAxis1.Label = "Price [$]";

            plotCanvas.XAxis1 = new TradingDateTimeAxis (plotCanvas.XAxis1);

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

            PackStart (plotCanvas.Canvas, true);
            Label la = new Label (infoText);
            PackStart (la);
        }
Beispiel #5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="lp1">LinePlot that provides bounds to filled region [upper or lower]</param>
 /// <param name="lp2">LinePlot that provides bounds to filled region [upper or lower]</param>
 /// <remarks>TODO: make this work with other plot types.</remarks>
 public FilledRegion(LinePlot plot1, LinePlot plot2)
 {
     lp1 = plot1;
     lp2 = plot2;
 }
Beispiel #6
0
        public PointPlotSample()
            : base()
        {
            infoText = "";
            infoText += "Sinc Function Example. Demonstrates - \n";
            infoText += " * Charting LinePlot and PointPlot at the same time. \n";
            infoText += " * Adding a legend.";

            plotCanvas.Clear(); // clear everything. reset fonts. remove plot components etc.

            System.Random r = new Random();
            double[] a = new double[100];
            double[] b = new double[100];
            double mult = 0.00001f;
            for (int i=0; i<100; ++i) {
                a[i] = ((double)r.Next(1000)/5000.0f-0.1f)*mult;
                if (i == 50) {
                    b[i] = 1.0f*mult;
                }
                else {
                    b[i] = Math.Sin ((((double)i-50.0)/4.0))/(((double)i-50.0)/4.0);
                    b[i] *= mult;
                }
                a[i] += b[i];
            }

            Marker m = new Marker (Marker.MarkerType.Cross1, 6, Colors.Blue);
            PointPlot pp = new PointPlot (m);
            pp.OrdinateData = a;
            pp.AbscissaData = new StartStep (-500.0, 10.0);
            pp.Label = "Random";
            plotCanvas.Add (pp);

            LinePlot lp = new LinePlot ();
            lp.OrdinateData = b;
            lp.AbscissaData = new StartStep( -500.0, 10.0 );
            lp.LineColor = Colors.Red;
            lp.LineWidth = 2;
            plotCanvas.Add (lp);

            plotCanvas.Title = "Sinc Function";
            plotCanvas.YAxis1.Label = "Magnitude";
            plotCanvas.XAxis1.Label = "Position";

            Legend legend = new Legend();
            legend.AttachTo (XAxisPosition.Top, YAxisPosition.Left);
            legend.VerticalEdgePlacement = Legend.Placement.Inside;
            legend.HorizontalEdgePlacement = Legend.Placement.Inside;
            legend.YOffset = 8;

            plotCanvas.Legend = legend;
            plotCanvas.LegendZOrder = 1; // default zorder for adding idrawables is 0, so this puts legend on top.

            PackStart (plotCanvas.Canvas, true);
            Label la = new Label (infoText);
            PackStart (la);
        }
Beispiel #7
0
        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);
        }