Class: Wads::Plot

Inherits:
Widget show all
Defined in:
lib/wads/widgets.rb

Overview

A two-dimensional graph display which plots a number of PlotPoint objects. Options include grid lines that can be displayed, as well as whether lines should be drawn connecting each point in a data set.

Instance Attribute Summary collapse

Attributes inherited from Widget

#base_z, #children, #gui_theme, #height, #is_selected, #layout, #overlay_widget, #override_color, #text_input_fields, #visible, #width, #x, #y

Instance Method Summary collapse

Methods inherited from Widget

#add, #add_axis_lines, #add_button, #add_child, #add_delete_button, #add_document, #add_graph_display, #add_image, #add_multi_select_table, #add_overlay, #add_panel, #add_plot, #add_single_select_table, #add_table, #add_text, #border_color, #bottom_edge, #button_down, #button_up, #center_children, #center_x, #center_y, #clear_children, #contains_click, #debug, #disable_background, #disable_border, #draw, #draw_background, #draw_border, #enable_background, #enable_border, #error, #get_layout, #get_theme, #graphics_color, #handle_key_press, #handle_mouse_down, #handle_mouse_up, #handle_right_mouse, #handle_update, #info, #intercept_widget_event, #left_edge, #move_recursive_absolute, #move_recursive_delta, #overlaps_with, #relative_x, #relative_y, #relative_z_order, #remove_child, #remove_children, #remove_children_by_type, #right_edge, #selection_color, #set_absolute_position, #set_dimensions, #set_layout, #set_selected, #set_theme, #text_color, #top_edge, #unset_selected, #update, #uses_layout, #warn, #widget_z, #x_pixel_to_screen, #y_pixel_to_screen, #z_order

Constructor Details

#initialize(x, y, width, height) ⇒ Plot

Returns a new instance of Plot.



2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
# File 'lib/wads/widgets.rb', line 2603

def initialize(x, y, width, height) 
    super(x, y)
    set_dimensions(width, height)
    @display_grid = false
    @display_lines = true
    @grid_line_color = COLOR_LIGHT_GRAY
    @cursor_line_color = COLOR_DARK_GRAY 
    @zero_line_color = COLOR_HEADER_BRIGHT_BLUE 
    @zoom_level = 1
    @data_point_size = 4
    # Hash of rendered points keyed by data set name, so we can toggle visibility
    @points_by_data_set_name = {}
    @visibility_map = {}
    disable_border
end

Instance Attribute Details

#display_gridObject

Returns the value of attribute display_grid.



2598
2599
2600
# File 'lib/wads/widgets.rb', line 2598

def display_grid
  @display_grid
end

#display_linesObject

Returns the value of attribute display_lines.



2599
2600
2601
# File 'lib/wads/widgets.rb', line 2599

def display_lines
  @display_lines
end

#pointsObject

Returns the value of attribute points.



2596
2597
2598
# File 'lib/wads/widgets.rb', line 2596

def points
  @points
end

#visibility_mapObject

Returns the value of attribute visibility_map.



2601
2602
2603
# File 'lib/wads/widgets.rb', line 2601

def visibility_map
  @visibility_map
end

#visible_rangeObject

Returns the value of attribute visible_range.



2597
2598
2599
# File 'lib/wads/widgets.rb', line 2597

def visible_range
  @visible_range
end

#zoom_levelObject

Returns the value of attribute zoom_level.



2600
2601
2602
# File 'lib/wads/widgets.rb', line 2600

def zoom_level
  @zoom_level
end

Instance Method Details

#add_data_point(data_set_name, data_x, data_y, color = COLOR_MAROON) ⇒ Object



2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
# File 'lib/wads/widgets.rb', line 2678

def add_data_point(data_set_name, data_x, data_y, color = COLOR_MAROON) 
    if range_set?
        rendered_points = @points_by_data_set_name[data_set_name]
        if rendered_points.nil?
            rendered_points = []
            @points_by_data_set_name[data_set_name] = rendered_points
        end 
        rendered_points << PlotPoint.new(draw_x(data_x), draw_y(data_y),
                                         data_x, data_y,
                                         color)
        if @visibility_map[data_set_name].nil?
            @visibility_map[data_set_name] = true
        end
    else
        error("ERROR: range not set, cannot add data")
    end
