Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use Plotting module.

  • Loading branch information...
commit bd7354a74ee97be98090faab983714374e6169bc 1 parent f3522c9
Martijn Koedam authored
Showing with 193 additions and 156 deletions.
  1. +191 −149 Plotting.vala
  2. +2 −7 main.vala
View
340 Plotting.vala
@@ -1,200 +1,242 @@
using GLib;
using Gtk;
-void plot_weekday(EnergyStorage es, string[] argv, uint offset)
+class Plotting : Module
{
- DateTime tstart = es.get_starting_datetime();
- DateTime tstop = es.get_stopping_datetime();
-
- // Parse range.
- for(uint i = offset; i < argv.length; i++)
- {
- if(argv[i] == "range") {
- i += parse_range(argv, i+1, ref tstart, ref tstop);
- }
+ private enum PlotType {
+ POINTS,
+ AVG_WEEKDAY,
+ DAYS,
+ WEEKS,
+ MONTHS,
+ NUM_PLOT_TYPES
}
- double week[7] = {0};
- int num_week[7] = {0};
+ private EnergyStorage es = null;
- var start = tstart;
- var stop = start.add_minutes(-start.get_minute());
- stop = stop.add_hours(-stop.get_hour());
- while(stop.compare(tstop)< 0)
- {
- start = stop;
- int d = start.get_day_of_week();
- stop = start.add_days(1);
- week[d-1] += es.get_average_energy(start, stop);
- num_week[d-1]++;
+ private PlotType plot_type = PlotType.POINTS;
+ private DateTime tstart;
+ private DateTime tstop;
- }
- // Draw graph.
- Gtk.init(ref argv);
- Gtk.Window win = new Gtk.Window();
-
- win.delete_event.connect((source) =>{
- Gtk.main_quit();
- return false;
- });
- // Set default size
- win.set_default_size(800, 600);
-
- var a = new Graph.Widget();
-
- a.graph.title_label = "Power consumption";
- a.graph.y_axis_label = "Energy (kWh)";
- a.graph.x_axis_label = "Week day";
-
- a.graph.min_y_point = 0;
- var ds = a.graph.create_data_set_bar();
- ds.set_color(0.4,0.5,0.3);
-
- a.graph.add_xticks(0.5, "Monday");
- a.graph.add_xticks(1.5, "Tuesday");
- a.graph.add_xticks(2.5, "Wednesday");
- a.graph.add_xticks(3.5, "Thursday");
- a.graph.add_xticks(4.5, "Friday");
- a.graph.add_xticks(5.5, "Saturday");
- a.graph.add_xticks(6.5, "Sunday");
- ds.add_point(0, 0);
- for(uint i = 0; i < 7; i++)
+ public Plotting(EnergyStorage es)
{
- if(num_week[i] > 0)
- {
- ds.add_point(i+1,
- 24/1000.0*week[i]/(double)num_week[i]);
- }else{
- ds.add_point(i+1, 0);
- }
+ this.es = es;
+
}
- win.add(a);
- win.show_all();
- Gtk.main();
-}
+ public override void print_help()
+ {
+ stdout.printf("""
+ Usage plotting:
+ ep plotting <options> <commands>
-/**
- * Plotting graph
- */
-void plot_graph(EnergyStorage es, string[] argv, uint offset)
-{
- bool do_average = false;
- bool do_bars = false;
- bool do_points = false;
- DateTime tstart = es.get_starting_datetime();
- DateTime tstop = es.get_stopping_datetime();
+ commands:
+points: Plot all the data points. (use in combination with range)
+weekday: Shows the average power consumption for each day of the week.
+
+Options:
+--help, help print this help message.
+range <start date> <end date> limit the evaluated data to a certain range.
+
+Example:
+""");
+ }
- for(uint i = offset; i < argv.length; i++)
+ public override bool parse_arguments(string[] argv)
{
- if(argv[i] == "average")
+ Gtk.init(ref argv);
+ tstart = es.get_starting_datetime();
+ tstop = es.get_stopping_datetime();
+ for(uint i = 0; i < argv.length; i++)
{
- do_average = true;
+ if(argv[i] == "help" || argv[i] == "--help") {
+ print_help();
+ return false;
+ } else if (argv[i] == "range") {
+ i += parse_range(argv, i+1, ref tstart, ref tstop);
+ } else if (argv[i] == "weekday") {
+ plot_type = PlotType.AVG_WEEKDAY;
+ }else if (argv[i] == "points") {
+ plot_type = PlotType.POINTS;
+ }else if (argv[i] == "days") {
+ plot_type = PlotType.DAYS;
+ } else {
+ print_help();
+ return false;
+ }
}
- // Plot hour bars.
- else if (argv[i] == "bars")
+ return true;
+ }
+ public override int execute()
+ {
+ if(plot_type == PlotType.POINTS)
{
- do_bars = true;
+ plot_graph();
}
- else if (argv[i] == "points")
+ else if (plot_type == PlotType.AVG_WEEKDAY)
{
- do_points = true;
+ plot_weekday();
}
- // Parse range.
- else if(argv[i] == "range")
+
+ return 0;
+ }
+
+ private void plot_weekday()
+ {
+ double week[7] = {0};
+ int num_week[7] = {0};
+
+ var start = tstart;
+ var stop = start.add_minutes(-start.get_minute());
+ stop = stop.add_hours(-stop.get_hour());
+ while(stop.compare(tstop)< 0)
{
- i += parse_range(argv, i+1, ref tstart, ref tstop);
+ start = stop;
+ int d = start.get_day_of_week();
+ stop = start.add_days(1);
+
+ week[d-1] += es.get_average_energy(start, stop);
+ num_week[d-1]++;
+
}
- else
+
+ // Draw graph.
+ Gtk.Window win = new Gtk.Window();
+
+ win.delete_event.connect((source) =>{
+ Gtk.main_quit();
+ return false;
+ });
+ // Set default size
+ win.set_default_size(800, 600);
+
+ var a = new Graph.Widget();
+
+ a.graph.title_label = "Power consumption";
+ a.graph.y_axis_label = "Energy (kWh)";
+ a.graph.x_axis_label = "Week day";
+
+ a.graph.min_y_point = 0;
+ var ds = a.graph.create_data_set_bar();
+ ds.set_color(0.4,0.5,0.3);
+
+ a.graph.add_xticks(0.5, "Monday");
+ a.graph.add_xticks(1.5, "Tuesday");
+ a.graph.add_xticks(2.5, "Wednesday");
+ a.graph.add_xticks(3.5, "Thursday");
+ a.graph.add_xticks(4.5, "Friday");
+ a.graph.add_xticks(5.5, "Saturday");
+ a.graph.add_xticks(6.5, "Sunday");
+ ds.add_point(0, 0);
+ for(uint i = 0; i < 7; i++)
{
- stdout.printf("Failed to parse commandline: %s\n", argv[i]);
+ if(num_week[i] > 0)
+ {
+ ds.add_point(i+1,
+ 24/1000.0*week[i]/(double)num_week[i]);
+ }else{
+ ds.add_point(i+1, 0);
+ }
}
+ win.add(a);
+ win.show_all();
+ Gtk.main();
}
- if(!do_bars) do_points = true;
- tstart = es.get_starting_datetime(tstart);
- tstop = es.get_stopping_datetime(tstop);
- // Draw graph.
- Gtk.init(ref argv);
- Gtk.Window win = new Gtk.Window();
+ /**
+ * Plotting graph
+ */
+ void plot_graph()
+ {
+ bool do_average = false;
+ bool do_bars = false;
+ bool do_points = false;
- win.delete_event.connect((source) =>{
- Gtk.main_quit();
- return false;
- });
- // Set default size
- win.set_default_size(800, 600);
+ if(!do_bars) do_points = true;
+ tstart = es.get_starting_datetime(tstart);
+ tstop = es.get_stopping_datetime(tstop);
- double avg = es.get_average_energy(tstart, tstop);
- //double eng = es.get_energy(tstart, tstop);
+ // Draw graph.
- var a = new Graph.Widget();
+ Gtk.Window win = new Gtk.Window();
- a.graph.title_label = "Power consumption";
- a.graph.y_axis_label = "Power (W)";
- a.graph.x_axis_label = "Time (HH:MM)";
+ win.delete_event.connect((source) =>{
+ Gtk.main_quit();
+ return false;
+ });
+ // Set default size
+ win.set_default_size(800, 600);
- a.graph.min_y_point = 0;
- var ds = a.graph.create_data_set_area();
- ds.set_color(0.4,0.5,0.3);
+ double avg = es.get_average_energy(tstart, tstop);
+ //double eng = es.get_energy(tstart, tstop);
- // if we need average.
- if(do_average)
- {
- var ds2 = new Graph.DataSetAverage(ds);
- a.graph.add_data_set(ds2);
- ds2.set_color(0.2,0.2,0.4);
- }
- if (do_bars)
- {
- var ds3 = a.graph.create_data_set_bar();
- ds3.set_color(0.5,0.2,0.2);
+ var a = new Graph.Widget();
+ a.graph.title_label = "Power consumption";
+ a.graph.y_axis_label = "Power (W)";
+ a.graph.x_axis_label = "Time (HH:MM)";
- var start = tstart;
- var stop = start.add_minutes(-start.get_minute());
- ds3.add_point((double)stop.to_unix(), 0);
+ a.graph.min_y_point = 0;
+ var ds = a.graph.create_data_set_area();
+ ds.set_color(0.4,0.5,0.3);
- while(stop.compare(tstop)< 0)
+ // if we need average.
+ if(do_average)
{
- start = stop;
- stop = start.add_hours(1);
+ var ds2 = new Graph.DataSetAverage(ds);
+ a.graph.add_data_set(ds2);
+ ds2.set_color(0.2,0.2,0.4);
+ }
+ if (do_bars)
+ {
+ var ds3 = a.graph.create_data_set_bar();
+ ds3.set_color(0.5,0.2,0.2);
+
+
+ var start = tstart;
+ var stop = start.add_minutes(-start.get_minute());
+ ds3.add_point((double)stop.to_unix(), 0);
+
+ while(stop.compare(tstop)< 0)
+ {
+ start = stop;
+ stop = start.add_hours(1);
- avg = es.get_energy(start,stop);
- ds3.add_point((double)stop.to_unix(), avg);
+ avg = es.get_energy(start,stop);
+ ds3.add_point((double)stop.to_unix(), avg);
+ }
}
- }
- // add zero point.
- if(do_points)
- {
- ds.add_point((double)tstart.to_unix(), 0);
- foreach ( EnergyPoint ep in es.get_data(tstart, tstop))
+ // add zero point.
+ if(do_points)
{
- ds.add_point((double)ep.time.to_unix(), ep.power);
+ ds.add_point((double)tstart.to_unix(), 0);
+ foreach ( EnergyPoint ep in es.get_data(tstart, tstop))
+ {
+ ds.add_point((double)ep.time.to_unix(), ep.power);
+ }
}
- }
- var start = tstart;
- var stop = start.add_minutes(-start.get_minute());
- // ticks.
- while(stop.compare(tstop)< 0)
- {
- start = stop;
- stop = start.add_hours(1);
- string sa = "%02d:%02d".printf(start.get_hour(), start.get_minute());
- a.graph.add_xticks((double)start.to_unix(),sa);
+ var start = tstart;
+ var stop = start.add_minutes(-start.get_minute());
+ // ticks.
+ while(stop.compare(tstop)< 0)
+ {
+ start = stop;
+ stop = start.add_hours(1);
+ string sa = "%02d:%02d".printf(start.get_hour(), start.get_minute());
+ a.graph.add_xticks((double)start.to_unix(),sa);
- }
+ }
- win.add(a);
+ win.add(a);
- win.show_all();
- Gtk.main();
+ win.show_all();
+ Gtk.main();
+ }
}
-
View
9 main.vala
@@ -55,14 +55,9 @@ int main (string[] argv)
/**
* Plot graphs
*/
- else if (argv[1] == "graph")
+ else if (argv[1] == "plot")
{
- if(argv.length > 1 && argv[2] == "weekday")
- {
- plot_weekday(es, argv, 2);
- }else{
- plot_graph(es, argv, 2);
- }
+ module = new Plotting(es);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.