Merge pull request #20 from fenix-hub/dev

Update 0.4.1
This commit is contained in:
Nicolò Santilio 2020-11-06 19:35:15 +01:00 committed by GitHub
commit c66fa0c985
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 1502 additions and 1032 deletions

View File

@ -9,7 +9,7 @@
A library of Charts plotted in Control, 2D and 3D nodes to visualize general purpose datasets.
Author: *"Nicolo (fenix) Santilio"*
Version: *0.3.1*
Version: *0.4.1*
Wiki: *[wip]*
Godot Version: *3.2stable*

View File

@ -82,10 +82,10 @@ __meta__ = {
[node name="PointData" parent="PointData" index="0"]
visible = false
margin_left = -154.32
margin_top = -115.171
margin_right = -154.456
margin_bottom = -114.371
margin_left = -223.769
margin_top = -45.5654
margin_right = -223.906
margin_bottom = -44.7655
theme = SubResource( 1 )
[editable path="PointData"]

View File

@ -0,0 +1,62 @@
[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]
[node name="PieChart" type="Control"]
anchor_right = 1.0
anchor_bottom = 0.993333
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": true
}
Chart_Properties/are_values_columns = false
Chart_Properties/labels_index = 0
Chart_Properties/show_x_values_as_labels = true
Chart_Style/function_colors = PoolColorArray( 0.117647, 0.117647, 0.117647, 1, 0.117647, 0.117647, 0.117647, 1, 0.117647, 0.117647, 0.117647, 1, 0.117647, 0.117647, 0.117647, 1 )
Chart_Style/font = null
Chart_Style/bold_font = null
Chart_Style/font_color = Color( 0.117647, 0.117647, 0.117647, 1 )
Chart_Style/template = 0
Chart_Modifiers/rotation = 0.0
[node name="Background" type="ColorRect" parent="."]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
[node name="Points" type="Control" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Legend" type="HBoxContainer" parent="."]
visible = false
anchor_right = 1.0
anchor_bottom = 0.106667
__meta__ = {
"_edit_use_anchors_": true
}
[node name="ChartName" type="Label" parent="."]
use_parent_material = true
anchor_right = 1.0
anchor_bottom = 0.0233333
align = 1
valign = 1
__meta__ = {
"_edit_use_anchors_": true
}
[node name="PointData" parent="." instance=ExtResource( 2 )]
[node name="PointData" parent="PointData" index="0"]
visible = false
margin_left = -148.407
margin_top = -156.174
margin_right = -148.543
margin_bottom = -155.375
[editable path="PointData"]

View File

@ -0,0 +1,219 @@
tool
extends Chart
var should_draw : bool = false
var area_angles : Array
var slices : Array
var areas : Array
var areas_interacted : Array
class CustomSorter:
static func sort_ascending(a,b):
if a[1] < b[1]:
return true
return false
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 Style
{
"hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/function_colors",
"type": TYPE_COLOR_ARRAY
},
{
"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_RANGE,
"hint_string": "0,360",
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Modifiers/rotation",
"type": TYPE_REAL
},
]
# Called when the node enters the scene tree for the first time.
func _ready():
pass
func plot_placeholder() -> void:
datas = [
["United States",46],
["Great Britain",27],
["China",26],
["Russia",19],
["Germany",17]
]
function_colors = [
Color.red,
Color.white,
Color.yellow,
Color.green,
Color.blue
]
plot_from_array(datas)
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
clean_variables()
self.are_values_columns = are_values_columns
if are_values_columns:
for row in database.size():
var t_vals: Array
for column in database[row].size():
if column == x_values_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())
else:
for row in database.size():
if row == x_values_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
func build_chart():
SIZE = get_size()
origin = SIZE/2
radius = (SIZE.y/2 - 20) if SIZE.y < SIZE.x else (SIZE.x/2 - 20)
func calculate_pass():
var tot : float
for y_data in y_datas: tot+=y_data[0]
x_pass = 360/tot
func calculate_coordinates():
area_angles.clear()
slices.clear()
areas.clear()
var from : float = 0.0
var to : float = y_datas[0][0]*x_pass
area_angles.append([from,to])
for info in range(y_datas.size()):
slices.append(Slice.new(y_labels[info], str(y_datas[info][0]), from, to, x_label+" : "+x_datas[0], function_colors[info]))
areas.append(calculate_circle_arc_polygon(origin, radius, from + rotation, to + rotation, function_colors[info]))
from = to
to = (y_datas[info+1][0]*x_pass + from) if info < y_datas.size()-1 else (360)
area_angles.append([from, to])
create_legend()
func calculate_circle_arc_polygon(center : Vector2, radius : float, angle_from : float, angle_to : float, color : Color) -> PoolVector2Array:
var nb_points : int = 32
var points_arc : PoolVector2Array = PoolVector2Array()
points_arc.push_back(center)
var colors : PoolColorArray = PoolColorArray([color])
for i in range(nb_points + 1):
var angle_point = deg2rad(angle_from + i * (angle_to - angle_from) / nb_points - 90)
points_arc.push_back(center + Vector2(cos(angle_point), sin(angle_point)) * radius)
return points_arc
func _draw():
_draw_areas()
if mouse_on_slice:
_draw_arc(area_angles[mouse_on_area], mouse_on_area)
mouse_on_slice = false
func _draw_arc(arc : Array, index : int):
var temp_color : Color = function_colors[index]
temp_color.a = 0.7
draw_arc(origin, radius + 6, deg2rad(arc[0]-90 + rotation), deg2rad(arc[1]-90 + rotation), 32, temp_color, 4)
func _draw_areas():
for area_idx in range(areas.size()):
draw_polygon(areas[area_idx], [function_colors[area_idx]])
var mouse_on_area : int
var mouse_on_slice : bool = false
func _input(event):
if event is InputEventMouseMotion:
for area_idx in range(areas.size()):
if Geometry.is_point_in_polygon(event.global_position, areas[area_idx]):
mouse_on_slice = true
mouse_on_area = area_idx
show_slice_data(slices[area_idx])
update()
if not mouse_on_slice:
mouse_on_area = -1
mouse_on_slice = false
hide_data()
update()

