From 0cf6873d758d5f5d912adbfd9313a696d1b1a848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Santilio?= Date: Sat, 30 May 2020 01:42:58 +0200 Subject: [PATCH] update v0.2.8 --- .gitignore | 1 + README.md | 14 +- addons/easy_charts/BarChart/BarChart.gd | 363 +++++------- addons/easy_charts/BarChart2D/BarChart2D.gd | 4 +- addons/easy_charts/LineChart/LineChart.gd | 393 ++++--------- addons/easy_charts/LineChart2D/LineChart2D.gd | 8 +- addons/easy_charts/RadarChart/RadarChart.gd | 366 +++++------- .../easy_charts/ScatterChart/ScatterChart.gd | 392 ++++--------- .../ScatterChart2D/ScatterChart2D.gd | 4 +- addons/easy_charts/Utilities/Point/Point.gd | 37 +- addons/easy_charts/Utilities/Scripts/Chart.gd | 536 +++++++++++++++++- .../easy_charts/Utilities/Scripts/Chart2D.gd | 4 +- .../Utilities/Scripts/ChartObject.gd | 39 ++ .../Utilities/Scripts/utilities.gd | 3 + addons/easy_charts/plugin.cfg | 2 +- addons/easy_charts/plugin.gd | 2 + 16 files changed, 1099 insertions(+), 1069 deletions(-) create mode 100644 addons/easy_charts/Utilities/Scripts/ChartObject.gd diff --git a/.gitignore b/.gitignore index 270c39f..b754931 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.import *.png + diff --git a/README.md b/README.md index 6290df2..4df77f5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![version](https://img.shields.io/badge/plugin%20version-0.2.5-blue)](https://github.com/fenix-hub/godot-engine.easy-charts) +[![version](https://img.shields.io/badge/plugin%20version-0.2.8-blue)](https://github.com/fenix-hub/godot-engine.easy-charts) [![updates](https://img.shields.io/badge/plugin%20updates-on%20discord-purple)](https://discord.gg/JNrcucg) [![paypal](https://img.shields.io/badge/donations-PayPal-cyan)](https://paypal.me/NSantilio?locale.x=it_IT) @@ -9,7 +9,7 @@ Check my **[Discord](https://discord.gg/KnJGY9S)** to stay updated on this repos A library of Charts plotted in Control, 2D and 3D nodes to visualize general purpose datasets. Author: *"Nicolo (fenix) Santilio"* -Version: *0.2.5* +Version: *0.2.8* Wiki: *[wip]* Godot Version: *3.2stable* @@ -113,7 +113,7 @@ However, adding custom templates is not yet recommended, since it would require ``` # Available Charts and when to use them -This library offers a set of chart for each main Godot Node: +This library offers a set of charts for each main Godot Node: - **Control Nodes:** "Control Charts" are fast Charts that can be plotted in a Control space, such as UIs or Control user interactable areas. They offer basic Control properties, such as Margins, size inheritance and control. No animations, no real time changes, just charts. - **2D Nodes:** "2D Charts" are a set of Charts which can be Used in 2D spaces. They offer additional tools, such as animations and real time changes in editor. They can be used to implement more aesthetic charts in 2D contexts. - **[wip] 3D Nodes:** "3D Charts" are a set of Charts which can be Used in both 2D and 3D spaces. They offer the possibility to plot 3D datasets, which are common in machine learning contexts or just data analysis. A Camera Control will also be available, which can be used to move around the chart. @@ -134,7 +134,7 @@ This library offers a set of chart for each main Godot Node: ### Some Examples ![example01](imgs/scatter.gif) -![example02](imgs/radar.png) +![example02](imgs/example02.png) ![example03](imgs/example03.gif) ##### Some references for charts and plots @@ -150,8 +150,4 @@ I don't assume any responsibility for possible corruptions of your project. It i ----------------- > This text file was created via [TextEditor Integration](https://github.com/fenix-hub/godot-engine.text-editor) inside Godot Engine's Editor. - - - - - +> This text file was pushed via [GitHub Integration](https://github.com/fenix-hub/godot-engine.github-integretion) inside Godot Engine's Editor. diff --git a/addons/easy_charts/BarChart/BarChart.gd b/addons/easy_charts/BarChart/BarChart.gd index c043c2a..fdf91e3 100644 --- a/addons/easy_charts/BarChart/BarChart.gd +++ b/addons/easy_charts/BarChart/BarChart.gd @@ -16,194 +16,137 @@ values of more than one measured variable. / source : Wikipedia / """ -onready var PointData = $PointData/PointData -onready var Points = $Points -onready var Legend = $Legend - -var point_node : PackedScene = preload("../Utilities/Point/Point.tscn") -var FunctionLegend : PackedScene = preload("../Utilities/Legend/FunctionLegend.tscn") - -var font_size : float = 16 -var const_height : float = font_size/2*font_size/20 -var const_width : float = font_size/2 - -var OFFSET : Vector2 = Vector2(0,0) - -#-------------------------------------------------------------------------# -var origin : Vector2 - -# actual distance between x and y values -var x_pass : float -var y_pass : float - -# vertical distance between y consecutive points used for intervals -var v_dist : float -var h_dist : float - -# quantization, representing the interval in which values will be displayed - -# define values on x an y axis -var x_chors : Array -var y_chors : Array - -# actual coordinates of points (in pixel) -var x_coordinates : Array -var y_coordinates : Array - -# datas contained in file -var datas : Array - -# amount of functions to represent -var functions : int = 0 - -var x_label : String - -# database values -var x_datas : Array -var y_datas : Array - -# labels displayed on chart -var x_labels : Array -var y_labels : Array - -var x_margin_min : int = 0 -var y_margin_min : int = 0 - -# actual values of point, from the database -var point_values : Array - -# actual position of points in pixel -var point_positions : Array - -var legend : Array setget set_legend,get_legend - # --------------------- -var SIZE : Vector2 = Vector2() -export (String, FILE, "*.txt, *.csv") var source : String = "" -export (String) var delimiter : String = ";" -export (bool) var origin_at_zero : bool = true -export (bool) var are_values_columns : bool = false -export (int,0,100) var x_values_index : int = 0 -export(bool) var show_x_values_as_labels : bool = true - -export (float,1,20,0.5) var column_width : float = 10 -export (float,0,10,0.5) var column_gap : float = 2 - -export (float,0.1,10.0) var x_decim : float = 5.0 -export (float,0.1,10.0) var y_decim : float = 5.0 - -export (point_shapes) var point_shape : int = 0 - -export (PoolColorArray) var function_colors = [Color("#1e1e1e")] -export (Color) var v_lines_color : Color = Color("#cacaca") -export (Color) var h_lines_color : Color = Color("#cacaca") - -export (bool) var boxed : bool = true -export (Color) var box_color : Color = Color("#1e1e1e") -export (Font) var font : Font -export (Font) var bold_font : Font -export (Color) var font_color : Color = Color("#1e1e1e") -export (templates_names) var template : int = Chart.templates_names.Default setget apply_template -export (bool) var invert_chart : bool = false - -var templates : Dictionary = {} - -signal chart_plotted(chart) -signal point_pressed(point) - -func _point_plotted(): - pass - -func _ready(): - pass - -func load_font(): - if font != null: - font_size = font.get_height() - var theme : Theme = Theme.new() - theme.set_default_font(font) - PointData.set_theme(theme) - else: - var lbl = Label.new() - font = lbl.get_font("") - lbl.free() - if bold_font != null: - PointData.Data.set("custom_fonts/font",bold_font) - -func _plot(source_ : String, delimiter_ : String, are_values_columns_ : bool, x_values_index_ : int): - randomize() - - - load_font() - PointData.hide() - - datas = read_datas(source_,delimiter_) - count_functions() - structure_datas(datas,are_values_columns_,x_values_index_) - build_chart() - calculate_pass() - calculate_coordinates() - calculate_colors() - create_legend() - emit_signal("chart_plotted") - -func plot(): - randomize() - - - load_font() - PointData.hide() - - if source == "" or source == null: - Utilities._print_message("Can't plot a chart without a Source file. Please, choose it in editor, or use the custom function _plot().",1) - return - datas = read_datas(source,delimiter) - count_functions() - structure_datas(datas,are_values_columns,x_values_index) - build_chart() - calculate_pass() - calculate_coordinates() - calculate_colors() - create_legend() - emit_signal("chart_plotted") - -func clear_points(): - if Points.get_children().size(): - for function in Points.get_children(): - function.queue_free() - for legend in Legend.get_children(): - legend.queue_free() - -func calculate_colors(): - if function_colors.empty() or function_colors.size() < functions: - for function in functions: - function_colors.append(Color("#1e1e1e")) +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 + }, + { + "hint": PROPERTY_HINT_RANGE, + "hint_string": "1,20,0.5", + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Properties/column_width", + "type": TYPE_REAL + }, + { + "hint": PROPERTY_HINT_RANGE, + "hint_string": "0,10,0.5", + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Properties/column_gap", + "type": TYPE_REAL + }, + + # 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(TemplatesNames.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 build_chart(): SIZE = get_size() origin = Vector2(OFFSET.x,SIZE.y-OFFSET.y) -func point_pressed(point : Point): - emit_signal("point_pressed",point) - -func _enter_tree(): - templates = Utilities._load_templates() - _ready() - -func read_datas(source : String, delimiter : String): - var file : File = File.new() - file.open(source,File.READ) - var content : Array - while not file.eof_reached(): - var line : PoolStringArray = file.get_csv_line(delimiter) - content.append(line) - file.close() - for data in content: - if data.size() < 2: - content.erase(data) - return content - func structure_datas(database : Array, are_values_columns : bool, x_values_index : int): # @x_values_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 @@ -387,7 +330,7 @@ func _draw(): point.connect("_mouse_entered",self,"show_data") point.connect("_mouse_exited",self,"hide_data") - point.create_point(point_shape, function_colors[function_point if invert_chart else _function], + point.create_point(points_shape[_function], function_colors[function_point if invert_chart else _function], Color.white, point_positions[_function][function_point], point.format_value(point_values[_function][function_point], false, false), y_labels[function_point if invert_chart else _function] as String) @@ -421,50 +364,12 @@ func draw_grid(): draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,font_size/2),y_chors[p],font_color) func draw_chart_outlines(): - if boxed: - draw_line(Vector2(origin.x,0),Vector2(SIZE.x,0),box_color,1,true) - draw_line(Vector2(SIZE.x,0),Vector2(SIZE.x,origin.y),box_color,1,true) +# if boxed: + draw_line(Vector2(origin.x,0),Vector2(SIZE.x,0),box_color,1,true) + draw_line(Vector2(SIZE.x,0),Vector2(SIZE.x,origin.y),box_color,1,true) draw_line(Vector2(SIZE.x,origin.y),origin,box_color,1,true) draw_line(origin,Vector2(origin.x,0),box_color,1,true) -func redraw(): - build_chart() - calculate_pass() - calculate_coordinates() - update() - - -func show_data(point): - PointData.update_datas(point) - PointData.show() - -func hide_data(): - PointData.hide() - -func set_legend(l : Array): - legend = l - -func get_legend(): - return legend - -func invert_chart(): - invert_chart = !invert_chart - count_functions() - redraw() - create_legend() - -func count_functions(): - if are_values_columns: - if not invert_chart: - functions = datas[0].size()-1 - else: - functions = datas.size()-1 - else: - if invert_chart: - functions = datas[0].size()-1 - else: - functions = datas.size()-1 - func create_legend(): legend.clear() for function in functions: @@ -482,15 +387,3 @@ func create_legend(): function_legend.create_legend(f_name,function_colors[function],bold_font,font_color) legend.append(function_legend) -func apply_template(template_name : int): - template = template_name - templates = Utilities._load_templates() - if template_name!=null: - var custom_template = templates.get(templates.keys()[template_name]) - function_colors = custom_template.function_colors as PoolColorArray - v_lines_color = Color(custom_template.v_lines_color) - h_lines_color = Color(custom_template.h_lines_color) - box_color = Color(custom_template.outline_color) - font_color = Color(custom_template.font_color) - property_list_changed_notify() - diff --git a/addons/easy_charts/BarChart2D/BarChart2D.gd b/addons/easy_charts/BarChart2D/BarChart2D.gd index 2735f15..91cfef2 100644 --- a/addons/easy_charts/BarChart2D/BarChart2D.gd +++ b/addons/easy_charts/BarChart2D/BarChart2D.gd @@ -96,7 +96,7 @@ export (float,0,10,0.5) var column_gap : float = 2 export (float,0.1,10.0) var x_decim : float = 5.0 export (float,0.1,10.0) var y_decim : float = 5.0 -export (point_shapes) var point_shape : int = 0 +export (PointShapes) var point_shape : int = 0 export (PoolColorArray) var function_colors = [Color("#1e1e1e")] export (Color) var v_lines_color : Color = Color("#cacaca") export (Color) var h_lines_color : Color = Color("#cacaca") @@ -106,7 +106,7 @@ export (Color) var box_color : Color = Color("#1e1e1e") export (Font) var font : Font export (Font) var bold_font : Font export (Color) var font_color : Color = Color("#1e1e1e") -export (templates_names) var template : int = Chart.templates_names.Default setget apply_template +export (TemplatesNames) var template : int = Chart.TemplatesNames.Default setget apply_template export (float,0.1,1) var drawing_duration : float = 0.5 export (bool) var invert_chart : bool = false diff --git a/addons/easy_charts/LineChart/LineChart.gd b/addons/easy_charts/LineChart/LineChart.gd index e30a065..9f68da1 100644 --- a/addons/easy_charts/LineChart/LineChart.gd +++ b/addons/easy_charts/LineChart/LineChart.gd @@ -15,172 +15,118 @@ In these cases they are known as run charts. / source : Wikipedia / """ -onready var PointData = $PointData/PointData -onready var Points = $Points -onready var Legend = $Legend - -var point_node : PackedScene = preload("../Utilities/Point/Point.tscn") -var FunctionLegend : PackedScene = preload("../Utilities/Legend/FunctionLegend.tscn") - -var font_size : float = 16 -var const_height : float = font_size/2*font_size/20 -var const_width : float = font_size/2 - -var OFFSET : Vector2 = Vector2(0,0) - -#-------------------------------------------------------------------------# -var origin : Vector2 - -# actual distance between x and y values -var x_pass : float -var y_pass : float - -# vertical distance between y consecutive points used for intervals -var v_dist : float -var h_dist : float - -# quantization, representing the interval in which values will be displayed - - -# define values on x an y axis -var x_chors : Array -var y_chors : Array - -# actual coordinates of points (in pixel) -var x_coordinates : Array -var y_coordinates : Array - -# datas contained in file -var datas : Array - -# amount of functions to represent -var functions : int = 0 - - -# database values -var x_datas : Array -var y_datas : Array - -# labels displayed on chart -var x_label : String - -var x_labels : Array -var y_labels : Array - -var x_margin_min : int = 0 -var y_margin_min : int = 0 - -# actual values of point, from the database -var point_values : Array - -# actual position of points in pixel -var point_positions : Array - -var legend : Array setget set_legend,get_legend - # --------------------- -var SIZE : Vector2 = Vector2() -export (String, FILE, "*.txt, *.csv") var source : String = "" -export (String) var delimiter : String = ";" -export (bool) var origin_at_zero : bool = true -export (bool) var are_values_columns : bool = false -export (int,0,100) var x_values_index : int = 0 -export(bool) var show_x_values_as_labels : bool = true - -#export (float,1,20,0.5) var column_width : float = 10 -#export (float,0,10,0.5) var column_gap : float = 2 - -export (float,0.1,10.0) var x_decim : float = 5.0 -export (float,0.1,10.0) var y_decim : float = 5.0 -export (point_shapes) var point_shape : int = 0 -export (PoolColorArray) var function_colors = [Color("#1e1e1e")] -export (Color) var v_lines_color : Color = Color("#cacaca") -export (Color) var h_lines_color : Color = Color("#cacaca") - -export (bool) var boxed : bool = true -export (Color) var box_color : Color = Color("#1e1e1e") -export (Font) var font : Font -export (Font) var bold_font : Font -export (Color) var font_color : Color = Color("#1e1e1e") -export (templates_names) var template : int = Chart.templates_names.Default setget apply_template -export (bool) var invert_chart : bool = false - - -var templates : Dictionary = {} - -signal chart_plotted(chart) -signal point_pressed(point) - - -func _ready(): - pass - -func _plot(source_ : String, delimiter_ : String, are_values_columns_ : bool, x_values_index_ : int, invert_chart_ : bool = false): - randomize() - - load_font() - PointData.hide() - - datas = read_datas(source_,delimiter_) - count_functions() - structure_datas(datas,are_values_columns_,x_values_index_) - build_chart() - calculate_pass() - calculate_coordinates() - calculate_colors() - create_legend() - emit_signal("chart_plotted") - -func plot(): - randomize() - - load_font() - PointData.hide() - - if source == "" or source == null: - Utilities._print_message("Can't plot a chart without a Source file. Please, choose it in editor, or use the custom function _plot().",1) - return - datas = read_datas(source,delimiter) - count_functions() - structure_datas(datas,are_values_columns,x_values_index) - build_chart() - calculate_pass() - calculate_coordinates() - calculate_colors() - create_legend() - emit_signal("chart_plotted") - -func calculate_colors(): - if function_colors.empty() or function_colors.size() < functions: - for function in functions: - function_colors.append(Color("#1e1e1e")) - -func load_font(): - if font != null: - font_size = font.get_height() - var theme : Theme = Theme.new() - theme.set_default_font(font) - PointData.set_theme(theme) - else: - var lbl = Label.new() - font = lbl.get_font("") - lbl.free() - if bold_font != null: - PointData.Data.set("custom_fonts/font",bold_font) - -func read_datas(source : String, delimiter : String): - var file : File = File.new() - file.open(source,File.READ) - var content : Array - while not file.eof_reached(): - var line : PoolStringArray = file.get_csv_line(delimiter) - content.append(line) - file.close() - for data in content: - if data.size() < 2: - content.erase(data) - return content +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 + }, + + # 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(TemplatesNames.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, are_values_columns : bool, x_values_index : int): # @x_values_index can be either a column or a row relative to x values @@ -345,12 +291,6 @@ func calculate_coordinates(): 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 redraw(): - build_chart() - calculate_pass() - calculate_coordinates() - update() - func _draw(): clear_points() @@ -371,7 +311,7 @@ func _draw(): point.connect("_mouse_entered",self,"show_data") point.connect("_mouse_exited",self,"hide_data") - point.create_point(point_shape,function_colors[function_point if invert_chart else _function], + point.create_point(points_shape[_function], function_colors[function_point if invert_chart else _function], Color.white, point_positions[_function][function_point], point.format_value(point_values[_function][function_point], false, false), y_labels[function_point if invert_chart else _function] as String) @@ -405,114 +345,3 @@ func draw_chart_outlines(): 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 create_legend(): - legend.clear() - for function in functions: - var function_legend = FunctionLegend.instance() - var f_name : String - if invert_chart: - f_name = x_datas[function] as String - else: - f_name = y_labels[function] - var legend_font : Font - if font != null: - legend_font = font - if bold_font != null: - legend_font = bold_font - function_legend.create_legend(f_name,function_colors[function],bold_font,font_color) - legend.append(function_legend) - - - -var can_grab_x : bool = false -var can_grab_y : bool = false -var can_move : bool = false -var range_mouse : float = 7 - -#func _input(event): -# if not can_grab_x and (event.position.x > (SIZE.x-range_mouse + rect_position.x) and event.position.x < (SIZE.x+range_mouse + rect_position.x)) : -# set_default_cursor_shape(Control.CURSOR_HSIZE) -# if Input.is_action_pressed("mouse_left"): -# can_grab_x = true -# -# if Input.is_action_just_released("mouse_left") and can_grab_x: -# can_grab_x = false -# -# if not can_grab_y and (event.position.y > ( rect_position.y + origin.y-range_mouse) and event.position.y < (rect_position.y+ origin.y+range_mouse)) : -# set_default_cursor_shape(Control.CURSOR_VSIZE) -# if Input.is_action_pressed("mouse_left"): -# can_grab_y = true -# -# if Input.is_action_just_released("mouse_left") and can_grab_y: -# can_grab_y = false -# -# if (event.position.x > SIZE.x-range_mouse+rect_position.x and event.position.x < SIZE.x+range_mouse + rect_position.x) and (event.position.y > rect_position.y+origin.y-range_mouse and event.position.y < rect_position.y+origin.y+range_mouse): -# set_default_cursor_shape(Control.CURSOR_FDIAGSIZE) -# if not (event.position.x > SIZE.x-range_mouse+rect_position.x and event.position.x < SIZE.x+range_mouse + rect_position.x) and not (event.position.y > rect_position.y+ origin.y-range_mouse and event.position.y < rect_position.y+origin.y+range_mouse ): -# set_default_cursor_shape(Control.CURSOR_ARROW) - -# -#func _process(delta): -# if can_grab_x: -# PointData.hide() -# get_parent().rect_size.x = get_global_mouse_position().x - rect_position.x -# redraw() -# -# if can_grab_y: -# PointData.hide() -# get_parent().rect_size.y = get_global_mouse_position().y - rect_position.y + OFFSET.y -# redraw() - -func show_data(point): - PointData.update_datas(point) - PointData.show() - -func hide_data(): - PointData.hide() - -func clear_points(): - if Points.get_children(): - for function in Points.get_children(): - function.queue_free() - for legend in Legend.get_children(): - legend.queue_free() - -func set_legend(l : Array): - legend = l - -func get_legend(): - return legend - -func invert_chart(): - invert_chart = !invert_chart - count_functions() - redraw() - create_legend() - -func count_functions(): - if are_values_columns: - if not invert_chart: - functions = datas[0].size()-1 - else: - functions = datas.size()-1 - else: - if invert_chart: - functions = datas[0].size()-1 - else: - functions = datas.size()-1 - -func apply_template(template_name : int): - template = template_name - templates = Utilities._load_templates() - if template_name!=null: - var custom_template = templates.get(templates.keys()[template_name]) - function_colors = custom_template.function_colors as PoolColorArray - v_lines_color = Color(custom_template.v_lines_color) - h_lines_color = Color(custom_template.h_lines_color) - box_color = Color(custom_template.outline_color) - font_color = Color(custom_template.font_color) - property_list_changed_notify() - -func _enter_tree(): - _ready() diff --git a/addons/easy_charts/LineChart2D/LineChart2D.gd b/addons/easy_charts/LineChart2D/LineChart2D.gd index 7bd9765..b718149 100644 --- a/addons/easy_charts/LineChart2D/LineChart2D.gd +++ b/addons/easy_charts/LineChart2D/LineChart2D.gd @@ -96,7 +96,7 @@ export(bool) var show_x_values_as_labels : bool = true export (float,0.1,10.0) var x_decim : float = 5.0 export (float,0.1,10.0) var y_decim : float = 5.0 -export (point_shapes) var point_shape : int = 0 +export (PointShapes) var point_shape : int = 0 export (PoolColorArray) var function_colors = [Color("#1e1e1e")] export (Color) var v_lines_color : Color = Color("#cacaca") export (Color) var h_lines_color : Color = Color("#cacaca") @@ -106,7 +106,7 @@ export (Color) var box_color : Color = Color("#1e1e1e") export (Font) var font : Font export (Font) var bold_font : Font export (Color) var font_color : Color = Color("#1e1e1e") -export (templates_names) var template : int = Chart.templates_names.Default setget apply_template +export (TemplatesNames) var template : int = Chart.TemplatesNames.Default setget apply_template export (float,0.1,1) var drawing_duration : float = 0.5 export (bool) var invert_chart : bool = false @@ -562,3 +562,7 @@ func apply_template(template_name : int): func _enter_tree(): _ready() + +# Signal Repeaters +func point_pressed(point : Point) -> Point: + return point diff --git a/addons/easy_charts/RadarChart/RadarChart.gd b/addons/easy_charts/RadarChart/RadarChart.gd index c80acd7..0ceec77 100644 --- a/addons/easy_charts/RadarChart/RadarChart.gd +++ b/addons/easy_charts/RadarChart/RadarChart.gd @@ -13,175 +13,116 @@ distinct correlations, trade-offs, and a multitude of other comparative measures / source : Wikipedia / """ -onready var PointData = $PointData/PointData -onready var Points = $Points -onready var Legend = $Legend - -var point_node : PackedScene = preload("../Utilities/Point/Point.tscn") -var FunctionLegend : PackedScene = preload("../Utilities/Legend/FunctionLegend.tscn") - -var font_size : float = 16 -var const_height : float = font_size/2*font_size/20 -var const_width : float = font_size/2 - -var OFFSET : Vector2 = Vector2(0,0) - -#-------------------------------------------------------------------------# -var origin : Vector2 - -# actual distance between x and y values -var x_pass : float -var y_pass : float - -# vertical distance between y consecutive points used for intervals -var v_dist : float -var h_dist : float - -# quantization, representing the interval in which values will be displayed - - -# define values on x an y axis -var x_chors : Array -var y_chors : Array - -# actual coordinates of points (in pixel) -var x_coordinates : Array -var y_coordinates : Array - -# datas contained in file -var datas : Array - -# amount of functions to represent -var functions : int = 0 - - -# database values -var x_datas : Array -var y_datas : Array - -# labels displayed on chart -var x_label : String - -var x_labels : Array -var y_labels : Array - -var x_margin_min : int = 0 -var y_margin_min : int = 0 - -# actual values of point, from the database -var point_values : Array - -# actual position of points in pixel -var point_positions : Array - -var legend : Array setget set_legend,get_legend - -# --------------------- -var SIZE : Vector2 = Vector2() -export (String, FILE, "*.txt, *.csv") var source : String = "" setget set_source -export (String) var delimiter : String = ";" -#export (bool) var origin_at_zero : bool = true - -export (bool) var are_values_columns : bool = false -export (int,-1,100) var labels_index : int = 0 -export (int,-1,100) var function_names_index : int = 0 -#export(bool) var show_x_values_as_labels : bool = true - -#export (float,1,20,0.5) var column_width : float = 10 -#export (float,0,10,0.5) var column_gap : float = 2 -export (bool) var use_height_as_radius : bool = false -export (float) var radius : float = 150.0 - -#export (float,0.1,10.0) var x_decim : float = 5.0 -#export (float,0.1,10.0) var y_decim : float = 5.0 -export (float,0.1,100) var full_scale : float = 1.0 - -export (point_shapes) var point_shape : int = 0 -export (PoolColorArray) var function_colors = [Color("#1e1e1e")] -export (Color) var outline_color : Color = Color("#1e1e1e") -export (Color) var grid_color : Color = Color("#1e1e1e") -export (Font) var font : Font -export (Font) var bold_font : Font -export (Color) var font_color : Color = Color("#1e1e1e") - -export (templates_names) var template : int = Chart.templates_names.Default setget apply_template -#export (bool) var invert_chart : bool = false -export (float,0,360) var rotation : float = 0 - -var templates : Dictionary = {} - -signal chart_plotted(chart) -signal point_pressed(point) - - -func _ready(): - pass - -func _plot(source_ : String, delimiter_ : String, are_values_columns_ : bool, x_values_index_ : int, invert_chart_ : bool = false): - randomize() - - load_font() - PointData.hide() - - datas = read_datas(source_,delimiter_) - structure_datas(datas,are_values_columns_,x_values_index_) - build_chart() - count_functions() - calculate_pass() - calculate_coordinates() - calculate_colors() - create_legend() - emit_signal("chart_plotted") - -func plot(): - randomize() - - load_font() - PointData.hide() - - if source == "" or source == null: - Utilities._print_message("Can't plot a chart without a Source file. Please, choose it in editor, or use the custom function _plot().",1) - return - datas = read_datas(source,delimiter) - structure_datas(datas,are_values_columns,labels_index) - build_chart() - count_functions() - calculate_pass() - calculate_coordinates() - calculate_colors() - create_legend() - emit_signal("chart_plotted") - -func calculate_colors(): - if function_colors.empty() or function_colors.size() < functions: - for function in functions: - function_colors.append(Color("#1e1e1e")) - -func load_font(): - if font != null: - font_size = font.get_height() - var theme : Theme = Theme.new() - theme.set_default_font(font) - PointData.set_theme(theme) - else: - var lbl = Label.new() - font = lbl.get_font("") - lbl.free() - if bold_font != null: - PointData.Data.set("custom_fonts/font",bold_font) - -func read_datas(source : String, delimiter : String): - var file : File = File.new() - file.open(source,File.READ) - var content : Array - while not file.eof_reached(): - var line : PoolStringArray = file.get_csv_line(delimiter) - content.append(line) - file.close() - for data in content: - if data.size() < 2 or data.empty(): - content.erase(data) - return content +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_RANGE, + "hint_string": "-1,100,1", + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Properties/function_names_index", + "type": TYPE_INT + }, + { + "hint": PROPERTY_HINT_NONE, + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Properties/use_height_as_radius", + "type": TYPE_BOOL + }, + { + "hint": PROPERTY_HINT_RANGE, + "hint_string": "0,2000", + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Properties/radius", + "type": TYPE_REAL + }, + + # Chart Display + { + "hint": PROPERTY_HINT_RANGE, + "hint_string": "0.1,100", + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Display/full_scale", + "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/outline_color", + "type": TYPE_COLOR + }, + { + "hint": PROPERTY_HINT_NONE, + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Style/grid_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(TemplatesNames.keys()).join(","), + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Style/template", + "type": TYPE_INT + }, + { + "hint": PROPERTY_HINT_RANGE, + "hint_string": "0,360", + "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, + "name": "Chart_Modifiers/rotation", + "type": TYPE_REAL + }, + ] func structure_datas(database : Array, are_values_columns : bool, labels_index : int): # @x_values_index can be either a column or a row relative to x values @@ -251,8 +192,8 @@ func calculate_coordinates(): var scalar_factor : float = (x_chors[chor] as float/x_chors.back() as float) for function in functions: var angle : float = ((2 * PI * function) / functions) - PI /2 + deg2rad(rotation) - var x_coordinate : float = (radius if not use_height_as_radius else SIZE.y/2) * scalar_factor * cos(angle) + origin.x - var y_coordinate : float = (radius if not use_height_as_radius else SIZE.y/2) * scalar_factor * sin(angle) + origin.y + var x_coordinate : float = (radius if (not use_height_as_radius and radius (SIZE.x-range_mouse + rect_position.x) and event.position.x < (SIZE.x+range_mouse + rect_position.x)) : -# set_default_cursor_shape(Control.CURSOR_HSIZE) -# if Input.is_action_pressed("mouse_left"): -# can_grab_x = true -# -# if Input.is_action_just_released("mouse_left") and can_grab_x: -# can_grab_x = false -# -# if not can_grab_y and (event.position.y > ( rect_position.y + origin.y-range_mouse) and event.position.y < (rect_position.y+ origin.y+range_mouse)) : -# set_default_cursor_shape(Control.CURSOR_VSIZE) -# if Input.is_action_pressed("mouse_left"): -# can_grab_y = true -# -# if Input.is_action_just_released("mouse_left") and can_grab_y: -# can_grab_y = false -# -# if (event.position.x > SIZE.x-range_mouse+rect_position.x and event.position.x < SIZE.x+range_mouse + rect_position.x) and (event.position.y > rect_position.y+origin.y-range_mouse and event.position.y < rect_position.y+origin.y+range_mouse): -# set_default_cursor_shape(Control.CURSOR_FDIAGSIZE) -# if not (event.position.x > SIZE.x-range_mouse+rect_position.x and event.position.x < SIZE.x+range_mouse + rect_position.x) and not (event.position.y > rect_position.y+ origin.y-range_mouse and event.position.y < rect_position.y+origin.y+range_mouse ): -# set_default_cursor_shape(Control.CURSOR_ARROW) - -# -#func _process(delta): -# if can_grab_x: -# PointData.hide() -# get_parent().rect_size.x = get_global_mouse_position().x - rect_position.x -# redraw() -# -# if can_grab_y: -# PointData.hide() -# get_parent().rect_size.y = get_global_mouse_position().y - rect_position.y + OFFSET.y -# redraw() - -func show_data(point): - PointData.update_datas(point) - PointData.show() - -func hide_data(): - PointData.hide() - -func clear_points(): - if Points.get_children(): - for function in Points.get_children(): - function.queue_free() - for legend in Legend.get_children(): - legend.queue_free() - -func set_legend(l : Array): - legend = l - -func get_legend(): - return legend - -func invert_chart(): - invert_chart = !invert_chart - count_functions() - redraw() - create_legend() - -func count_functions(): - if are_values_columns: - if not invert_chart: - functions = datas[0].size()-1 - else: - functions = datas.size()-1 - else: - if invert_chart: - functions = datas[0].size()-1 - else: - functions = datas.size()-1 - -func apply_template(template_name : int): - template = template_name - templates = Utilities._load_templates() - if template_name!=null: - var custom_template = templates.get(templates.keys()[template_name]) - function_colors = custom_template.function_colors as PoolColorArray - v_lines_color = Color(custom_template.v_lines_color) - h_lines_color = Color(custom_template.h_lines_color) - box_color = Color(custom_template.outline_color) - font_color = Color(custom_template.font_color) - property_list_changed_notify() - -func _enter_tree(): - templates = Utilities._load_templates() - _ready() diff --git a/addons/easy_charts/ScatterChart2D/ScatterChart2D.gd b/addons/easy_charts/ScatterChart2D/ScatterChart2D.gd index 0c31c79..5a1e23a 100644 --- a/addons/easy_charts/ScatterChart2D/ScatterChart2D.gd +++ b/addons/easy_charts/ScatterChart2D/ScatterChart2D.gd @@ -93,7 +93,7 @@ export(bool) var show_x_values_as_labels : bool = true export (float,0.1,10.0) var x_decim : float = 5.0 export (float,0.1,10.0) var y_decim : float = 5.0 -export (point_shapes) var point_shape : int = 0 +export (Array, PointShapes) var points_shape : Array = 0 export (PoolColorArray) var function_colors = [Color("#1e1e1e")] as PoolColorArray export (Color) var v_lines_color : Color = Color("#cacaca") export (Color) var h_lines_color : Color = Color("#cacaca") @@ -103,7 +103,7 @@ export (Color) var box_color : Color = Color("#1e1e1e") export (Font) var font : Font export (Font) var bold_font : Font export (Color) var font_color : Color = Color("#1e1e1e") -export (templates_names) var template : int = templates_names.Default setget apply_template +export (TemplatesNames) var template : int = TemplatesNames.Default setget apply_template export (float,0.1,1) var drawing_duration : float = 0.5 export (bool) var invert_chart : bool = false diff --git a/addons/easy_charts/Utilities/Point/Point.gd b/addons/easy_charts/Utilities/Point/Point.gd index 6e65650..5e1b850 100644 --- a/addons/easy_charts/Utilities/Point/Point.gd +++ b/addons/easy_charts/Utilities/Point/Point.gd @@ -24,7 +24,6 @@ signal _point_pressed(point) func _ready(): pass # Replace with function body. - func _draw(): if mouse_entered: draw_point(7,color_outline) @@ -35,12 +34,12 @@ func draw_point(size : float, color : Color): SHAPES.DOT: draw_circle(OFFSET, size, color) SHAPES.TRIANGLE: - size+=4 + size+=6 draw_colored_polygon([ OFFSET-Vector2(0,size/2), OFFSET+Vector2(1,1)*size/2, OFFSET-Vector2(1,-1)*size/2 ], color,[],null,null,false) SHAPES.SQUARE: - size+=2 + size+=4 draw_colored_polygon([ OFFSET-Vector2(1,1)*size/2, OFFSET-Vector2(-1,1)*size/2, OFFSET+Vector2(1,1)*size/2, OFFSET-Vector2(1,-1)*size/2 ], color,[],null,null,false) @@ -69,6 +68,12 @@ func _on_Point_mouse_exited(): emit_signal("_mouse_exited") update() +func _on_Point_gui_input(event): + if event is InputEventMouseButton: + if event.is_pressed(): + if event.button_index == 1: + emit_signal("_point_pressed",self) + func format_value(v : Array, format_x : bool, format_y : bool): var x : String = str(v[0]) var y : String = str(v[1]) @@ -92,33 +97,27 @@ func format(n): return s.replace("Null","") - -func _on_Point_gui_input(event): - if event is InputEventMouseButton: - if event.is_pressed(): - if event.button_index == 1: - emit_signal("_point_pressed",self) - func set_value( v : Array = [] ) : point_value = v -func get_value() -> Array: - return point_value - func set_color_point( c : Color ): color = c -func get_color_point() -> Color: - return color - func set_function( f : String ): function = f -func get_function() -> String: - return function - func set_shape(s : int): shape = s +# Public Getters +func get_value() -> Array: + return point_value + +func get_color_point() -> Color: + return color + +func get_function() -> String: + return function + func get_shape() -> int: return shape diff --git a/addons/easy_charts/Utilities/Scripts/Chart.gd b/addons/easy_charts/Utilities/Scripts/Chart.gd index b9cc393..07a2fdf 100644 --- a/addons/easy_charts/Utilities/Scripts/Chart.gd +++ b/addons/easy_charts/Utilities/Scripts/Chart.gd @@ -1,5 +1,537 @@ extends Control class_name Chart -enum point_shapes { Dot, Triangle, Square, Cross } -enum templates_names { Default, Clean, Gradient, Minimal, Invert } +# Signals .................................. +signal chart_plotted(chart) +signal point_pressed(point) + +# Onready Vars ............................ +onready var PointData = $PointData/PointData +onready var Points = $Points +onready var Legend = $Legend + +# Scenes and Reosurces ...................... +var point_node : PackedScene = preload("../Point/Point.tscn") +var FunctionLegend : PackedScene = preload("../Legend/FunctionLegend.tscn") + +# Enums ..................................... +enum PointShapes { Dot, Triangle, Square, Cross } +enum TemplatesNames { Default, Clean, Gradient, Minimal, Invert } + +# Shared Variables ......................... +var SIZE : Vector2 = Vector2() +var OFFSET : Vector2 = Vector2(0,0) +var origin : Vector2 + +var font_size : float = 16 +var const_height : float = font_size/2*font_size/20 +var const_width : float = font_size/2 + +# actual distance between x and y values +var x_pass : float +var y_pass : float + +# vertical distance between y consecutive points used for intervals +var v_dist : float +var h_dist : float + +# define values on x an y axis +var x_chors : Array +var y_chors : Array + +# actual coordinates of points (in pixel) +var x_coordinates : Array +var y_coordinates : Array + +# datas contained in file +var datas : Array + +# amount of functions to represent +var functions : int = 0 + +# database values +var x_datas : Array +var y_datas : Array + +# labels displayed on chart +var x_label : String + +var x_labels : Array +var y_labels : Array + +var x_margin_min : int = 0 +var y_margin_min : int = 0 + +# actual values of point, from the database +var point_values : Array + +# actual position of points in pixel +var point_positions : Array + +var legend : Array setget set_legend,get_legend + +var templates : Dictionary = {} + +# ................... Export Shared Variables .................. +export (String, FILE, "*.txt, *.csv") var source : String = "" setget set_source +export (String) var delimiter : String = ";" setget set_delimiter + +var origin_at_zero : bool = true setget set_origin_at_zero#, get_origin_at_zero +var are_values_columns : bool = false setget set_are_values_columns#, get_are_values_columns +var show_x_values_as_labels : bool = true setget set_show_x_values_as_labels#, get_show_x_values_as_labels +var labels_index : int = 0 setget set_labels_index#, get_labels_index +var function_names_index : int = 0 setget set_function_names_index#, get_function_names_index + +# for radar +var use_height_as_radius : bool = false setget set_use_height_as_radius +var radius : float = 150.0 setget _set_radius + +# for columns +var column_width : float = 10 setget set_column_width +var column_gap : float = 2 setget set_column_gap + +var full_scale : float = 1.0 setget set_full_scale +var x_decim : float = 5.0 setget set_x_decim +var y_decim : float = 5.0 setget set_y_decim + +var points_shape : Array = [PointShapes.Dot] setget set_points_shape +var function_colors = [Color("#1e1e1e")] setget set_function_colors +var outline_color : Color = Color("#1e1e1e") setget set_outline_color +var box_color : Color = Color("#1e1e1e") setget set_box_color +var v_lines_color : Color = Color("#cacaca") setget set_v_lines_color +var h_lines_color : Color = Color("#cacaca") setget set_h_lines_color +var grid_color : Color = Color("#1e1e1e") setget set_grid_color +var font : Font setget set_font +var bold_font : Font setget set_bold_font +var font_color : Color = Color("#1e1e1e") setget set_font_color + +var template : int = TemplatesNames.Default setget set_template + +# modifiers +var rotation : float = 0 setget set_rotation +var invert_chart : bool = false setget set_invert_chart + +# .......................... Properties Manager .................................... +func _get(property): + match property: + "Chart_Properties/origin_at_zero": + return origin_at_zero + "Chart_Properties/are_values_columns": + return are_values_columns + "Chart_Properties/show_x_values_as_labels": + return show_x_values_as_labels + "Chart_Properties/labels_index": + return labels_index + "Chart_Properties/function_names_index": + return function_names_index + "Chart_Properties/use_height_as_radius": + return use_height_as_radius + "Chart_Properties/radius": + return radius + "Chart_Properties/column_width": + return column_width + "Chart_Properties/column_gap": + return column_gap + + "Chart_Display/full_scale": + return full_scale + "Chart_Display/x_decim": + return x_decim + "Chart_Display/y_decim": + return y_decim + + "Chart_Style/points_shape": + return points_shape + "Chart_Style/function_colors": + return function_colors + "Chart_Style/template": + return template + "Chart_Style/outline_color": + return outline_color + "Chart_Style/grid_color": + return grid_color + "Chart_Style/box_color": + return box_color + "Chart_Style/v_lines_color": + return v_lines_color + "Chart_Style/h_lines_color": + return h_lines_color + "Chart_Style/font": + return font + "Chart_Style/bold_font": + return bold_font + "Chart_Style/font_color": + return font_color + + "Chart_Modifiers/rotation": + return rotation + "Chart_Modifiers/invert_chart": + return invert_chart + +func _set(property, value): + match property: + "Chart_Properties/origin_at_zero": + origin_at_zero = value + return true + "Chart_Properties/are_values_columns": + are_values_columns = value + return true + "Chart_Properties/show_x_values_as_labels": + show_x_values_as_labels = value + return true + "Chart_Properties/labels_index": + labels_index = value + return true + "Chart_Properties/function_names_index": + function_names_index = value + return true + "Chart_Properties/use_height_as_radius": + use_height_as_radius = value + return true + "Chart_Properties/radius": + radius = value + return true + "Chart_Properties/column_width": + column_width = value + return true + "Chart_Properties/column_gap": + column_width = value + return true + + "Chart_Display/full_scale": + full_scale = value + return true + "Chart_Display/x_decim": + x_decim = value + return true + "Chart_Display/y_decim": + y_decim = value + return true + + "Chart_Style/points_shape": + points_shape = value + return true + "Chart_Style/function_colors": + function_colors = value + return true + "Chart_Style/template": + template = value + apply_template(template) + return true + "Chart_Style/outline_color": + outline_color = value + return true + "Chart_Style/grid_color": + grid_color = value + return true + "Chart_Style/box_color": + box_color = value + return true + "Chart_Style/v_lines_color": + v_lines_color = value + return true + "Chart_Style/h_lines_color": + h_lines_color = value + return true + "Chart_Style/font": + font = value + return true + "Chart_Style/bold_font": + bold_font = value + return true + "Chart_Style/font_color": + font_color = value + apply_template(template) + return true + + "Chart_Modifiers/rotation": + rotation = value + return true + "Chart_Modifiers/invert_chart": + invert_chart = value + return true + +# .......................... Shared Functions and virtuals ........................ + +#func _plot(source_ : String, delimiter_ : String, are_values_columns_ : bool, x_values_index_ : int, invert_chart_ : bool = false): +# randomize() +# +# load_font() +# PointData.hide() +# +# datas = read_datas(source_,delimiter_) +# structure_datas(datas,are_values_columns_,x_values_index_) +# build_chart() +# count_functions() +# calculate_pass() +# calculate_coordinates() +# calculate_colors() +# set_shapes() +# create_legend() +# emit_signal("chart_plotted") + +func _ready(): + templates = Utilities._load_templates() + +func plot(): + load_font() + PointData.hide() + + if source == "" or source == null: + Utilities._print_message("Can't plot a chart without a Source file. Please, choose it in editor, or use the custom function _plot().",1) + return + + datas = read_datas(source,delimiter) + structure_datas(datas,are_values_columns,labels_index) + build_chart() + count_functions() + calculate_pass() + calculate_coordinates() + calculate_colors() + set_shapes() + create_legend() + emit_signal("chart_plotted",self) + +func load_font(): + if font != null: + font_size = font.get_height() + var theme : Theme = Theme.new() + theme.set_default_font(font) + PointData.set_theme(theme) + else: + var lbl = Label.new() + font = lbl.get_font("") + lbl.free() + if bold_font != null: + PointData.Data.set("custom_fonts/font",bold_font) + +func calculate_colors(): + if function_colors.empty() or function_colors.size() < functions: + for function in functions: + function_colors.append(Color("#1e1e1e")) + +func set_shapes(): + if points_shape.empty() or points_shape.size() < functions: + for function in functions: + points_shape.append(PointShapes.Dot) + +func read_datas(source : String, delimiter : String): + var file : File = File.new() + file.open(source,File.READ) + var content : Array + while not file.eof_reached(): + var line : PoolStringArray = file.get_csv_line(delimiter) + content.append(line) + file.close() + for data in content: + if data.size() < 2 or data.empty(): + content.erase(data) + return content + +func count_functions(): + if are_values_columns: + if not invert_chart: + functions = datas[0].size()-1 + else: + functions = datas.size()-1 + else: + if invert_chart: + functions = datas[0].size()-1 + else: + functions = datas.size()-1 + +func clear_points(): + if $Points.get_children(): + for function in Points.get_children(): + function.queue_free() + for legend in $Legend.get_children(): + legend.queue_free() + +func redraw(): + build_chart() + calculate_pass() + calculate_coordinates() + update() + +# .................. VIRTUAL FUNCTIONS ......................... +func calculate_pass(): + pass + +func calculate_coordinates(): + pass + +func structure_datas(database : Array, are_values_columns : bool, labels_index : int): + pass + +func build_chart(): + pass + +func function_colors(): + pass + +func create_legend(): + pass + +# ........................... Shared Setters & Getters .............................. +func apply_template(template_name : int): + if Engine.editor_hint: + set_template(template_name) + property_list_changed_notify() + +# !!! API v2 +func set_source(source_file : String, delim : String = ";"): + source = source_file + delimiter = delim + +# !!! API v2 +func set_indexes(lb : int = 0, function_names : int = 0): + labels_index = lb + function_names_index = function_names + +# !!! API v2 +func set_radius(use_height : bool = false, f : float = 0): + use_height_as_radius = use_height + radius = f + +# !!! API v2 +func set_chart_colors(f_colors : Array, o_color : Color, b_color : Color, g_color : Color, h_lines : Color, v_lines : Color): + function_colors = f_colors + outline_color = o_color + box_color = b_color + grid_color = g_color + h_lines_color = h_lines + v_lines_color = v_lines + +# !!! API v2 +func set_chart_fonts(normal_font : Font, bold_font : Font, f_color : Color): + font = normal_font + self.bold_font = bold_font + font_color = f_color + +func set_delimiter(d : String): + d = delimiter + +# ! API +func set_origin_at_zero(b : bool): + origin_at_zero = b + +# ! API +func set_are_values_columns(b : bool): + are_values_columns = b + +# ! API +func set_show_x_values_as_labels(b : bool): + show_x_values_as_labels = b + +func set_labels_index(i : int): + labels_index = i + +func set_function_names_index(i : int): + function_names_index = i + +func set_use_height_as_radius(b : bool): + use_height_as_radius = b + +func _set_radius(r : float): + radius = r + +# ! API +func set_column_width(f : float): + column_width = f + +# ! API +func set_column_gap(f : float): + column_gap = f + +# ! API +func set_full_scale(f : float): + full_scale = f + +# ! API +func set_x_decim(f : float): + x_decim = f + +# ! API +func set_y_decim(f : float): + y_decim = f + +# ! API +func set_points_shape(a : Array): + points_shape = a + + +# ! API +func set_function_colors(a : Array): + function_colors = a + +# ! API +func set_outline_color(c : Color): + outline_color = c + +# ! API +func set_box_color(c : Color): + box_color = c + +# ! API +func set_grid_color(c : Color): + grid_color = c + +# ! API +func set_v_lines_color(c : Color): + v_lines_color = c + +# ! API +func set_h_lines_color(c : Color): + h_lines_color = c + +# ! API +func set_font(f : Font): + font = f + +# ! API +func set_bold_font(f : Font): + bold_font = f + +# ! API +func set_font_color(c : Color): + font_color = c + +# ! API +func set_template(template_name : int): + template = template_name + templates = Utilities.templates + if template_name!=null: + var custom_template = templates.get(templates.keys()[template_name]) + function_colors = custom_template.function_colors as PoolColorArray + outline_color = Color(custom_template.outline_color) + box_color = Color(custom_template.outline_color) + grid_color = Color(custom_template.v_lines_color) + v_lines_color = Color(custom_template.v_lines_color) + h_lines_color = Color(custom_template.h_lines_color) + box_color = Color(custom_template.outline_color) + font_color = Color(custom_template.font_color) + +# ! API +func set_rotation(f : float): + rotation = f + +# ! API +func set_invert_chart(b : bool): + invert_chart = b + +func set_legend(l : Array): + legend = l + +func get_legend(): + return legend + +# ............................. Shared Signals .............................. +func point_pressed(point : Point): + emit_signal("point_pressed",point) + +func show_data(point : Point): + PointData.update_datas(point) + PointData.show() + +func hide_data(): + PointData.hide() diff --git a/addons/easy_charts/Utilities/Scripts/Chart2D.gd b/addons/easy_charts/Utilities/Scripts/Chart2D.gd index a4c3f59..448a5fa 100644 --- a/addons/easy_charts/Utilities/Scripts/Chart2D.gd +++ b/addons/easy_charts/Utilities/Scripts/Chart2D.gd @@ -1,8 +1,8 @@ extends Node2D class_name Chart2D -enum point_shapes { Dot, Triangle, Square, Cross } -enum templates_names { Default, Clean, Gradient, Minimal, Invert } +enum PointShapes { Dot, Triangle, Square, Cross } +enum TemplatesNames { Default, Clean, Gradient, Minimal, Invert } # Called when the node enters the scene tree for the first time. func _ready(): diff --git a/addons/easy_charts/Utilities/Scripts/ChartObject.gd b/addons/easy_charts/Utilities/Scripts/ChartObject.gd new file mode 100644 index 0000000..6b92f74 --- /dev/null +++ b/addons/easy_charts/Utilities/Scripts/ChartObject.gd @@ -0,0 +1,39 @@ +#extends Object +#class_name ChartObject +# +#""" +#[ChartObject] :: Class +# +#this class is used to store all the functions that Chart, Chart2D and Chart3D custom instances +#will share in-between. +#Chart classes will extend this class. +#""" +# +#enum PointShapes { Dot, Triangle, Square, Cross } +#enum TemplatesNames { Default, Clean, Gradient, Minimal, Invert } +# +#class Chart extends Control: +# var CHART_TYPE : String = "Chart" +# enum PointShapes { Dot, Triangle, Square, Cross } +# enum TemplatesNames { Default, Clean, Gradient, Minimal, Invert } +# +# export (PoolColorArray) var function_colors = [Color("#1e1e1e")] +# export (Array, PointShapes) var points_shape : Array = [PointShapes.Dot] +# +# var functions : int = 0 +# +# func calculate_colors(): +# if function_colors.empty() or function_colors.size() < functions: +# for function in functions: +# function_colors.append(Color("#1e1e1e")) +# +# func set_shapes(): +# if points_shape.empty() or points_shape.size() < functions: +# for function in functions: +# points_shape.append(PointShapes.Dot) +# +# +#class Chart2D extends Node2D: +# var CHART_TYPE : String = "Chart2D" +# enum PointShapes { Dot, Triangle, Square, Cross } +# enum TemplatesNames { Default, Clean, Gradient, Minimal, Invert } diff --git a/addons/easy_charts/Utilities/Scripts/utilities.gd b/addons/easy_charts/Utilities/Scripts/utilities.gd index 3323bd9..7ac7a48 100644 --- a/addons/easy_charts/Utilities/Scripts/utilities.gd +++ b/addons/easy_charts/Utilities/Scripts/utilities.gd @@ -21,3 +21,6 @@ func _load_templates() -> Dictionary: var templates = JSON.parse(template_file.get_as_text()).get_result() template_file.close() return templates + +func get_template(template_index : int): + return templates.get(templates.keys()[template_index]) diff --git a/addons/easy_charts/plugin.cfg b/addons/easy_charts/plugin.cfg index e6e689e..81f765c 100644 --- a/addons/easy_charts/plugin.cfg +++ b/addons/easy_charts/plugin.cfg @@ -3,5 +3,5 @@ name="EasyCharts" description="" author="Nicolò \"fenix\" Santilio" -version="0.2.1" +version="0.2.8" script="plugin.gd" diff --git a/addons/easy_charts/plugin.gd b/addons/easy_charts/plugin.gd index f4e3b47..09bc947 100644 --- a/addons/easy_charts/plugin.gd +++ b/addons/easy_charts/plugin.gd @@ -3,10 +3,12 @@ extends EditorPlugin func _enter_tree(): add_autoload_singleton("Utilities","res://addons/easy_charts/Utilities/Scripts/utilities.gd") +# add_custom_type("ChartObject","Node", load("res://addons/easy_charts/Utilities/Scripts/ChartObject.gd"), preload("Utilities/icons/linechart.svg")) add_custom_type("Chart","Control", load("res://addons/easy_charts/Utilities/Scripts/Chart.gd"), preload("Utilities/icons/linechart.svg")) add_custom_type("Chart2D","Node2D", preload("Utilities/Scripts/Chart2D.gd"), preload("Utilities/icons/linechart2d.svg")) func _exit_tree(): remove_custom_type("Chart") remove_custom_type("Chart2D") +# remove_custom_type("ChartObject") remove_autoload_singleton("Utilities")