Class: CDK::GRAPH

Inherits:
CDKOBJS show all
Defined in:
lib/cdk/components/graph.rb

Instance Attribute Summary

Attributes included from HasTitle

#title_attrib

Attributes included from HasScreen

#is_visible, #screen, #screen_index

Attributes included from ExitConditions

#exit_type

Attributes included from Bindings

#binding_list

Attributes included from Focusable

#accepts_focus, #has_focus

Attributes included from Borders

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #border_size, #box

Instance Method Summary collapse

Methods inherited from CDKOBJS

#setBackgroundColor, #timeout, #validCDKObject, #validObjType

Methods included from WindowHooks

#refreshData, #saveData

Methods included from WindowInput

#getc, #getch, #inject, #setPostProcess, #setPreProcess

Methods included from HasTitle

#cleanTitle, #drawTitle, #init_title, #setTitle

Methods included from HasScreen

#SCREEN_XPOS, #SCREEN_YPOS, #init_screen, #wrefresh

Methods included from ExitConditions

#init_exit_conditions, #resetExitType, #setExitType

Methods included from Bindings

#bind, #bindableObject, #checkBind, #cleanBindings, #init_bindings, #isBind, #unbind

Methods included from Focusable

#focus, #init_focus, #unfocus

Methods included from Borders

#getBox, #init_borders, #setBXattr, #setBox, #setHZchar, #setLLchar, #setLRchar, #setULchar, #setURchar, #setVTchar

Methods included from Movement

#move_specific

Methods included from Converters

#char2Chtype, #charOf, #chtype2Char, #chtype2String, #decode_attribute, #encode_attribute

Methods included from Justifications

#justify_string

Methods included from Alignments

#alignxy

Constructor Details

#initialize(cdkscreen, xplace, yplace, height, width, title, xtitle, ytitle) ⇒ GRAPH

Returns a new instance of GRAPH.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cdk/components/graph.rb', line 5

def initialize(cdkscreen, xplace, yplace, height, width,
    title, xtitle, ytitle)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy

  self.setBox(false)

  box_height = CDK.setWidgetDimension(parent_height, height, 3)
  box_width = CDK.setWidgetDimension(parent_width, width, 0)
  box_width = self.setTitle(title, box_width)
  box_height += @title_lines
  box_width = [parent_width, box_width].min
  box_height = [parent_height, box_height].min

  # Rejustify the x and y positions if we need to
  xtmp = [xplace]
  ytmp = [yplace]
  alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Create the widget pointer
  @screen = cdkscreen
  @parent = cdkscreen.window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)
  @box_height = box_height
  @box_width = box_width
  @minx = 0
  @maxx = 0
  @xscale = 0
  @yscale = 0
  @count = 0
  @display_type = :LINE

  if @win.nil?
    self.destroy
    return nil
  end
  @win.keypad(true)

  # Translate the X axis title string to a chtype array
  if !(xtitle.nil?) && xtitle.size > 0
    xtitle_len = []
    xtitle_pos = []
    @xtitle = char2Chtype(xtitle, xtitle_len, xtitle_pos)
    @xtitle_len = xtitle_len[0]
    @xtitle_pos = justify_string(@box_height, @xtitle_len, xtitle_pos[0])

  else
    xtitle_len = []
    xtitle_pos = []
    @xtitle = char2Chtype("<C></5>X Axis", xtitle_len, xtitle_pos)
    @xtitle_len = title_len[0]
    @xtitle_pos = justify_string(@box_height, @xtitle_len, xtitle_pos[0])
  end

  # Translate the Y Axis title string to a chtype array
  if !(ytitle.nil?) && ytitle.size > 0
    ytitle_len = []
    ytitle_pos = []
    @ytitle = char2Chtype(ytitle, ytitle_len, ytitle_pos)
    @ytitle_len = ytitle_len[0]
    @ytitle_pos = justify_string(@box_width, @ytitle_len, ytitle_pos[0])
  else
    ytitle_len = []
    ytitle_pos = []
    @ytitle = char2Chtype("<C></5>Y Axis", ytitle_len, ytitle_pos)
    @ytitle_len = ytitle_len[0]
    @ytitle_pos = justify_string(@box_width, @ytitle_len, ytitle_pos[0])
  end

  @graph_char = 0
  @values = []

  cdkscreen.register(:GRAPH, self)
end

Instance Method Details

#activate(actions) ⇒ Object

This was added for the builder.



84
85
86
# File 'lib/cdk/components/graph.rb', line 84

def activate(actions)
  self.draw(@box)
end

#destroyObject



365
366
367
368
369
370
# File 'lib/cdk/components/graph.rb', line 365