end

#add_data_set(data_set_name, rendered_points) ⇒ Object



2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
# File 'lib/wads/widgets.rb', line 2696

def add_data_set(data_set_name, rendered_points)
    if range_set?
        @points_by_data_set_name[data_set_name] = rendered_points
        if @visibility_map[data_set_name].nil?
            @visibility_map[data_set_name] = true
        end
    else
        error("ERROR: range not set, cannot add data")
    end
end

#decrease_data_point_sizeObject



2631
2632
2633
2634
2635
# File 'lib/wads/widgets.rb', line 2631

def decrease_data_point_size 
    if @data_point_size > 2
        @data_point_size = @data_point_size - 2
    end
end

#define_range(range) ⇒ Object



2665
2666
2667
2668
# File 'lib/wads/widgets.rb', line 2665

def define_range(range)
    @visible_range = range
    @zoom_level = 1
end

#display_grid_linesObject



2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
# File 'lib/wads/widgets.rb', line 2761

def display_grid_lines
    grid_widgets = []

    x_lines = @visible_range.grid_line_x_values
    y_lines = @visible_range.grid_line_y_values
    first_x = draw_x(@visible_range.left_x)
    last_x = draw_x(@visible_range.right_x)
    first_y = draw_y(@visible_range.bottom_y)
    last_y = draw_y(@visible_range.top_y)

    x_lines.each do |grid_x|
        dx = draw_x(grid_x)
        color = @grid_line_color
        if grid_x == 0 and grid_x != @visible_range.left_x.to_i
            color = @zero_line_color 
        end    
        grid_widgets << Line.new(dx, first_y, dx, last_y, color) 
    end

    y_lines.each do |grid_y| 
        dy = draw_y(grid_y)
        color = @grid_line_color
        if grid_y == 0 and grid_y != @visible_range.bottom_y.to_i
            color = @zero_line_color
        end
        grid_widgets << Line.new(first_x, dy, last_x, dy, color) 
    end 

    grid_widgets.each do |gw| 
        gw.draw 
    end
end

#display_lines_for_point_set(points) ⇒ Object



2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
# File 'lib/wads/widgets.rb', line 2749

def display_lines_for_point_set(points) 
    if points.length > 1
        points.inject(points[0]) do |last, the_next|
            if last.x < the_next.x
                Gosu::draw_line last.x, last.y, last.graphics_color,
                                the_next.x, the_next.y, last.graphics_color, relative_z_order(Z_ORDER_GRAPHIC_ELEMENTS)
            end
            the_next
        end
    end
end

#draw_cursor_lines(mouse_x, mouse_y) ⇒ Object



2808
2809
2810
2811
2812
2813
2814
# File 'lib/wads/widgets.rb', line 2808

def draw_cursor_lines(mouse_x, mouse_y)
    Gosu::draw_line mouse_x, y_pixel_to_screen(0), @cursor_line_color, mouse_x, y_pixel_to_screen(@height), @cursor_line_color, Z_ORDER_GRAPHIC_ELEMENTS
    Gosu::draw_line x_pixel_to_screen(0), mouse_y, @cursor_line_color, x_pixel_to_screen(@width), mouse_y, @cursor_line_color, Z_ORDER_GRAPHIC_ELEMENTS
    
    # Return the data values at this point, so the plotter can display them
    [get_x_data_val(mouse_x), get_y_data_val(mouse_y)]
end

#draw_x(x) ⇒ Object



2722
2723
2724
# File 'lib/wads/widgets.rb', line 2722

def draw_x(x)
    x_pixel_to_screen(x_val_to_pixel(x)) 
end

#draw_y(y) ⇒ Object



2726
2727
2728
# File 'lib/wads/widgets.rb', line 2726

def draw_y(y)
    y_pixel_to_screen(y_val_to_pixel(y)) 
end

#get_x_data_val(mouse_x) ⇒ Object



2794
2795
2796
2797
2798
2799
# File 'lib/wads/widgets.rb', line 2794

