Class: Flukso::ScatterPlotDaily

Inherits:
Object
  • Object
show all
Defined in:
lib/flukso/plots.rb

Overview

a simple scatterplot

Instance Method Summary collapse

Constructor Details

#initialize(annotation) ⇒ ScatterPlotDaily

Returns a new instance of ScatterPlotDaily.



25
26
27
28
29
# File 'lib/flukso/plots.rb', line 25

def initialize(annotation)
  @annotation=annotation;
  @readings=Array.new()
  @dataset_id="data"+generateRandomString();
end

Instance Method Details

#addReading(reading) ⇒ Object



30
31
32
# File 'lib/flukso/plots.rb', line 30

def addReading(reading);
  @readings << reading
end

#cmdObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/flukso/plots.rb', line 54

def cmd()
  dataframe=create_dataframe_cmd();
  cmd= <<-END_OF_CMD
    # plot commmands. 
    library(lattice);
    y_max=max(#{@dataset_id}$value);
    title<-paste("Daily Power Consumption", "#{@annotation}");
    data_points<-paste("Number of data points:",nrow(#{@dataset_id}));
    cat("Plotting scatterplot:", title, data_points, "\n");
    xyplot(#{@dataset_id}$value ~ #{@dataset_id}$period, main=title, 
      ylab="Power Usage [W]", xlab="Time Period [15min Intervals]", 
      sub=data_points, ylim=c(0,y_max), xlim=c(0,95));
  END_OF_CMD
  return dataframe << cmd
end

#create_dataframe_cmdObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flukso/plots.rb', line 33

def create_dataframe_cmd()
  cmd_create_dataframe= <<-END_OF_CMD
    #{@dataset_id} <- data.frame( 
      date=character(), 
      dayofweek=character(), 
      period=numeric(), 
      value=numeric() 
    );
  END_OF_CMD
  @readings.each{|reading|
    # calculate some values.
    currenttime=Time.at(reading.utc_timestamp);
    cmd_compose_data= <<-END_OF_CMD
      newline<-data.frame(#{reading.utc_timestamp}, "#{reading.dayOfWeek}", #{reading.period}, #{reading.value}); 
      colnames(newline)<- colnames(#{@dataset_id});
      #{@dataset_id} <- rbind(#{@dataset_id}, newline);
    END_OF_CMD
    cmd_create_dataframe << cmd_compose_data;
  }
  return cmd_create_dataframe
end