Class: Sol::Dashboard

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/dashboard.rb

Overview

This class executes in another thread than the GUI thread. Communication between the Dashboard and the GUI (WebView) is done through the Bridge class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data, dimension_labels, date_columns = []) ⇒ Dashboard





56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_rich/dashboard.rb', line 56

def initialize(name, data, dimension_labels, date_columns = [])
  
  @name = name
  @data = data
  @dimension_labels = dimension_labels
  @date_columns = date_columns

  # Access the bridge to communicate with DCFX. Bridge is a singleton class
  @bridge = Bridge.instance

  # prepare a bootstrap scene specification for this dashboard
  @scene = Bootstrap.new
  
  @charts = Hash.new
  @properties = Hash.new
  @base_dimensions = Hash.new
  @runned  = false                      # dashboard has never executed

  # adds the dashboard data to the Browser
  add_data
  
end

Instance Attribute Details

#base_dimensionsObject (readonly)

dimensions used by crossfilter



50
51
52
# File 'lib/ruby_rich/dashboard.rb', line 50

def base_dimensions
  @base_dimensions
end

#chartsObject (readonly)

All the charts to be added to the dashboard



46
47
48
# File 'lib/ruby_rich/dashboard.rb', line 46

def charts
  @charts
end

#dataObject (readonly)

Returns the value of attribute data.



37
38
39
# File 'lib/ruby_rich/dashboard.rb', line 37

def data
  @data
end

#date_columnsObject (readonly)

columns that have date information



39
40
41
# File 'lib/ruby_rich/dashboard.rb', line 39

def date_columns
  @date_columns
end

#dimension_labelsObject (readonly)

Returns the value of attribute dimension_labels.



38
39
40
# File 'lib/ruby_rich/dashboard.rb', line 38

def dimension_labels
  @dimension_labels
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/ruby_rich/dashboard.rb', line 36

def name
  @name
end

#propertiesObject (readonly)

list of properties to be added to the dashboard. These are Javascript sentences that will be added at the right time to the embedded browser. Dashboard properties should be added before charts are added.



44
45
46
# File 'lib/ruby_rich/dashboard.rb', line 44

def properties
  @properties
end

#sceneObject (readonly)

Returns the value of attribute scene.



47
48
49
# File 'lib/ruby_rich/dashboard.rb', line 47

def scene
  @scene
end

#scriptObject (readonly)

automatically generated javascript script for this dashboard



48
49
50
# File 'lib/ruby_rich/dashboard.rb', line 48

def script
  @script
end

Instance Method Details

#chart(type, x_column, y_column, name) ⇒ Object


Create a new chart of the given type and name, usign x_column for the x_axis and y_column for the Y axis. Set the default values for the chart. Those values can be changed by the user later.




121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ruby_rich/dashboard.rb', line 121

def chart(type, x_column, y_column, name)

  prepare_dimension(x_column, x_column) if (@base_dimensions[x_column + "Dimension"] == nil)

  chart = Sol::Chart.build(type, x_column, y_column, name)

  # Set chart defaults. Should preferably be read from a config file 
  chart.elastic_y(true)
  chart.x_axis_label(x_column)
  chart.y_axis_label(y_column)
  chart.group(:reduce_sum)

  @charts[name] = chart
  chart

end

#cleanObject


Cleans the scene and the charts, preparing for new visualization.




251
252
253
254
# File 'lib/ruby_rich/dashboard.rb', line 251

def clean
  @scene = Bootstrap.new
  @charts = Hash.new
end

#dimension?(dim_name) ⇒ Boolean



Returns:

  • (Boolean)


103
104
105
# File 'lib/ruby_rich/dashboard.rb', line 103

def dimension?(dim_name)
  !@base_dimension[Sol.camelcase(dim_name.to_s)].nil?
end

#dimensions_specObject


Converts the @base_dimensions into Javascript code to define the crossfilters´dimensions.




143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby_rich/dashboard.rb', line 143

