Skip to content
This repository was archived by the owner on Jan 17, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

This repository contains the add-on and examples.

## Fork

This fork adds a custom node named FloatingLegend that displays the legend of a Graph2D. It is used when you don't want to show the legend inside the plot area, but display it somewhere else. A boolean parameter is added to Graph2D to not display the legend.

## Features

- Display several plots on the same graph.
Expand Down
1 change: 1 addition & 0 deletions addons/graph_2d/custom_nodes/legend.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@tool
extends Control
class_name Legend

var layout := VBoxContainer.new()

Expand Down
1 change: 1 addition & 0 deletions addons/graph_2d/custom_refcounted/plot_item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func _init(obj, l, c, w):
label = l
_curve.name = l
_curve.color = c
color = c
_curve.width = w
_graph.get_node("PlotArea").add_child(_curve)

Expand Down
45 changes: 45 additions & 0 deletions addons/graph_2d/floating_legend.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@tool
extends Control
class_name FloatingLegend
## Display the legend of a [Graph2D]
##
## This node will display the list of [PoltItem] names that a [Graph2D]
## has. It is used as alternative to the legend displayed inside the
## plot area. You can hide the legend of the plot area by setting the
## show_legend parameter of the [Graph2D].
##
## tutorial: https://www.reddit.com/r/godot/comments/1jkc0al/graph2d_plugin_floating_legend_branch/

## The [Graph2D] of which's legend will be displayed.
@export var graph : Graph2D

const _Graph2DLegend = preload("res://addons/graph_2d/custom_nodes/legend.gd")
var legend: Legend
var label_list : VBoxContainer
var backgrnound: ColorRect


func _ready() -> void:
legend = _Graph2DLegend.new()
add_child(legend)
label_list = legend.get_child(0) as VBoxContainer
label_list.resized.connect(_on_resized)
if graph :
graph.legend_updated.connect(_on_graph_legend_updated)
backgrnound = ColorRect.new()
backgrnound.layout_mode = 1
backgrnound.anchors_preset = Control.PRESET_FULL_RECT
backgrnound.color = graph.background_color
add_child(backgrnound)
move_child(backgrnound,0)
else :
printerr("No graph declared for the floating legend node")


func _on_graph_legend_updated(labels:Array):
legend.update(labels)


func _on_resized() -> void:
size = label_list.size + Vector2(20,40)

14 changes: 12 additions & 2 deletions addons/graph_2d/graph_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ extends Control
_update_graph()

@export_group("Background")
## Show legend on graph
@export var show_legend := true:
set(value):
show_legend = value
## Background color of graph
@export var background_color = Color.BLACK:
@export var background_color := Color.BLACK:
set(value):
background_color = value
if get_node_or_null("Background"):
Expand Down Expand Up @@ -136,6 +140,8 @@ var _plots: Array
var _x_step: float
var _y_step: float

signal legend_updated

#endregion

func _ready():
Expand Down Expand Up @@ -333,7 +339,11 @@ func _update_legend() -> void:
name = p.label,
color = p.color,
})
get_node("PlotArea/Legend").update(labels)
if show_legend:
get_node("PlotArea/Legend").update(labels)
else :
get_node("PlotArea/Legend").update([])
legend_updated.emit(labels)

func _on_Graph_resized() -> void:
_update_graph()
Expand Down
64 changes: 64 additions & 0 deletions addons/graph_2d/legend.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions addons/graph_2d/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ func _enter_tree() -> void:
# Initialization of the plugin goes here.
add_custom_type("Graph2D", "Control",
preload("res://addons/graph_2d/graph_2d.gd"),
preload("res://addons/graph_2d/graph_2d.svg"))

preload("res://addons/graph_2d/graph_2d.svg"),
)
add_custom_type("FloatingLegend", "Control",
preload("res://addons/graph_2d/floating_legend.gd"),
preload("res://addons/graph_2d/legend.svg"),
)

func _exit_tree() -> void:
# Clean-up of the plugin goes here.
remove_custom_type("Graph2D")
remove_custom_type("FloatingLegend")
56 changes: 56 additions & 0 deletions examples/graph_with_legend.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[gd_scene load_steps=4 format=3 uid="uid://desy7txx62pb2"]

[ext_resource type="Script" path="res://examples/single_plot.gd" id="1_62wlx"]
[ext_resource type="Script" path="res://addons/graph_2d/graph_2d.gd" id="2_y0q6u"]
[ext_resource type="Script" path="res://addons/graph_2d/floating_legend.gd" id="3_7par3"]

[node name="SinglePlot" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_62wlx")

[node name="Graph2D" type="Control" parent="."]
anchors_preset = 0
offset_left = 282.0
offset_right = 1141.0
offset_bottom = 635.0
script = ExtResource("2_y0q6u")
x_label = "X"
y_min = -53.0
y_max = 50.0
show_legend = false
background_color = Color(0.280876, 0.373644, 0.701865, 1)
grid_horizontal_visible = true
grid_vertical_visible = true

[node name="AddPlot" type="Button" parent="."]
layout_mode = 0
offset_left = 97.0
offset_top = 37.0
offset_right = 210.0
offset_bottom = 68.0
text = "Add New Plot"

[node name="RemoveAllPlots" type="Button" parent="."]
layout_mode = 0
offset_left = 69.0
offset_top = 98.0
offset_right = 206.0
offset_bottom = 129.0
text = "Remove All Plots"

[node name="FloatingLegend" type="Control" parent="." node_paths=PackedStringArray("graph")]
anchors_preset = 0
offset_left = 52.4538
offset_top = 281.007
offset_right = 92.4538
offset_bottom = 321.007
script = ExtResource("3_7par3")
graph = NodePath("../Graph2D")

[connection signal="pressed" from="AddPlot" to="." method="_on_add_plot_pressed"]
[connection signal="pressed" from="RemoveAllPlots" to="." method="_on_remove_all_plots_pressed"]
1 change: 1 addition & 0 deletions examples/single_plot.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ x_label = "Time(s)"
y_min = -53.0
y_max = 50.0
y_label = "Y"
show_legend = null
background_color = Color(0.0941176, 0.227451, 0.4, 1)
grid_horizontal_visible = true
grid_vertical_visible = true
Expand Down