View File

@ -27,7 +27,7 @@ Chart_Properties/show_x_values_as_labels = true
Chart_Display/x_decim = 5.0
Chart_Display/y_decim = 5.0
Chart_Style/points_shape = [ 0 ]
Chart_Style/function_colors = [ "#1e1e1e", "#1e1e1e", "#1e1e1e", "#1e1e1e" ]
Chart_Style/function_colors = PoolColorArray( 0.117647, 0.117647, 0.117647, 1, 0.117647, 0.117647, 0.117647, 1, 0.117647, 0.117647, 0.117647, 1, 0.117647, 0.117647, 0.117647, 1 )
Chart_Style/box_color = Color( 0.117647, 0.117647, 0.117647, 1 )
Chart_Style/v_lines_color = Color( 0.792157, 0.792157, 0.792157, 1 )
Chart_Style/h_lines_color = Color( 0.792157, 0.792157, 0.792157, 1 )
@ -76,10 +76,10 @@ __meta__ = {
[node name="PointData" parent="PointData" index="0"]
visible = false
margin_left = 252.182
margin_top = 37.3706
margin_right = 252.046
margin_bottom = 38.1706
margin_left = -19.0
margin_top = -63.0
margin_right = -19.136
margin_bottom = -62.2
theme = SubResource( 1 )
[editable path="PointData"]

View File

@ -13,7 +13,7 @@ onready var Value : Label = $PointData/Value/y
onready var Function : Label = $PointData/Function
func _ready():
pass
hide()
func _process(delta):
if get_global_mouse_position().y > OFFSET.y + GAP.y:
@ -42,6 +42,27 @@ func update_datas(point : Control):
update()
show()
func update_slice_datas(slice : Slice):
update_size()
get("custom_styles/panel").set("bg_color",slice.color)
var font_color : Color
if slice.color.g < 0.75:
font_color = Color(1,1,1,1)
else:
font_color = Color(0,0,0,1)
Data.set("custom_colors/font_color",font_color)
Value.set("custom_colors/font_color",font_color)
Function.set("custom_colors/font_color",font_color)
get("custom_styles/panel").set("border_color",font_color)
Data.set_text(slice.x_value+":")
Value.set_text(slice.y_value)
Function.set_text(slice.function)
update()
show()
func update_size():
OFFSET.x = get_size().x/2
OFFSET.y = get_size().y

View File

@ -24,8 +24,10 @@ corner_detail = 20
[node name="PointData" type="PanelContainer" parent="."]
anchor_right = 0.0694688
anchor_bottom = 0.067
margin_right = -0.136002
margin_bottom = 0.799999
margin_left = -256.805
margin_top = -36.1267
margin_right = -256.941
margin_bottom = -35.3267
grow_horizontal = 2
mouse_filter = 2
custom_styles/panel = SubResource( 1 )

View File

@ -9,6 +9,7 @@ signal point_pressed(point)
onready var PointData = $PointData/PointData
onready var Points = $Points
onready var Legend = $Legend
onready var ChartName : Label = $ChartName
# Scenes and Reosurces ......................
var point_node : PackedScene = preload("../Point/Point.tscn")
@ -256,25 +257,6 @@ func _ready():
templates = Utilities._load_templates()
# .......................... 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 plot():
load_font()
PointData.hide()
@ -284,7 +266,27 @@ func plot():
return
datas = read_datas(source)
structure_datas(datas,are_values_columns,labels_index)
structure_datas(datas.duplicate(true),are_values_columns,labels_index)
build_chart()
count_functions()
calculate_pass()
calculate_coordinates()
calculate_colors()
set_shapes()
create_legend()
emit_signal("chart_plotted",self)
connect("item_rect_changed", self, "redraw")
func plot_from_csv(csv_file : String, _delimiter : String = delimiter):
load_font()
PointData.hide()
if csv_file == "" or csv_file == 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(csv_file, _delimiter)
structure_datas(datas.duplicate(true),are_values_columns,labels_index)
build_chart()
count_functions()
calculate_pass()
@ -294,12 +296,51 @@ func plot():
create_legend()
emit_signal("chart_plotted",self)
func plot_from_array(array : Array) -> void:
load_font()
PointData.hide()
if array.empty():
Utilities._print_message("Can't plot a chart without an empty Array.",1)
return
datas = array
structure_datas(datas.duplicate(true),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 update_plot_data(array : Array) -> void:
if array.empty():
Utilities._print_message("Can't plot a chart without an empty Array.",1)
return
datas.append(array)
structure_datas(datas.duplicate(true),are_values_columns,labels_index)
redraw()
count_functions()
calculate_colors()
set_shapes()
create_legend()
emit_signal("chart_plotted",self)
update()
func plot_placeholder() -> void:
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)
set_theme(theme)
else:
var lbl = Label.new()
font = lbl.get_font("")
@ -308,21 +349,20 @@ func load_font():
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"))
if function_colors.size() <= functions:
for function in range(functions - function_colors.size() + 1): function_colors.append(Color(randf(),randf(), randf()))
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):
func read_datas(source : String, _delimiter : String = delimiter):
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)
var line : PoolStringArray = file.get_csv_line(_delimiter)
content.append(line)
file.close()
for data in content:
@ -355,24 +395,41 @@ func redraw():
calculate_coordinates()
update()
func clean_variables():
x_datas.clear()
y_datas.clear()
x_label = ""
x_labels.clear()
y_labels.clear()
# .................. 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 calculate_pass():
pass
func calculate_coordinates():
pass
func function_colors():
pass
func create_legend():
pass
legend.clear()
for function in functions:
var function_legend = FunctionLegend.instance()
var f_name : String = 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)
# ........................... Shared Setters & Getters ..............................
func apply_template(template_name : int):
@ -409,7 +466,7 @@ func set_chart_colors(f_colors : Array, o_color : Color, b_color : Color, g_colo
v_lines_color = v_lines
# !!! API v2
func set_chart_fonts(normal_font : Font, bold_font : Font, f_color : Color):
func set_chart_fonts(normal_font : Font, bold_font : Font, f_color : Color = Color.white):
font = normal_font
self.bold_font = bold_font
font_color = f_color
@ -542,3 +599,7 @@ func show_data(point : Point):
func hide_data():
PointData.hide()
func show_slice_data(slice : Slice):
PointData.update_slice_datas(slice)
PointData.show()

View File

@ -0,0 +1,17 @@
extends Reference
class_name Slice
var x_value : String
var y_value : String
var from_angle : float
var to_angle : float
var function : String
var color : Color
func _init(x : String, y : String, from : float, to : float, fun : String, col : Color):
x_value = x
y_value = y
from_angle = from
to_angle = to
function = fun
color = col

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/2_columns.csv"
[params]

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/datas_on_columns.csv"
[params]

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/datas_on_rows.csv"
[params]

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/gei_jam#2columns.csv"
[params]

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/godot_engine_italia_jam#2.csv"
[params]

View File

@ -0,0 +1,8 @@
Country,Votes
United States,46
Great Britain,27
China,26
Russia,19
Germany,17
Italy,34
Sweden,83
1 Country Votes
2 United States 46
3 Great Britain 27
4 China 26
5 Russia 19
6 Germany 17
7 Italy 34
8 Sweden 83

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/pie.csv"
[params]

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/radar.csv"
[params]

View File

@ -0,0 +1,10 @@
[remap]
importer="csv"
type="TextFile"
[deps]
source_file="res://addons/easy_charts/file.samples/scatter.csv"
[params]

View File

@ -3,5 +3,5 @@
name="EasyCharts"
description=""
author="Nicolò \"fenix\" Santilio"
version="0.3.1"
version="0.4.1"
script="plugin.gd"