def dimensions_spec

  facts = "#{@name.downcase}_facts"

  dim_spec = String.new
  @base_dimensions.each_pair do |key, value|
    dim_spec << "var #{key} = #{facts}.dimension(function(d) {return d[\"#{value}\"];});"
  end
  dim_spec

end

#plotObject


Launches the UI and passes self so that it can add elements to it.




260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/ruby_rich/dashboard.rb', line 260

def plot

  # Remove all elements from the dashboard.  This could be changed in future releases
  # of the library.
  B.delete_all

  if (!@runned )
    run
    clean
    runned = true
  else
    re_run
  end

end

#prepare_dimension(dim_name, dim) ⇒ Object





94
95
96
97
# File 'lib/ruby_rich/dashboard.rb', line 94

def prepare_dimension(dim_name, dim)
  @base_dimensions[dim_name + "Dimension"] = dim
  return self
end

#propsObject


Prepare dashboard data and properties




159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ruby_rich/dashboard.rb', line 159

def props

  dashboard = "#{@name.downcase}_dashboard"
  facts = "#{@name.downcase}_facts"
  data = "#{@name.downcase}_data"
  
  # convert the data to JSON format
  scrpt = "\n    var \#{dashboard} = new DCDashboard();\n    \#{dashboard}.convert(\#{@date_columns});\n    // Make variable data accessible to all charts\n    var \#{data} = \#{dashboard}.getData();\n    //$('#help').append(JSON.stringify(\#{data}));\n    // add data to crossfilter and call it 'facts'.\n    \#{facts} = crossfilter(\#{data});\n\n  EOS\n\n  @properties.each_pair do |key, value|\n    scrpt << value\n  end\n\n  scrpt\n\nend\n"

#re_runObject


When we re_run a script, there is no need to add the dashboard properties again




224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/ruby_rich/dashboard.rb', line 224

def re_run

  # scrpt will have the javascript specification
  scrpt = String.new

  # add bootstrap container if it wasn't specified by the user
  @scene.create_grid((keys = @charts.keys).size, keys) if !@scene.specified?
  scrpt << @scene.bootstrap

  # add charts
  @charts.each do |name, chart|
    # add the chart specification
    scrpt << chart.js_spec if !chart.nil?
  end
  
  # render all charts
  scrpt += "dc.renderAll();"

  # sends a message to the gui to execute the given script
  @bridge.send(:gui, :executeScript, scrpt)
  
end

#runObject





190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ruby_rich/dashboard.rb', line 190

def run

  # scrpt will have the javascript specification
  scrpt = String.new
  
  # add dashboard properties
  scrpt << props

  # add bootstrap container if it wasn't specified by the user.  
  @scene.create_grid((keys = @charts.keys).size, keys) if !@scene.specified?
  scrpt << @scene.bootstrap
  
  # add dimensions (the x dimension)
  scrpt << dimensions_spec
  
  # add charts
  @charts.each do |name, chart|
    # add the chart specification
    scrpt << chart.js_spec if !chart.nil?
  end
  
  # render all charts
  scrpt += "dc.renderAll();"

  # sends a message to the gui to execute the given script
  @bridge.send(:gui, :executeScript, scrpt)

end

#set_demo_script(scrpt) ⇒ Object





280
281
282
# File 'lib/ruby_rich/dashboard.rb', line 280

def set_demo_script(scrpt)
  @demo_script = scrpt
end

#time_format(val = nil) ⇒ Object


Property that defines how to format date information. Uses d3 time format.




83
84
85
86
87
# File 'lib/ruby_rich/dashboard.rb', line 83

def time_format(val = nil)
  return @properties["timeFormat"] if !val
  @properties["timeFormat"] = "var timeFormat = d3.time.format(\"#{val}\");"
  return self
end

#title=(title) ⇒ Object





111
112
113
# File 'lib/ruby_rich/dashboard.rb', line 111

def title=(title)
  @scene.title=(title)
end