#Graph no longer plotting `frequencies` as expected. Any ideas?

2 messages · Page 1 of 1 (latest)

thick ridge
#
func draw_frequency_graph(frequencies: Array, graph_width: int, graph_height: int, parent_node: Node, overall_range: Array):
    # Create a Line2D node
    var line_graph = Line2D.new()
    line_graph.width = 2
    line_graph.default_color = Color(1, 0, 0)  # Red color
    line_graph.antialiased = true

    var graph_position = Vector2(100, 650)
    line_graph.position = graph_position
    parent_node.add_child(line_graph)

    # Get the max frequency and use it to scale graph
    var max_frequency = get_max_frequency(frequencies)
    var x_scale = graph_width / frequencies.size()
    var y_scale = graph_height / max_frequency

    var points = PackedVector2Array()
    for i in range(frequencies.size()):
        var x = i * x_scale
        var y = graph_height - (frequencies[i] / max_frequency) * graph_height  # Scale frequency to graph height
        points.push_back(Vector2(x, y))

    line_graph.points = points

    # Draw value labels
    draw_value_labels(parent_node, overall_range, graph_position, graph_width, graph_height, max_frequency)
    
func get_max_frequency(frequencies: Array) -> int:
    var max_freq = frequencies[0]
    for frequency in frequencies:
        if frequency > max_freq:
            max_freq = frequency
    return max_freq
    
func draw_value_labels(parent_node: Node, range_values: Array, graph_pos: Vector2, graph_width: int, graph_height: int, max_frequency: int):

    var interval = max(1, max_frequency / 10)

    for i in range(0, max_frequency + 1, interval):
        var label = Label.new()
        label.text = str(i)
        var y_position = graph_height - (i / float(max_frequency)) * graph_height
        label.position = Vector2(graph_pos.x - 30, graph_pos.y + y_position - label.get_rect().size.y / 2)
        parent_node.add_child(label)

    var vertical_axis_label = Label.new()
    vertical_axis_label.text = "Number of Rolls"
    vertical_axis_label.rotation_degrees = -90
    vertical_axis_label.position = Vector2(graph_pos.x - 70, graph_pos.y + graph_height / 1.5)
    parent_node.add_child(vertical_axis_label)

    var horizontal_axis_label = Label.new()
    horizontal_axis_label.text = "Range of Values"
    horizontal_axis_label.position = Vector2(graph_pos.x + graph_width / 2.25, graph_pos.y + graph_height + 30)
    parent_node.add_child(horizontal_axis_label)

    var range = range_values[1] - range_values[0]
    var num_labels = 5
    var step = range / (num_labels - 1)
    for i in range(num_labels):
        var val = range_values[0] + step * i
        var label = Label.new()
        label.text = str(int(val))
        var x_position = ((val - range_values[0]) / float(range)) * graph_width
        label.position = Vector2(graph_pos.x + x_position, graph_pos.y + graph_height + 10)
        parent_node.add_child(label)
#

Output should contain one value per roll inside of frequencies corresponding to the value of the roll. Here's an example set of data and output graph without proper data representation.