def destroy
  self.cleanTitle
  self.cleanBindings(:GRAPH)
  CDK::SCREEN.unregister(:GRAPH, self)
  CDK.deleteCursesWindow(@win)
end

#draw(box) ⇒ Object

Draw the grpah widget



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/cdk/components/graph.rb', line 275

def draw(box)
  adj = 2 + (if @xtitle.nil? || @xtitle.size == 0 then 0 else 1 end)
  spacing = 0
  attrib = ' '.ord | Ncurses::A_REVERSE

  if box
    Draw.drawObjBox(@win, self)
  end

  # Draw in the vertical axis
  Draw.drawLine(@win, 2, @title_lines + 1, 2, @box_height - 3,
      Ncurses::ACS_VLINE)

  # Draw in the horizontal axis
  Draw.drawLine(@win, 3, @box_height - 3, @box_width, @box_height - 3,
      Ncurses::ACS_HLINE)

  self.drawTitle(@win)

  # Draw in the X axis title.
  if !(@xtitle.nil?) && @xtitle.size > 0
    Draw.writeChtype(@win, 0, @xtitle_pos, @xtitle, CDK::VERTICAL,
        0, @xtitle_len)
    attrib = @xtitle[0] & Ncurses::A_ATTRIBUTES
  end

  # Draw in the X axis high value
  temp = "%d" % [@maxx]
  Draw.writeCharAttrib(@win, 1, @title_lines + 1, temp, attrib,
      CDK::VERTICAL, 0, temp.size)

  # Draw in the X axis low value.
  temp = "%d" % [@minx]
  Draw.writeCharAttrib(@win, 1, @box_height - 2 - temp.size, temp, attrib,
      CDK::VERTICAL, 0, temp.size)

  # Draw in the Y axis title
  if !(@ytitle.nil?) && @ytitle.size > 0
    Draw.writeChtype(@win, @ytitle_pos, @box_height - 1, @ytitle,
        CDK::HORIZONTAL, 0, @ytitle_len)
  end

  # Draw in the Y axis high value.
  temp = "%d" % [@count]
  Draw.writeCharAttrib(@win, @box_width - temp.size - adj,
      @box_height - 2, temp, attrib, CDK::HORIZONTAL, 0, temp.size)

  # Draw in the Y axis low value.
  Draw.writeCharAttrib(@win, 3, @box_height - 2, "0", attrib,
      CDK::HORIZONTAL, 0, "0".size)

  # If the count is zero then there aren't any points.
  if @count == 0
    wrefresh(@win)
    return
  end

  spacing = (@box_width - 3) / @count  # FIXME magic number (TITLE_LM)

  # Draw in the graph line/plot points.
  (0...@count).each do |y|
    colheight = (@values[y] / @xscale) - 1
    # Add the marker on the Y axis.
    @win.mvwaddch(@box_height - 3, (y + 1) * spacing + adj,
        Ncurses::ACS_TTEE)

    # If this is a plot graph, all we do is draw a dot.
    if @display_type == :PLOT
      xpos = @box_height - 4 - colheight
      ypos = (y + 1) * spacing + adj
      @win.mvwaddch(xpos, ypos, @graph_char[y])
    else
      (0..@yscale).each do |x|
        xpos = @box_height - 3
        ypos = (y + 1) * spacing - adj
        Draw.drawLine(@win, ypos, xpos - colheight, ypos, xpos,
            @graph_char[y])
      end
    end
  end

  # Draw in the axis corners.
  @win.mvwaddch(@title_lines, 2, Ncurses::ACS_URCORNER)
  @win.mvwaddch(@box_height - 3, 2, Ncurses::ACS_LLCORNER)
  @win.mvwaddch(@box_height - 3, @box_width, Ncurses::ACS_URCORNER)

  # Refresh and lets see it
  wrefresh
end

#eraseObject



372
373
374
375
376
# File 'lib/cdk/components/graph.rb', line 372

def erase
  if self.validCDKObject
    CDK.eraseCursesWindow(@win)
  end
end

#getCharacter(index) ⇒ Object



218
219
220
# File 'lib/cdk/components/graph.rb', line 218

def getCharacter(index)
  return graph_char[index]
end

#getCharactersObject



192
193
194
# File 'lib/cdk/components/graph.rb', line 192

def getCharacters
  return @graph_char
end

#getDisplayTypeObject



227
228
229
# File 'lib/cdk/components/graph.rb', line 227

def getDisplayType
  @display_type
end

#getValue(index) ⇒ Object



175
176
177
# File 'lib/cdk/components/graph.rb', line 175

def getValue(index)
  if index >= 0 && index < @count then @values[index] else 0 end
end

#getValues(size) ⇒ Object



148
149
150
151
# File 'lib/cdk/components/graph.rb', line 148

def getValues(size)
  size << @count
  return @values
end

