mirror of
https://github.com/Relintai/pandemonium_engine_easy_charts.git
synced 2024-11-14 10:17:24 +01:00
Rework of ScatterChart and LineChart (#40)
* Fixed ScatterChart to work like LineChart when are_values_columns is true * Fixed labeling spaguetti and added offset. * Fixed indentation to match GDScript style guide * Fix distances calculations Note that I removed the origin_at_zero conditional at calculate_coordinates because I already fix y_margin_min to 0 at structure_datas. * Add missing features to ScatterChart I added the missing features from LineChart to ScatterChart (like treshold and some properties) in preparation to creation of a Parent Class common to both. This way the changes can be traced easily. * Add BaseClass for Scatter and Line Charts * Changes to property_list getter * Rename ScatterChartBase.gd to scatter_chart_base.gd * Filenames changed to match GDscript Style Guide From Godot so it doesn't break the links. * Fixed some dependencies on chart and chart2D
This commit is contained in:
parent
29e0010b20
commit
d46e099717
@ -1,8 +1,9 @@
|
||||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/ColumnChart/column_chart.gd" type="Script" id=2]
|
||||
|
||||
|
||||
[sub_resource type="Theme" id=1]
|
||||
|
||||
[node name="ColumnChart" type="Control"]
|
@ -1,7 +1,9 @@
|
||||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/ColumnChart2D/ColumnChart2D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=2]
|
||||
[ext_resource path="res://addons/easy_charts/ColumnChart2D/column_chart2D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=2]
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
tool
|
||||
extends Chart
|
||||
extends ScatterChartBase
|
||||
class_name LineChart
|
||||
|
||||
# [Linechart] - General purpose node for Line Charts
|
||||
@ -16,335 +16,27 @@ class_name LineChart
|
||||
|
||||
|
||||
func _get_property_list():
|
||||
return [
|
||||
# Chart Properties
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_CATEGORY,
|
||||
"name": "LineChart",
|
||||
"type": TYPE_STRING
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/are_values_columns",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "-1,100,1",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/labels_index",
|
||||
"type": TYPE_INT
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/show_x_values_as_labels",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
property_list[0].name = "LineChart"
|
||||
return property_list
|
||||
|
||||
# Chart Display
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "0.1,10",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Display/x_decim",
|
||||
"type": TYPE_REAL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "0.001,1,0.001",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Display/y_decim",
|
||||
"type": TYPE_REAL
|
||||
},
|
||||
|
||||
# Chart Style
|
||||
{
|
||||
"hint": 24,
|
||||
"hint_string":
|
||||
(
|
||||
"%d/%d:%s"
|
||||
% [TYPE_INT, PROPERTY_HINT_ENUM, PoolStringArray(PointShapes.keys()).join(",")]
|
||||
),
|
||||
"name": "Chart_Style/points_shape",
|
||||
"type": TYPE_ARRAY,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/function_colors",
|
||||
"type": TYPE_COLOR_ARRAY
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/box_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/v_lines_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/h_lines_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"class_name": "Font",
|
||||
"hint": PROPERTY_HINT_RESOURCE_TYPE,
|
||||
"hint_string": "Font",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/font",
|
||||
"type": TYPE_OBJECT
|
||||
},
|
||||
{
|
||||
"class_name": "Font",
|
||||
"hint": PROPERTY_HINT_RESOURCE_TYPE,
|
||||
"hint_string": "Font",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/bold_font",
|
||||
"type": TYPE_OBJECT
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/font_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/use_template",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_ENUM,
|
||||
"hint_string": PoolStringArray(Utilities.templates.keys()).join(","),
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/template",
|
||||
"type": TYPE_INT
|
||||
},
|
||||
|
||||
# Chart Modifiers
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Modifiers/treshold",
|
||||
"type": TYPE_VECTOR2
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Modifiers/only_disp_values",
|
||||
"type": TYPE_VECTOR2
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Modifiers/invert_chart",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
func structure_datas(database: Array):
|
||||
# @labels_index can be either a column or a row relative to x values
|
||||
are_values_columns = (invert_chart != are_values_columns)
|
||||
if are_values_columns:
|
||||
for row in database.size():
|
||||
var t_vals: Array
|
||||
for column in database[row].size():
|
||||
if column == labels_index:
|
||||
var x_data = database[row][column]
|
||||
if typeof(x_data) == TYPE_INT or typeof(x_data) == TYPE_REAL:
|
||||
x_datas.append(x_data as float)
|
||||
else:
|
||||
x_datas.append(x_data)
|
||||
else:
|
||||
if row != 0:
|
||||
var y_data = database[row][column]
|
||||
if typeof(y_data) == TYPE_INT or typeof(y_data) == TYPE_REAL:
|
||||
t_vals.append(y_data as float)
|
||||
else:
|
||||
t_vals.append(y_data.replace(",", ".") as float)
|
||||
else:
|
||||
y_labels.append(str(database[row][column]))
|
||||
if not t_vals.empty():
|
||||
y_datas.append(t_vals)
|
||||
x_label = str(x_datas.pop_front())
|
||||
else:
|
||||
for row in database.size():
|
||||
if row == labels_index:
|
||||
x_datas = (database[row])
|
||||
x_label = x_datas.pop_front() as String
|
||||
else:
|
||||
var values = database[row] as Array
|
||||
y_labels.append(values.pop_front() as String)
|
||||
y_datas.append(values)
|
||||
for data in y_datas:
|
||||
for value in data.size():
|
||||
data[value] = data[value] as float
|
||||
|
||||
# draw y labels
|
||||
var to_order: Array
|
||||
var to_order_min: Array
|
||||
for cluster in y_datas.size():
|
||||
# define x_chors and y_chors
|
||||
var ordered_cluster = y_datas[cluster].duplicate() as Array
|
||||
ordered_cluster.sort()
|
||||
var margin_max = ordered_cluster[ordered_cluster.size() - 1]
|
||||
var margin_min = ordered_cluster[0]
|
||||
to_order.append(margin_max)
|
||||
to_order_min.append(margin_min)
|
||||
|
||||
to_order.sort()
|
||||
to_order_min.sort()
|
||||
var margin = to_order.pop_back()
|
||||
if not origin_at_zero:
|
||||
y_margin_min = to_order_min.pop_front()
|
||||
v_dist = y_decim * pow(10.0, str(margin).length()-1) #* pow(10.0, (str(margin).length() - 2 if typeof(margin) == TYPE_INT else str(margin).length() - str(y_decim).length() ))
|
||||
var multi = 0
|
||||
var p = (v_dist * multi) + ((y_margin_min) if not origin_at_zero else 0)
|
||||
y_chors.append(p as String)
|
||||
while p < margin:
|
||||
multi += 1
|
||||
p = (v_dist * multi) + ((y_margin_min) if not origin_at_zero else 0)
|
||||
y_chors.append(p as String)
|
||||
OFFSET.x = (str(margin).length()) * font_size
|
||||
OFFSET.y = (font_size * 2) - 4
|
||||
|
||||
func build_chart():
|
||||
SIZE = get_size() - Vector2(OFFSET.x,0)
|
||||
origin = Vector2(OFFSET.x, SIZE.y - OFFSET.y)
|
||||
|
||||
|
||||
func calculate_pass():
|
||||
x_chors = x_datas.duplicate(true) as PoolStringArray
|
||||
|
||||
|
||||
# calculate distance in pixel between 2 consecutive values/datas
|
||||
x_pass = (SIZE.x - OFFSET.x) / (x_chors.size()-1 if x_chors.size()>1 else x_chors.size() )
|
||||
y_pass = (origin.y - ChartName.get_rect().size.y*2) / (y_chors.size() - 1)
|
||||
|
||||
|
||||
func calculate_coordinates():
|
||||
x_coordinates.clear()
|
||||
y_coordinates.clear()
|
||||
point_values.clear()
|
||||
point_positions.clear()
|
||||
|
||||
for cluster in y_datas:
|
||||
var single_coordinates: Array
|
||||
for value in cluster.size():
|
||||
if origin_at_zero:
|
||||
single_coordinates.append((cluster[value] * y_pass) / v_dist)
|
||||
else:
|
||||
single_coordinates.append((cluster[value] - y_margin_min) * y_pass / v_dist)
|
||||
y_coordinates.append(single_coordinates)
|
||||
|
||||
for x in x_datas.size():
|
||||
x_coordinates.append(x_pass * x)
|
||||
|
||||
for f in range(0,functions):
|
||||
point_values.append([])
|
||||
point_positions.append([])
|
||||
|
||||
for cluster in y_coordinates.size():
|
||||
for y in y_coordinates[cluster].size():
|
||||
if are_values_columns:
|
||||
point_values[y].append([x_datas[cluster], y_datas[cluster][y]])
|
||||
point_positions[y].append(Vector2(
|
||||
x_coordinates[cluster] + origin.x, origin.y - y_coordinates[cluster][y]))
|
||||
else:
|
||||
point_values[cluster].append([x_datas[y], y_datas[cluster][y]])
|
||||
point_positions[cluster].append(Vector2(
|
||||
x_coordinates[y] + origin.x,
|
||||
origin.y - y_coordinates[cluster][y]))
|
||||
|
||||
func _draw():
|
||||
clear_points()
|
||||
draw_grid()
|
||||
draw_chart_outlines()
|
||||
draw_points()
|
||||
draw_lines()
|
||||
draw_treshold()
|
||||
|
||||
var defined_colors: bool = false
|
||||
if function_colors.size():
|
||||
defined_colors = true
|
||||
|
||||
if Points.get_child_count() > 0 :
|
||||
for point in Points.get_children():
|
||||
point.queue_free()
|
||||
|
||||
for _function in point_values.size():
|
||||
for function_point in point_values[_function].size():
|
||||
var point: Point = point_node.instance()
|
||||
point.connect("_point_pressed", self, "point_pressed")
|
||||
point.connect("_mouse_entered", self, "show_data")
|
||||
point.connect("_mouse_exited", self, "hide_data")
|
||||
point.create_point(points_shape[_function],function_colors[_function],Color.white,point_positions[_function][function_point],point.format_value(point_values[_function][function_point], false, false),y_labels[_function] as String)
|
||||
|
||||
Points.add_child(point)
|
||||
if function_point > 0:
|
||||
func draw_lines():
|
||||
var _function = 0
|
||||
for PointContainer in Points.get_children(): #Each function is stored in a different PointContainer
|
||||
for function_point in range(1, PointContainer.get_children().size()):
|
||||
draw_line(
|
||||
point_positions[_function][function_point - 1],
|
||||
point_positions[_function][function_point],
|
||||
function_colors[_function],
|
||||
2,
|
||||
false)
|
||||
draw_treshold()
|
||||
|
||||
func draw_grid():
|
||||
# ascisse
|
||||
for p in x_chors.size():
|
||||
var point: Vector2 = origin + Vector2((p) * x_pass, 0)
|
||||
# v grid
|
||||
draw_line(point, point - Vector2(0, SIZE.y - OFFSET.y*2), v_lines_color, 0.2, true)
|
||||
# ascisse
|
||||
draw_line(point - Vector2(0, 5), point, v_lines_color, 1, true)
|
||||
if show_x_values_as_labels : pass
|
||||
else: continue
|
||||
draw_string(
|
||||
font,
|
||||
point + Vector2(-const_width / 2 * x_chors[p].length(), font_size + const_height),
|
||||
x_chors[p],
|
||||
font_color)
|
||||
|
||||
if not show_x_values_as_labels :
|
||||
draw_string(
|
||||
font,
|
||||
Vector2((SIZE.x - origin.x)/2 + ((x_label.length() * font_size) / 2), SIZE.y - font_size/2),
|
||||
x_label,
|
||||
font_color)
|
||||
|
||||
# ordinate
|
||||
for p in y_chors.size():
|
||||
var point: Vector2 = origin - Vector2(0, (p) * y_pass)
|
||||
# h grid
|
||||
draw_line(point, point + Vector2(SIZE.x - OFFSET.x, 0), h_lines_color, 0.2, true)
|
||||
# ordinate
|
||||
draw_line(point, point + Vector2(5, 0), h_lines_color, 1, true)
|
||||
draw_string(
|
||||
font,
|
||||
point - Vector2(y_chors[p].length() * const_width + font_size, -const_height),
|
||||
y_chors[p],
|
||||
font_color)
|
||||
|
||||
func draw_chart_outlines():
|
||||
draw_line(origin, SIZE - Vector2(0, OFFSET.y), box_color, 1.2, true)
|
||||
draw_line(origin, Vector2(OFFSET.x, OFFSET.y), box_color, 1.2, true)
|
||||
draw_line(Vector2(OFFSET.x, OFFSET.y), Vector2(SIZE.x, OFFSET.y), box_color, 1.2, true)
|
||||
draw_line(Vector2(SIZE.x, OFFSET.y), SIZE - Vector2(0, OFFSET.y), box_color, 1.2, true)
|
||||
|
||||
func draw_treshold():
|
||||
if v_dist != 0:
|
||||
treshold_draw = Vector2((treshold.x * x_pass) + origin.x ,origin.y - ((treshold.y * y_pass)/v_dist))
|
||||
if treshold.y != 0:
|
||||
draw_line(Vector2(origin.x, treshold_draw.y), Vector2(SIZE.x, treshold_draw.y), Color.red, 0.4, true)
|
||||
if treshold.x != 0:
|
||||
draw_line(Vector2(treshold_draw.x, 0), Vector2(treshold_draw.x, SIZE.y - OFFSET.y), Color.red, 0.4, true)
|
||||
_function += 1
|
||||
|
@ -1,8 +1,9 @@
|
||||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/LineChart/line_chart.gd" type="Script" id=4]
|
||||
|
||||
|
||||
[sub_resource type="Theme" id=1]
|
||||
|
||||
[node name="LineChart" type="Control"]
|
@ -1,7 +1,10 @@
|
||||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/LineChart2D/LineChart2D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=2]
|
||||
[ext_resource path="res://addons/easy_charts/LineChart2D/line_chart2D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=2]
|
||||
|
||||
|
||||
|
||||
|
||||
[node name="LineChart2D" type="Node2D"]
|
||||
position = Vector2( -1, 0 )
|
@ -1,7 +1,8 @@
|
||||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/PieChart/pie_chart.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=2]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=2]
|
||||
|
||||
|
||||
[node name="PieChart" type="Control"]
|
||||
anchor_right = 1.0
|
@ -1,8 +1,9 @@
|
||||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/RadarChart/radar_chart.gd" type="Script" id=2]
|
||||
|
||||
|
||||
[sub_resource type="Theme" id=1]
|
||||
|
||||
[node name="RadarChart" type="Control"]
|
@ -1,5 +1,5 @@
|
||||
tool
|
||||
extends Chart
|
||||
extends ScatterChartBase
|
||||
class_name ScatterChart
|
||||
|
||||
"""
|
||||
@ -16,302 +16,15 @@ the horizontal axis and the value of the other variable determining the position
|
||||
|
||||
# ---------------------
|
||||
|
||||
|
||||
func _get_property_list():
|
||||
return [
|
||||
# Chart Properties
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/are_values_columns",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "-1,100,1",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/labels_index",
|
||||
"type": TYPE_INT
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/show_x_values_as_labels",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
property_list[0].name = "ScatterChart"
|
||||
return property_list
|
||||
|
||||
# Chart Display
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "0.1,10",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Display/x_decim",
|
||||
"type": TYPE_REAL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "0.1,10",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Display/y_decim",
|
||||
"type": TYPE_REAL
|
||||
},
|
||||
|
||||
# Chart Style
|
||||
{
|
||||
"hint": 24,
|
||||
"hint_string": "%d/%d:%s"%[TYPE_INT, PROPERTY_HINT_ENUM,
|
||||
PoolStringArray(PointShapes.keys()).join(",")],
|
||||
"name": "Chart_Style/points_shape",
|
||||
"type": TYPE_ARRAY,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/function_colors",
|
||||
"type": TYPE_COLOR_ARRAY
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/box_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/v_lines_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/h_lines_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"class_name": "Font",
|
||||
"hint": PROPERTY_HINT_RESOURCE_TYPE,
|
||||
"hint_string": "Font",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/font",
|
||||
"type": TYPE_OBJECT
|
||||
},
|
||||
{
|
||||
"class_name": "Font",
|
||||
"hint": PROPERTY_HINT_RESOURCE_TYPE,
|
||||
"hint_string": "Font",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/bold_font",
|
||||
"type": TYPE_OBJECT
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/font_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_ENUM,
|
||||
"hint_string": PoolStringArray(Utilities.templates.keys()).join(","),
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/template",
|
||||
"type": TYPE_INT
|
||||
},
|
||||
|
||||
# Chart Modifiers
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Modifiers/invert_chart",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
]
|
||||
|
||||
func structure_datas(database : Array):
|
||||
# @labels_index can be either a column or a row relative to x values
|
||||
# @y_values can be either a column or a row relative to y values
|
||||
are_values_columns = invert_chart != are_values_columns
|
||||
match are_values_columns:
|
||||
true:
|
||||
for row in database.size():
|
||||
var t_vals : Array
|
||||
for column in database[row].size():
|
||||
if column == labels_index:
|
||||
var x_data = database[row][column]
|
||||
if x_data.is_valid_float() or x_data.is_valid_integer():
|
||||
x_datas.append(x_data as float)
|
||||
else:
|
||||
x_datas.append(x_data.replace(",",".") as float)
|
||||
else:
|
||||
if row != 0:
|
||||
var y_data = database[row][column]
|
||||
if y_data.is_valid_float() or y_data.is_valid_integer():
|
||||
t_vals.append(y_data as float)
|
||||
else:
|
||||
t_vals.append(y_data.replace(",",".") as float)
|
||||
else:
|
||||
y_labels.append(str(database[row][column]))
|
||||
if not t_vals.empty():
|
||||
y_datas.append(t_vals)
|
||||
x_label = str(x_datas.pop_front())
|
||||
false:
|
||||
for row in database.size():
|
||||
if row == labels_index:
|
||||
x_datas = (database[row])
|
||||
x_label = x_datas.pop_front() as String
|
||||
else:
|
||||
var values = database[row] as Array
|
||||
y_labels.append(values.pop_front() as String)
|
||||
y_datas.append(values)
|
||||
for data in y_datas:
|
||||
for value in data.size():
|
||||
data[value] = data[value] as float
|
||||
|
||||
# draw y labels
|
||||
var to_order : Array
|
||||
var to_order_min : Array
|
||||
for cluster in y_datas.size():
|
||||
# define x_chors and y_chors
|
||||
var ordered_cluster = y_datas[cluster] as Array
|
||||
ordered_cluster.sort()
|
||||
ordered_cluster = PoolIntArray(ordered_cluster)
|
||||
var margin_max = ordered_cluster[ordered_cluster.size()-1]
|
||||
var margin_min = ordered_cluster[0]
|
||||
to_order.append(margin_max)
|
||||
to_order_min.append(margin_min)
|
||||
|
||||
to_order.sort()
|
||||
to_order_min.sort()
|
||||
var margin = to_order.pop_back()
|
||||
if not origin_at_zero:
|
||||
y_margin_min = to_order_min.pop_front()
|
||||
v_dist = y_decim * pow(10.0,str(margin).length()-2)
|
||||
var multi = 0
|
||||
var p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0)
|
||||
y_chors.append(p as String)
|
||||
while p < margin:
|
||||
multi+=1
|
||||
p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0)
|
||||
y_chors.append(p as String)
|
||||
|
||||
# draw x_labels
|
||||
if not show_x_values_as_labels:
|
||||
to_order.clear()
|
||||
to_order = x_datas as PoolIntArray
|
||||
|
||||
to_order.sort()
|
||||
margin = to_order.pop_back()
|
||||
if not origin_at_zero:
|
||||
x_margin_min = to_order.pop_front()
|
||||
h_dist = x_decim * pow(10.0,str(margin).length()-2)
|
||||
multi = 0
|
||||
p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0)
|
||||
x_labels.append(p as String)
|
||||
while p < margin:
|
||||
multi+=1
|
||||
p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0)
|
||||
x_labels.append(p as String)
|
||||
|
||||
func build_chart():
|
||||
SIZE = get_size()
|
||||
origin = Vector2(OFFSET.x,SIZE.y-OFFSET.y)
|
||||
|
||||
func calculate_pass():
|
||||
if show_x_values_as_labels:
|
||||
x_chors = x_datas as PoolStringArray
|
||||
else:
|
||||
x_chors = x_labels
|
||||
|
||||
# calculate distance in pixel between 2 consecutive values/datas
|
||||
x_pass = (SIZE.x - OFFSET.x) / (x_chors.size()-1)
|
||||
y_pass = origin.y / (y_chors.size()-1)
|
||||
|
||||
func calculate_coordinates():
|
||||
x_coordinates.clear()
|
||||
y_coordinates.clear()
|
||||
point_values.clear()
|
||||
point_positions.clear()
|
||||
|
||||
for cluster in y_datas:
|
||||
var single_coordinates : Array
|
||||
for value in cluster.size():
|
||||
if origin_at_zero:
|
||||
single_coordinates.append((cluster[value]*y_pass)/v_dist)
|
||||
else:
|
||||
single_coordinates.append((cluster[value] - y_margin_min)*y_pass/v_dist)
|
||||
y_coordinates.append(single_coordinates)
|
||||
|
||||
if show_x_values_as_labels:
|
||||
for x in x_datas.size():
|
||||
x_coordinates.append(x_pass*x)
|
||||
else:
|
||||
for x in x_datas.size():
|
||||
if origin_at_zero:
|
||||
x_coordinates.append(x_datas[x]*x_pass/h_dist)
|
||||
else:
|
||||
x_coordinates.append((x_datas[x] - x_margin_min)*x_pass/h_dist)
|
||||
|
||||
for f in functions:
|
||||
point_values.append([])
|
||||
point_positions.append([])
|
||||
|
||||
for cluster in y_coordinates.size():
|
||||
for y in y_coordinates[cluster].size():
|
||||
if are_values_columns:
|
||||
point_values[y].append([x_datas[cluster],y_datas[cluster][y]])
|
||||
point_positions[y].append(Vector2(x_coordinates[cluster]+origin.x,origin.y-y_coordinates[cluster][y]))
|
||||
else:
|
||||
point_values[cluster].append([x_datas[y],y_datas[cluster][y]])
|
||||
point_positions[cluster].append(Vector2(x_coordinates[y]+origin.x,origin.y-y_coordinates[cluster][y]))
|
||||
|
||||
func _draw():
|
||||
clear_points()
|
||||
|
||||
draw_grid()
|
||||
draw_chart_outlines()
|
||||
|
||||
var defined_colors : bool = false
|
||||
if function_colors.size():
|
||||
defined_colors = true
|
||||
|
||||
for _function in point_values.size():
|
||||
var PointContainer : Control = Control.new()
|
||||
Points.add_child(PointContainer)
|
||||
|
||||
for function_point in point_values[_function].size():
|
||||
var point : Point = point_node.instance()
|
||||
point.connect("_point_pressed",self,"point_pressed")
|
||||
point.connect("_mouse_entered",self,"show_data")
|
||||
point.connect("_mouse_exited",self,"hide_data")
|
||||
|
||||
point.create_point(points_shape[_function], function_colors[_function],
|
||||
Color.white, point_positions[_function][function_point],
|
||||
point.format_value(point_values[_function][function_point], false, false),
|
||||
y_labels[_function] as String)
|
||||
|
||||
PointContainer.add_child(point)
|
||||
|
||||
func draw_grid():
|
||||
# ascisse
|
||||
for p in x_chors.size():
|
||||
var point : Vector2 = origin+Vector2((p)*x_pass,0)
|
||||
# v grid
|
||||
draw_line(point,point-Vector2(0,SIZE.y-OFFSET.y),v_lines_color,0.2,true)
|
||||
# ascisse
|
||||
draw_line(point-Vector2(0,5),point,v_lines_color,1,true)
|
||||
draw_string(font,point+Vector2(-const_width/2*x_chors[p].length(),font_size+const_height),x_chors[p],font_color)
|
||||
|
||||
# ordinate
|
||||
for p in y_chors.size():
|
||||
var point : Vector2 = origin-Vector2(0,(p)*y_pass)
|
||||
# h grid
|
||||
draw_line(point,point+Vector2(SIZE.x-OFFSET.x,0),h_lines_color,0.2,true)
|
||||
# ordinate
|
||||
draw_line(point,point+Vector2(5,0),h_lines_color,1,true)
|
||||
draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,-const_height),y_chors[p],font_color)
|
||||
|
||||
func draw_chart_outlines():
|
||||
draw_line(origin,SIZE-Vector2(0,OFFSET.y),box_color,1,true)
|
||||
draw_line(origin,Vector2(OFFSET.x,0),box_color,1,true)
|
||||
draw_line(Vector2(OFFSET.x,0),Vector2(SIZE.x,0),box_color,1,true)
|
||||
draw_line(Vector2(SIZE.x,0),SIZE-Vector2(0,OFFSET.y),box_color,1,true)
|
||||
draw_points()
|
||||
draw_treshold()
|
||||
|
@ -1,8 +1,9 @@
|
||||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/ScatterChart/scatter_chart.gd" type="Script" id=2]
|
||||
|
||||
|
||||
[sub_resource type="Theme" id=1]
|
||||
|
||||
[node name="ScatterChart" type="Control"]
|
@ -1,7 +1,9 @@
|
||||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/ScatterChart2D/ScatterChart2D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=2]
|
||||
[ext_resource path="res://addons/easy_charts/ScatterChart2D/scatter_chart2D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=2]
|
||||
|
||||
|
||||
|
||||
[node name="ScatterChart2D" type="Node2D"]
|
||||
script = ExtResource( 1 )
|
@ -1,7 +1,9 @@
|
||||
[gd_scene load_steps=7 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/ScatterChart3D/ScatterChart3D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=2]
|
||||
[ext_resource path="res://addons/easy_charts/ScatterChart3D/scatter_chart3D.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.tscn" type="PackedScene" id=2]
|
||||
|
||||
|
||||
|
||||
[sub_resource type="SpatialMaterial" id=1]
|
||||
flags_unshaded = true
|
@ -1,12 +1,12 @@
|
||||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Legend/FunctionLegend.gd" type="Script" id=2]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Legend/function_legend.gd" type="Script" id=1]
|
||||
|
||||
[node name="FunctionLegend" type="VBoxContainer"]
|
||||
margin_right = 80.0
|
||||
margin_bottom = 26.0
|
||||
alignment = 1
|
||||
script = ExtResource( 2 )
|
||||
script = ExtResource( 1 )
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/Point.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point.gd" type="Script" id=1]
|
||||
|
||||
|
||||
[node name="Point" type="Control"]
|
||||
margin_left = -13.0
|
||||
|
@ -1,6 +1,7 @@
|
||||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.gd" type="Script" id=1]
|
||||
[ext_resource path="res://addons/easy_charts/Utilities/Point/point_data.gd" type="Script" id=1]
|
||||
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id=1]
|
||||
content_margin_left = 10.0
|
@ -15,8 +15,8 @@ onready var Legend = $Legend
|
||||
onready var ChartName : Label = $ChartName
|
||||
|
||||
# Scenes and Reosurces ......................
|
||||
var point_node : PackedScene = preload("../Point/Point.tscn")
|
||||
var LegendElement : PackedScene = preload("../Legend/FunctionLegend.tscn")
|
||||
var point_node : PackedScene = preload("../Point/point.tscn")
|
||||
var LegendElement : PackedScene = preload("../Legend/function_legend.tscn")
|
||||
|
||||
# Enums .....................................
|
||||
enum PointShapes { Dot, Triangle, Square, Cross }
|
||||
@ -92,6 +92,7 @@ var radius : float = 150.0 setget _set_radius,get_radius
|
||||
var column_width : float = 10 setget set_column_width
|
||||
var column_gap : float = 2 setget set_column_gap
|
||||
|
||||
# Calculations of decim and its relation with number of tics: https://www.desmos.com/calculator/jeiceaswiy
|
||||
var full_scale : float = 1.0 setget set_full_scale
|
||||
var x_decim : float = 5.0 setget set_x_decim
|
||||
var y_decim : float = 1.0 setget set_y_decim
|
||||
|
@ -51,8 +51,8 @@ var PointData: PointData
|
||||
var Outlines: Line2D
|
||||
var Grid: Node2D
|
||||
|
||||
var point_node: PackedScene = preload("../Point/Point.tscn")
|
||||
var FunctionLegend: PackedScene = preload("../Legend/FunctionLegend.tscn")
|
||||
var point_node: PackedScene = preload("../Point/point.tscn")
|
||||
var FunctionLegend: PackedScene = preload("../Legend/function_legend.tscn")
|
||||
|
||||
var font_size: float = 16
|
||||
var const_height: float = font_size / 2 * font_size / 20
|
||||
|
331
addons/easy_charts/Utilities/Scripts/scatter_chart_base.gd
Normal file
331
addons/easy_charts/Utilities/Scripts/scatter_chart_base.gd
Normal file
@ -0,0 +1,331 @@
|
||||
tool
|
||||
extends Chart
|
||||
class_name ScatterChartBase
|
||||
|
||||
# Base Class to be inherited by any chart that wants to plot points or series
|
||||
# of points in a two-variable space. It handles basic data structure and grid
|
||||
# layout and leaves to child classes the more specific behaviour.
|
||||
|
||||
var property_list = [
|
||||
# Chart Properties
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_CATEGORY,
|
||||
"name": "ScatterChartBase", #TODO Changue this in the child classes
|
||||
"type": TYPE_STRING
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/are_values_columns",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "-1,100,1",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/labels_index",
|
||||
"type": TYPE_INT
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Properties/show_x_values_as_labels",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
|
||||
# Chart Display
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "0.001, 10",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Display/x_decim",
|
||||
"type": TYPE_REAL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_RANGE,
|
||||
"hint_string": "0.001, 10",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Display/y_decim",
|
||||
"type": TYPE_REAL
|
||||
},
|
||||
|
||||
# Chart Style
|
||||
{
|
||||
"hint": 24,
|
||||
"hint_string": ("%d/%d:%s"
|
||||
%[TYPE_INT, PROPERTY_HINT_ENUM,
|
||||
PoolStringArray(PointShapes.keys()).join(",")]),
|
||||
"name": "Chart_Style/points_shape",
|
||||
"type": TYPE_ARRAY,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/function_colors",
|
||||
"type": TYPE_COLOR_ARRAY
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/box_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/v_lines_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/h_lines_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"class_name": "Font",
|
||||
"hint": PROPERTY_HINT_RESOURCE_TYPE,
|
||||
"hint_string": "Font",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/font",
|
||||
"type": TYPE_OBJECT
|
||||
},
|
||||
{
|
||||
"class_name": "Font",
|
||||
"hint": PROPERTY_HINT_RESOURCE_TYPE,
|
||||
"hint_string": "Font",
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/bold_font",
|
||||
"type": TYPE_OBJECT
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/font_color",
|
||||
"type": TYPE_COLOR
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/use_template",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_ENUM,
|
||||
"hint_string": PoolStringArray(Utilities.templates.keys()).join(","),
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Style/template",
|
||||
"type": TYPE_INT
|
||||
},
|
||||
|
||||
# Chart Modifiers
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Modifiers/treshold",
|
||||
"type": TYPE_VECTOR2
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Modifiers/only_disp_values",
|
||||
"type": TYPE_VECTOR2
|
||||
},
|
||||
{
|
||||
"hint": PROPERTY_HINT_NONE,
|
||||
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
|
||||
"name": "Chart_Modifiers/invert_chart",
|
||||
"type": TYPE_BOOL
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
func structure_datas(database : Array):
|
||||
# @labels_index can be either a column or a row relative to x values
|
||||
# @y_values can be either a column or a row relative to y values
|
||||
are_values_columns = invert_chart != are_values_columns
|
||||
if are_values_columns:
|
||||
for row in database.size():
|
||||
var t_vals : Array
|
||||
for column in database[row].size():
|
||||
if column == labels_index:
|
||||
var x_data = database[row][column]
|
||||
if typeof(x_data) == TYPE_INT or typeof(x_data) == TYPE_REAL:
|
||||
x_datas.append(x_data as float)
|
||||
else:
|
||||
x_datas.append(x_data.replace(",", ".") as float)
|
||||
else:
|
||||
if row != 0:
|
||||
var y_data = database[row][column]
|
||||
if typeof(y_data) == TYPE_INT or typeof(y_data) == TYPE_REAL:
|
||||
t_vals.append(y_data as float)
|
||||
else:
|
||||
t_vals.append(y_data.replace(",",".") as float)
|
||||
else:
|
||||
y_labels.append(str(database[row][column]))
|
||||
if not t_vals.empty():
|
||||
y_datas.append(t_vals)
|
||||
x_label = str(x_datas.pop_front())
|
||||
else:
|
||||
for row in database.size():
|
||||
if row == labels_index:
|
||||
x_datas = (database[row])
|
||||
x_label = x_datas.pop_front() as String
|
||||
else:
|
||||
var values = database[row] as Array
|
||||
y_labels.append(values.pop_front() as String)
|
||||
y_datas.append(values)
|
||||
for data in y_datas:
|
||||
for value in data.size():
|
||||
data[value] = data[value] as float
|
||||
|
||||
# draw y labels
|
||||
var to_order : Array
|
||||
var to_order_min : Array
|
||||
for cluster in y_datas.size():
|
||||
# define x_chors and y_chors
|
||||
var margin_max = y_datas[cluster].max()
|
||||
var margin_min = y_datas[cluster].min()
|
||||
to_order.append(margin_max)
|
||||
to_order_min.append(margin_min)
|
||||
|
||||
var y_margin_max = to_order.max()
|
||||
y_margin_min = to_order_min.min() if not origin_at_zero else 0
|
||||
v_dist = y_decim * pow(10.0, str(y_margin_max).split(".")[0].length() - 1)
|
||||
var multi = 0
|
||||
var p = (v_dist * multi) + y_margin_min
|
||||
y_chors.append(p as String)
|
||||
while p < y_margin_max:
|
||||
multi += 1
|
||||
p = (v_dist * multi) + y_margin_min
|
||||
y_chors.append(p as String)
|
||||
|
||||
# draw x_labels
|
||||
to_order.clear()
|
||||
to_order = x_datas
|
||||
var x_margin_max = to_order.max()
|
||||
x_margin_min = to_order.min() if not origin_at_zero else 0
|
||||
if not show_x_values_as_labels:
|
||||
h_dist = x_decim * pow(10.0, str(x_margin_max).split(".")[0].length() - 1)
|
||||
multi = 0
|
||||
p = (h_dist * multi) + x_margin_min
|
||||
x_labels.append(p as String)
|
||||
while p < x_margin_max:
|
||||
multi += 1
|
||||
p = (h_dist * multi) + x_margin_min
|
||||
x_labels.append(p as String)
|
||||
|
||||
OFFSET.x = str(y_margin_max).length() * font_size
|
||||
OFFSET.y = font_size * 2
|
||||
|
||||
|
||||
func build_chart():
|
||||
SIZE = get_size() - Vector2(OFFSET.x, 0)
|
||||
origin = Vector2(OFFSET.x, SIZE.y - OFFSET.y)
|
||||
|
||||
|
||||
func calculate_pass():
|
||||
if show_x_values_as_labels:
|
||||
x_chors = x_datas.duplicate(true) as PoolStringArray
|
||||
else:
|
||||
x_chors = x_labels
|
||||
|
||||
# calculate distance in pixel between 2 consecutive values/datas
|
||||
x_pass = (SIZE.x - OFFSET.x) / (x_chors.size() - 1 if x_chors.size() > 1 else x_chors.size())
|
||||
y_pass = (origin.y - ChartName.get_rect().size.y * 2) / (y_chors.size() - 1)
|
||||
|
||||
|
||||
func calculate_coordinates():
|
||||
x_coordinates.clear()
|
||||
y_coordinates.clear()
|
||||
point_values.clear()
|
||||
point_positions.clear()
|
||||
|
||||
for cluster in y_datas:
|
||||
var single_coordinates : Array
|
||||
for value in cluster.size():
|
||||
single_coordinates.append((cluster[value] - y_margin_min) * y_pass / v_dist)
|
||||
y_coordinates.append(single_coordinates)
|
||||
|
||||
if show_x_values_as_labels:
|
||||
for x in x_datas.size():
|
||||
x_coordinates.append(x_pass * x)
|
||||
else:
|
||||
for x in x_datas.size():
|
||||
x_coordinates.append((x_datas[x] - x_margin_min) * x_pass / h_dist)
|
||||
|
||||
for f in functions:
|
||||
point_values.append([])
|
||||
point_positions.append([])
|
||||
|
||||
for cluster in y_coordinates.size():
|
||||
for y in y_coordinates[cluster].size():
|
||||
if are_values_columns:
|
||||
point_values[y].append([x_datas[cluster], y_datas[cluster][y]])
|
||||
point_positions[y].append(Vector2(x_coordinates[cluster] + origin.x,
|
||||
origin.y - y_coordinates[cluster][y]))
|
||||
else:
|
||||
point_values[cluster].append([x_datas[y], y_datas[cluster][y]])
|
||||
point_positions[cluster].append(Vector2(x_coordinates[y] + origin.x,
|
||||
origin.y - y_coordinates[cluster][y]))
|
||||
|
||||
|
||||
func draw_grid():
|
||||
# ascisse
|
||||
for p in x_chors.size():
|
||||
var point : Vector2 = origin+Vector2((p)*x_pass,0)
|
||||
# v grid
|
||||
draw_line(point,point-Vector2(0,SIZE.y-OFFSET.y),v_lines_color,0.2,true)
|
||||
# ascisse
|
||||
draw_line(point-Vector2(0,5),point,v_lines_color,1,true)
|
||||
draw_string(font,point+Vector2(-const_width/2*x_chors[p].length(),font_size+const_height),x_chors[p],font_color)
|
||||
|
||||
# ordinate
|
||||
for p in y_chors.size():
|
||||
var point : Vector2 = origin-Vector2(0,(p)*y_pass)
|
||||
# h grid
|
||||
draw_line(point,point+Vector2(SIZE.x-OFFSET.x,0),h_lines_color,0.2,true)
|
||||
# ordinate
|
||||
draw_line(point,point+Vector2(5,0),h_lines_color,1,true)
|
||||
draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,-const_height),y_chors[p],font_color)
|
||||
|
||||
|
||||
func draw_chart_outlines():
|
||||
draw_line(origin,SIZE-Vector2(0,OFFSET.y),box_color,1,true)
|
||||
draw_line(origin,Vector2(OFFSET.x,0),box_color,1,true)
|
||||
draw_line(Vector2(OFFSET.x,0),Vector2(SIZE.x,0),box_color,1,true)
|
||||
draw_line(Vector2(SIZE.x,0),SIZE-Vector2(0,OFFSET.y),box_color,1,true)
|
||||
|
||||
|
||||
func draw_points():
|
||||
var defined_colors : bool = false
|
||||
if function_colors.size():
|
||||
defined_colors = true
|
||||
|
||||
for _function in point_values.size():
|
||||
var PointContainer : Control = Control.new()
|
||||
Points.add_child(PointContainer)
|
||||
|
||||
for function_point in point_values[_function].size():
|
||||
var point : Point = point_node.instance()
|
||||
point.connect("_point_pressed",self,"point_pressed")
|
||||
point.connect("_mouse_entered",self,"show_data")
|
||||
point.connect("_mouse_exited",self,"hide_data")
|
||||
|
||||
point.create_point(points_shape[_function], function_colors[_function],
|
||||
Color.white, point_positions[_function][function_point],
|
||||
point.format_value(point_values[_function][function_point], false, false),
|
||||
y_labels[_function] as String)
|
||||
|
||||
PointContainer.add_child(point)
|
||||
|
||||
|
||||
func draw_treshold():
|
||||
if v_dist != 0:
|
||||
treshold_draw = Vector2((treshold.x * x_pass) + origin.x ,origin.y - ((treshold.y * y_pass)/v_dist))
|
||||
if treshold.y != 0:
|
||||
draw_line(Vector2(origin.x, treshold_draw.y), Vector2(SIZE.x, treshold_draw.y), Color.red, 0.4, true)
|
||||
if treshold.x != 0:
|
||||
draw_line(Vector2(treshold_draw.x, 0), Vector2(treshold_draw.x, SIZE.y - OFFSET.y), Color.red, 0.4, true)
|
Loading…
Reference in New Issue
Block a user