Exemple #1
0
        public void Test4()
        {
            Console.WriteLine("Beginning Test4...");
            // Time Values
            DateTime[] timestamps = {
                                        new DateTime(2004, 2, 1, 0, 0, 0),
                                        new DateTime(2004, 2, 1, 2, 0, 0),
                                        new DateTime(2004, 2, 1, 7, 0, 0),
                                        new DateTime(2004, 2, 1, 14, 0, 0),
                                        new DateTime(2004, 2, 1, 17, 0, 0),
                                        new DateTime(2004, 2, 1, 19, 0, 0),
                                        new DateTime(2004, 2, 1, 23, 0, 0),
                                        new DateTime(2004, 2, 2, 0, 0, 0)
                                    };

            // Corresponding Data Values
            double[] values = { 100, 250, 230, 370, 350, 300, 340, 350 };

            LinearInterpolator linear = new LinearInterpolator(timestamps, values);
            CubicSplineInterpolator spline = new CubicSplineInterpolator(timestamps, values);

            // graph range
            RrdGraphDef gDef = new RrdGraphDef(timestamps[0], timestamps[timestamps.Length - 1]);

            // graph title, time and value axis labels
            gDef.Title = "Plottable demonstration";
            gDef.TimeAxisLabel = "time";
            gDef.VerticalLabel = "water level [inches]";

            // interpolated datasources
            gDef.Datasource("linear", linear);
            gDef.Datasource("spline", spline);

            // splined plot will be an orange filled area
            gDef.Area("spline", Color.Orange, "Spline interpolation");

            // simply interpolated plot will be a red line
            gDef.Line("linear", Color.Red, "Linear inteprolation@r");

            // print average values for both interpolation methods
            gDef.Gprint("spline", "AVERAGE", "Average spline value: @0 inches@r");
            gDef.Gprint("linear", "AVERAGE", "Average linear value: @0 inches@r");

            // create the graph...
            RrdGraph graph = new RrdGraph(gDef);

            // ...and save it somewhere
            string filename = "test4.png";
            graph.SaveAsPNG(filename, 300, 100);
            Console.WriteLine("Test4 Complete.");
        }
Exemple #2
0
        public void JonasTest2()
        {
            UInt32 start_time = 1370000000; // 31th may 2013, 11:33:20
            UInt32 end_time = start_time + 300 * 300;
            RrdDef rrdDef = new RrdDef("jonas.rrd");
            rrdDef.StartTime = start_time;
            rrdDef.AddDatasource("battery", "GAUGE", 300, 1.0, 5.0); // Five minutes
            rrdDef.AddArchive("LAST", 0.5, 1, 576);         //
            rrdDef.AddArchive("MIN", 0.5, 1, 576);          // 24 hours average, 2.5 minutes resolution
            rrdDef.AddArchive("MAX", 0.5, 1, 576);          // 24 hours average, 2.5 minutes resolution
            rrdDef.AddArchive("AVERAGE", 0.5, 6, 672);     // 7 days average, 15 min resolution
            rrdDef.AddArchive("MIN", 0.5, 6, 672);         // 7 days average, 15 min resolution
            rrdDef.AddArchive("MAX", 0.5, 6, 672);         // 7 days average, 15 min resolution
            rrdDef.AddArchive("AVERAGE", 0.5, 24, 87600);   // 10 year average, 1 hour resolution
            rrdDef.AddArchive("MIN", 0.5, 24, 87600);       // 10 year average, 1 hour resolution
            rrdDef.AddArchive("MAX", 0.5, 24, 87600);       // 10 year average, 1 hour resolution
            RrdDb db = new RrdDb(rrdDef);
            db.Close();

            RrdDb rrd = new RrdDb("jonas.rrd");
            Random rand = new Random();
            //Sample s = rrd.CreateSample();
            // update
            for(long t = start_time + 300; t <= end_time; t += 300)
            {
                double r = (double)rand.Next(1, 6);
                //s.SetAndUpdate(String.Format("{0}:{1}", t, r));
                Sample sample = rrd.CreateSample(t);
                sample.SetValue("battery", (double)r);
                sample.Update();
                Console.WriteLine(r);
            }

            FetchRequest fr = rrd.CreateFetchRequest("LAST", start_time, end_time - 300);
            FetchData fd = fr.FetchData();
            for (int i = 0; i < fd.RowCount; i++)
            {
                FetchPoint fp = fd.GetRow(i);
                Console.WriteLine("{0}: {1}", fp.Time, fp.Values[0]);
            }

            rrd.Close();

            // Create new graph for the last 12 hours
            RrdGraphDef gdef = new RrdGraphDef(new DateTime(2013, 05, 31, 12, 00, 00), new DateTime(2013, 05, 31, 18, 59, 59));
            gdef.Title = "Battery voltage";
            gdef.VerticalLabel = "U (V)";
            gdef.SetValueAxis(1.0, 1.0);
            gdef.SetGridRange(1.000, 5.000, true);
            gdef.Datasource("vbat", "jonas.rrd", "battery", "LAST");
            //gdef.Datasource("vbat_avg", "jonas.rrd", "battery", "AVERAGE");
            gdef.Datasource("vbat_trend", "vbat,900,TREND");
            gdef.Line("vbat", System.Drawing.Color.Green, "Battery voltage", 1);
            //gdef.Line("vbat_avg", System.Drawing.Color.Red, "Average over 15 min", 1);
            gdef.Line("vbat_trend", System.Drawing.Color.Red, "Trend over 15 min", 1);
            gdef.Gprint("vbat", "LAST", "Current: @2V@r");
            gdef.Gprint("vbat", "AVERAGE", "Average: @2V@r");
            gdef.Gprint("vbat", "MAX", "Max: @2V@r");
            gdef.Gprint("vbat", "MIN", "Min: @2V@r");
            RrdGraph graph = new RrdGraph(gdef);
            graph.SaveAsPNG("battery2_2h.png", 700, 300);
        }