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_held_down, #handle_key_press, #handle_key_up, #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, #pad, #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.



2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
# File 'lib/wads/widgets.rb', line 2634

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.



2629
2630
2631
# File 'lib/wads/widgets.rb', line 2629

def display_grid
  @display_grid
end

#display_linesObject

Returns the value of attribute display_lines.



2630
2631
2632
# File 'lib/wads/widgets.rb', line 2630

def display_lines
  @display_lines
end

#pointsObject

Returns the value of attribute points.



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

def points
  @points
end

#visibility_mapObject

Returns the value of attribute visibility_map.



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

def visibility_map
  @visibility_map
end

#visible_rangeObject

Returns the value of attribute visible_range.



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

def visible_range
  @visible_range
end

#zoom_levelObject

Returns the value of attribute zoom_level.



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

def zoom_level
  @zoom_level
end

Instance Method Details

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



2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
# File 'lib/wads/widgets.rb', line 2709

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



2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
# File 'lib/wads/widgets.rb', line 2727

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



2662
2663
2664
2665
2666
# File 'lib/wads/widgets.rb', line 2662

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

#define_range(range) ⇒ Object



2696
2697
2698
2699
# File 'lib/wads/widgets.rb', line 2696

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

#display_grid_linesObject



2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
# File 'lib/wads/widgets.rb', line 2792

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



2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
# File 'lib/wads/widgets.rb', line 2780

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



2839
2840
2841
2842
2843
2844
2845
# File 'lib/wads/widgets.rb', line 2839

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



2753
2754
2755
# File 'lib/wads/widgets.rb', line 2753

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

#draw_y(y) ⇒ Object



2757
2758
2759
# File 'lib/wads/widgets.rb', line 2757

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

#get_x_data_val(mouse_x) ⇒ Object



2825
2826
2827
2828
2829
2830
# File 'lib/wads/widgets.rb', line 2825

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



2832
2833
2834
2835
2836
2837
# File 'lib/wads/widgets.rb', line 2832

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



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

def increase_data_point_size
    @data_point_size = @data_point_size + 2
end

#is_on_screen(point) ⇒ Object



2705
2706
2707
# File 'lib/wads/widgets.rb', line 2705

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)


2701
2702
2703
# File 'lib/wads/widgets.rb', line 2701

def range_set?
    not @visible_range.nil?
end

#remove_data_set(data_set_name) ⇒ Object



2738
2739
2740
2741
# File 'lib/wads/widgets.rb', line 2738

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

#renderObject



2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
# File 'lib/wads/widgets.rb', line 2761

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



2684
2685
2686
# File 'lib/wads/widgets.rb', line 2684

def scroll_down
    visible_range.scroll_down
end

#scroll_leftObject



2692
2693
2694
# File 'lib/wads/widgets.rb', line 2692

def scroll_left
    visible_range.scroll_left
end

#scroll_rightObject



2688
2689
2690
# File 'lib/wads/widgets.rb', line 2688

def scroll_right
    visible_range.scroll_right
end

#scroll_upObject



2680
2681
2682
# File 'lib/wads/widgets.rb', line 2680

def scroll_up 
    visible_range.scroll_up
end

#toggle_visibility(data_set_name) ⇒ Object



2650
2651
2652
2653
2654
2655
2656
# File 'lib/wads/widgets.rb', line 2650

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



2743
2744
2745
2746
# File 'lib/wads/widgets.rb', line 2743

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



2748
2749
2750
2751
# File 'lib/wads/widgets.rb', line 2748

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



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

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

#zoom_outObject



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

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