private static void createVectorGraphPNG(string rrdPath, string pngPath, string ds, DateTime start, DateTime end, string vertLabel, double lower = 0, double upper = 0) { RrdGraphDef graphDef = new RrdGraphDef(); graphDef.SetTimePeriod(getUnixTimeStamp(start), getUnixTimeStamp(end)); graphDef.VerticalLabel = vertLabel; if (lower != upper) { graphDef.SetGridRange(lower, upper, true); } string dsx = ds + ".X"; string dsy = ds + ".Y"; string dsz = ds + ".Z"; graphDef.Datasource(dsx, rrdPath, dsx, "AVERAGE"); graphDef.Datasource(dsy, rrdPath, dsy, "AVERAGE"); graphDef.Datasource(dsz, rrdPath, dsz, "AVERAGE"); graphDef.Line(dsx, Color.Blue, dsx, 2); graphDef.Line(dsy, Color.Green, dsy, 2); graphDef.Line(dsz, Color.Red, dsz, 2); RrdGraph graph = new RrdGraph(graphDef); graph.SaveAsPNG(pngPath, 800, 600); }
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); }