Skip to content

Commit

Permalink
+ LineChart, (up) chart
Browse files Browse the repository at this point in the history
  • Loading branch information
fenix-hub committed Jan 14, 2023
1 parent f7864e4 commit bc4c066
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 108 deletions.
20 changes: 20 additions & 0 deletions addons/easy_charts/control_charts/LineChart/line_chart.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extends ScatterChart
class_name LineChart

func _draw_line(from: Point, to: Point, function_index: int) -> void:
draw_line(
from.position,
to.position,
chart_properties.get_function_color(function_index),
chart_properties.line_width,
true
)

func _draw_lines() -> void:
for function in function_points.size():
for i in range(1, function_points[function].size()):
_draw_line(function_points[function][i], function_points[function][i - 1], function)

func _draw() -> void:
if chart_properties.lines:
_draw_lines()
7 changes: 7 additions & 0 deletions addons/easy_charts/control_charts/LineChart/line_chart.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://addons/easy_charts/control_charts/LineChart/line_chart.gd" type="Script" id=1]
[ext_resource path="res://addons/easy_charts/control_charts/chart.tscn" type="PackedScene" id=2]

[node name="LineChart" instance=ExtResource( 2 )]
script = ExtResource( 1 )
62 changes: 33 additions & 29 deletions addons/easy_charts/control_charts/ScatterChart/scatter_chart.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,37 @@ class_name ScatterChart

signal point_entered(point)

var focused_point: Point = null

var points: Array = []
var _point_box_rad: int = 10


# Called when the node enters the scene tree for the first time.
func _ready():
pass

func plot(x: Array, y: Array, properties: ChartProperties = self.chart_properties) -> void:
self.x = x
self.y = y

if properties != null:
self.chart_properties = properties

update()
var points: Array = []
var function_points: Array = []
var focused_point: Point = null

func _clear_points() -> void:
points = []
points.clear()
function_points.clear()

func _clear() -> void:
_clear_points()

func _get_point_box(point: Point, rad: int) -> Rect2:
return Rect2(point.position - (Vector2.ONE * rad), (Vector2.ONE * rad * 2))

func _move_tooltip(position: Vector2) -> void:
$Label.set_position(position + (Vector2.ONE * 15))
$Tooltip.set_position(position + (Vector2.ONE * 15))

func _show_tooltip(position: Vector2, text: String) -> void:
_move_tooltip(position)
$Label.show()
$Label.set_text(text)
$Label.set_size(Vector2.ZERO)
$Tooltip.show()
$Tooltip.set_text(text)
$Tooltip.set_size(Vector2.ZERO)

func _hide_tooltip() -> void:
$Label.hide()
$Label.set_text("")
$Label.set_size(Vector2.ZERO)
$Tooltip.hide()
$Tooltip.set_text("")
$Tooltip.set_size(Vector2.ZERO)

func _input(event: InputEvent):
func _input(event: InputEvent) -> void:
if event is InputEventMouse:
for point in points:
if _get_point_box(point, _point_box_rad).abs().has_point(event.position):
Expand All @@ -59,8 +50,6 @@ func _input(event: InputEvent):
_hide_tooltip()

func _draw_point(point: Point, function_index: int) -> void:
points.append(point)

match chart_properties.get_point_shape(function_index):
Point.Shape.CIRCLE:
draw_circle(point.position, chart_properties.point_radius, chart_properties.get_function_color(function_index))
Expand Down Expand Up @@ -94,21 +83,36 @@ func _draw_point(point: Point, function_index: int) -> void:
# )

func _draw_points() -> void:
for function in function_points.size():
for point in function_points[function]:
_draw_point(point, function)

func _calculate_points() -> void:
var validation: int = _validate_sampled_axis(x_sampled, y_sampled)
if not validation == OK:
printerr("Cannot plot points for invalid dataset! Error: %s" % validation)
return

if y_sampled.values[0] is Array:
for yxi in y_sampled.values.size():
var _function_points: Array = []
for i in y_sampled.values[yxi].size():
var real_point_val: Pair = Pair.new(x[i], y[yxi][i])
var sampled_point_pos: Vector2 = Vector2(x_sampled.values[i], y_sampled.values[yxi][i])
var point: Point = Point.new(sampled_point_pos, real_point_val)
_draw_point(point, yxi)
_function_points.append(point)
points.append(point)
function_points.append(_function_points)
else:
for i in y_sampled.values.size():
var real_point_val: Pair = Pair.new(x[i], y[i])
var sampled_point_pos: Vector2 = Vector2(x_sampled.values[i], y_sampled.values[i])
var point: Point = Point.news(sampled_point_pos, real_point_val)
_draw_point(point, i)
points.append(point)
function_points.append(points)

func _draw() -> void:
_calculate_points()

if chart_properties.points:
_draw_points()
22 changes: 2 additions & 20 deletions addons/easy_charts/control_charts/ScatterChart/scatter_chart.tscn
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://addons/easy_charts/control_charts/ScatterChart/scatter_chart.gd" type="Script" id=1]
[ext_resource path="res://addons/easy_charts/control_charts/chart.tscn" type="PackedScene" id=2]

[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 1, 1, 1, 1 )

[node name="ScatterChart" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
[node name="ScatterChart" instance=ExtResource( 2 )]
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": true
}

[node name="Canvas" type="Control" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0

[node name="Label" type="Label" parent="."]
visible = false
margin_right = 40.0
margin_bottom = 14.0
custom_colors/font_color = Color( 0, 0, 0, 1 )
custom_styles/normal = SubResource( 1 )
Loading

0 comments on commit bc4c066

Please sign in to comment.