Class: CDK::GRAPH

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

Instance Attribute Summary

Attributes inherited from CDKOBJS

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #accepts_focus, #binding_list, #border_size, #box, #exit_type, #has_focus, #is_visible, #screen, #screen_index

Instance Method Summary collapse

Methods inherited from CDKOBJS

#SCREEN_XPOS, #SCREEN_YPOS, #bind, #bindableObject, #checkBind, #cleanBindings, #cleanTitle, #drawTitle, #focus, #getBox, #getc, #getch, #inject, #isBind, #move_specific, #refreshData, #saveData, #setBXattr, #setBackgroundColor, #setBox, #setExitType, #setHZchar, #setLLchar, #setLRchar, #setPostProcess, #setPreProcess, #setTitle, #setULchar, #setURchar, #setVTchar, #unbind, #unfocus, #validCDKObject, #validObjType

Constructor Details

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

Returns a new instance of GRAPH.



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/cdk/dialog.rb', line 345

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]
  CDK.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 = CDK.char2Chtype(xtitle, xtitle_len, xtitle_pos)
    @xtitle_len = xtitle_len[0]
    @xtitle_pos = CDK.justifyString(@box_height,
        @xtitle_len, xtitle_pos[0])
  else
    xtitle_len = []
    xtitle_pos = []
    @xtitle = CDK.char2Chtype("<C></5>X Axis", xtitle_len, xtitle_pos)
    @xtitle_len = title_len[0]
    @xtitle_pos = CDK.justifyString(@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 = CDK.char2Chtype(ytitle, ytitle_len, ytitle_pos)
    @ytitle_len = ytitle_len[0]
    @ytitle_pos = CDK.justifyString(@box_width, @ytitle_len, ytitle_pos[0])
  else
    ytitle_len = []
    ytitle_pos = []
    @ytitle = CDK.char2Chtype("<C></5>Y Axis", ytitle_len, ytitle_pos)
    @ytitle_len = ytitle_len[0]
    @ytitle_pos = CDK.justifyString(@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.



425
426
427
# File 'lib/cdk/dialog.rb', line 425

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

#destroyObject



706
707
708
709
710
711
# File 'lib/cdk/dialog.rb', line 706

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

#draw(box) ⇒ Object

Draw the grpah widget



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/cdk/dialog.rb', line 616

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
    @win.wrefresh
    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
  @win.wrefresh
end

#eraseObject



713
714
715
716
717
# File 'lib/cdk/dialog.rb', line 713

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

#getCharacter(index) ⇒ Object



559
560
561
# File 'lib/cdk/dialog.rb', line 559

def getCharacter(index)
  return graph_char[index]
end

#getCharactersObject



533
534
535
# File 'lib/cdk/dialog.rb', line 533

def getCharacters
  return @graph_char
end

#getDisplayTypeObject



568
569
570
# File 'lib/cdk/dialog.rb', line 568

def getDisplayType
  @display_type
end

#getValue(index) ⇒ Object



516
517
518
# File 'lib/cdk/dialog.rb', line 516

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

#getValues(size) ⇒ Object



489
490
491
492
# File 'lib/cdk/dialog.rb', line 489

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

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

Move the graph field to the given location.



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/cdk/dialog.rb', line 578

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



719
720
721
# File 'lib/cdk/dialog.rb', line 719

def object_type
  :GRAPH
end

#positionObject



723
724
725
# File 'lib/cdk/dialog.rb', line 723

def position
  super(@win)
end

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

Set multiple attributes of the widget



430
431
432
433
434
435
# File 'lib/cdk/dialog.rb', line 430

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.



573
574
575
# File 'lib/cdk/dialog.rb', line 573

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

#setCharacter(index, character) ⇒ Object

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



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/cdk/dialog.rb', line 538

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 = CDK.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.



521
522
523
524
525
526
527
528
529
530
531
# File 'lib/cdk/dialog.rb', line 521

def setCharacters(characters)
  char_count = []
  new_tokens = CDK.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.



564
565
566
# File 'lib/cdk/dialog.rb', line 564

def setDisplayType(type)
  @display_type = type
end

#setScalesObject

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



438
439
440
441
442
443
444
445
446
447
448
# File 'lib/cdk/dialog.rb', line 438

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.



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/cdk/dialog.rb', line 495

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.



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/cdk/dialog.rb', line 451

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