#move(xplace, yplace, relative, refresh_flag) ⇒ Object

Move the graph field to the given location.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/cdk/components/graph.rb', line 237

def move(xplace, yplace, relative, refresh_flag)
  current_x = @win.getbegx
  current_y = @win.getbegy
  xpos = xplace
  ypos = yplace

  # If this is a relative move, then we will adjust where we want
  # to move to
  if relative
    xpos = @win.getbegx + xplace
    ypos = @win.getbegy + yplace
  end

  # Adjust the window if we need to.
  xtmp = [xpos]
  tymp = [ypos]
  CDK.alignxy(@screen.window, xtmp, ytmp, @box_width, @box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Get the difference
  xdiff = current_x - xpos
  ydiff = current_y - ypos

  # Move the window to the new location.
  CDK.moveCursesWindow(@win, -xdiff, -ydiff)
  CDK.moveCursesWindow(@shadow_win, -xdiff, -ydiff)

  # Touch the windows so they 'move'.
  CDK::SCREEN.refreshCDKWindow(@screen.window)

  # Reraw the windowk if they asked for it
  if refresh_flag
    self.draw(@box)
  end
end

#object_typeObject



378
379
380
# File 'lib/cdk/components/graph.rb', line 378

def object_type
  :GRAPH
end

#positionObject



382
383
384
# File 'lib/cdk/components/graph.rb', line 382

def position
  super(@win)
end

#set(values, count, graph_char, start_at_zero, display_type) ⇒ Object

Set multiple attributes of the widget



89
90
91
92
93
94
# File 'lib/cdk/components/graph.rb', line 89

def set(values, count, graph_char, start_at_zero, display_type)
  ret = self.setValues(values, count, start_at_zero)
  self.setCharacters(graph_char)
  self.setDisplayType(display_type)
  return ret
end

#setBKattr(attrib) ⇒ Object

Set the background attribute of the widget.



232
233
234
# File 'lib/cdk/components/graph.rb', line 232

def setBKattr(attrib)
  @win.wbkgd(attrib)
end

#setCharacter(index, character) ⇒ Object

Set the character of the graph widget of the given index.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cdk/components/graph.rb', line 197

def setCharacter(index, character)
  # Make sure the index is within range
  if index < 0 || index > @count
    return false
  end

  # Convert the string given to us
  char_count = []
  new_tokens = char2Chtype(character, char_count, [])

  # Check if the number of characters back is the same as the number
  # of elements in the list.
  if char_count[0] != @count
    return false
  end

  # Everything OK so far. Set the value of the array.
  @graph_char[index] = new_tokens[0]
  return true
end

#setCharacters(characters) ⇒ Object

Set the characters of the graph widget.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cdk/components/graph.rb', line 180

def setCharacters(characters)
  char_count = []
  new_tokens = char2Chtype(characters, char_count, [])

  if char_count[0] != @count
    return false
  end

  @graph_char = new_tokens
  return true
end

#setDisplayType(type) ⇒ Object

Set the display type of the graph.



223
224
225
# File 'lib/cdk/components/graph.rb', line 223

def setDisplayType(type)
  @display_type = type
end

#setScalesObject

Set the scale factors for the graph after wee have loaded new values.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cdk/components/graph.rb', line 97

def setScales
  @xscale = (@maxx - @minx) / [1, @box_height - @title_lines - 5].max
  if @xscale <= 0
    @xscale = 1
  end

  @yscale = (@box_width - 4) / [1, @count].max
  if @yscale <= 0
    @yscale = 1
  end
end

#setValue(index, value, start_at_zero) ⇒ Object

Set the value of the graph at the given index.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cdk/components/graph.rb', line 154

def setValue(index, value, start_at_zero)
  # Make sure the index is within range.
  if index < 0 || index >= @count
    return false
  end

  # Set the min, max, and value for the graph
  @minx = [value, @minx].min
  @maxx = [value, @maxx].max
  @values[index] = value

  # Check the start at zero status
  if start_at_zero
    @minx = 0
  end

  self.setScales

  return true
end

#setValues(values, count, start_at_zero) ⇒ Object

Set the values of the graph.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cdk/components/graph.rb', line 110

def setValues(values, count, start_at_zero)
  min = 2**30
  max = -2**30

  # Make sure everything is happy.
  if count < 0
    return false
  end

  if !(@values.nil?) && @values.size > 0
    @values = []
    @count = 0
  end

  # Copy the X values
  values.each do |value|
    min = [value, @minx].min
    max = [value, @maxx].max

    # Copy the value.
    @values << value
  end

  # Keep the count and min/max values
  @count = count
  @minx = min
  @maxx = max

  # Check the start at zero status.
  if start_at_zero
    @minx = 0
  end

  self.setScales

  return true
end