def get_x_data_val(mouse_x)
    graph_x = mouse_x - @x
    x_pct = (@width - graph_x).to_f / @width.to_f
    x_val = @visible_range.right_x - (x_pct * @visible_range.x_range)
    x_val
end

#get_y_data_val(mouse_y) ⇒ Object



2801
2802
2803
2804
2805
2806
# File 'lib/wads/widgets.rb', line 2801

def get_y_data_val(mouse_y)
    graph_y = mouse_y - @y
    y_pct = graph_y.to_f / @height.to_f
    y_val = @visible_range.top_y - (y_pct * @visible_range.y_range)
    y_val
end

#increase_data_point_sizeObject



2627
2628
2629
# File 'lib/wads/widgets.rb', line 2627

def increase_data_point_size
    @data_point_size = @data_point_size + 2
end

#is_on_screen(point) ⇒ Object



2674
2675
2676
# File 'lib/wads/widgets.rb', line 2674

def is_on_screen(point) 
    point.data_x >= @visible_range.left_x and point.data_x <= @visible_range.right_x and point.data_y >= @visible_range.bottom_y and point.data_y <= @visible_range.top_y
end

#range_set?Boolean

Returns:

  • (Boolean)


2670
2671
2672
# File 'lib/wads/widgets.rb', line 2670

def range_set?
    not @visible_range.nil?
end

#remove_data_set(data_set_name) ⇒ Object



2707
2708
2709
2710
# File 'lib/wads/widgets.rb', line 2707

def remove_data_set(data_set_name)
    @points_by_data_set_name.delete(data_set_name)
    @visibility_map.delete(data_set_name)
end

#renderObject



2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
# File 'lib/wads/widgets.rb', line 2730

def render
    @points_by_data_set_name.keys.each do |key|
        if @visibility_map[key]
            data_set_points = @points_by_data_set_name[key]
            data_set_points.each do |point| 
                if is_on_screen(point)
                    point.render(@data_point_size)
                end
            end 
            if @display_lines 
                display_lines_for_point_set(data_set_points) 
            end
        end
        if @display_grid and range_set?
            display_grid_lines
        end
    end
end

#scroll_downObject



2653
2654
2655
# File 'lib/wads/widgets.rb', line 2653

def scroll_down
    visible_range.scroll_down
end

#scroll_leftObject



2661
2662
2663
# File 'lib/wads/widgets.rb', line 2661

def scroll_left
    visible_range.scroll_left
end

#scroll_rightObject



2657
2658
2659
# File 'lib/wads/widgets.rb', line 2657

def scroll_right
    visible_range.scroll_right
end

#scroll_upObject



2649
2650
2651
# File 'lib/wads/widgets.rb', line 2649

def scroll_up 
    visible_range.scroll_up
end

#toggle_visibility(data_set_name) ⇒ Object



2619
2620
2621
2622
2623
2624
2625
# File 'lib/wads/widgets.rb', line 2619

def toggle_visibility(data_set_name)
    is_visible = @visibility_map[data_set_name]
    if is_visible.nil?
        return
    end
    @visibility_map[data_set_name] = !is_visible
end

#x_val_to_pixel(val) ⇒ Object



2712
2713
2714
2715
# File 'lib/wads/widgets.rb', line 2712

def x_val_to_pixel(val)
    x_pct = (@visible_range.right_x - val).to_f / @visible_range.x_range 
    @width - (@width.to_f * x_pct).round
end

#y_val_to_pixel(val) ⇒ Object



2717
2718
2719
2720
# File 'lib/wads/widgets.rb', line 2717

def y_val_to_pixel(val)
    y_pct = (@visible_range.top_y - val).to_f / @visible_range.y_range 
    (@height.to_f * y_pct).round
end

#zoom_inObject



2642
2643
2644
2645
2646
2647
# File 'lib/wads/widgets.rb', line 2642

def zoom_in
    if @zoom_level > 0.11
        @zoom_level = @zoom_level - 0.15
    end
    visible_range.scale(@zoom_level)
end

#zoom_outObject



2637
2638
2639
2640
# File 'lib/wads/widgets.rb', line 2637

def zoom_out 
    @zoom_level = @zoom_level + 0.15
    visible_range.scale(@zoom_level)
end