pandemonium_engine_easy_charts/addons/easy_charts/BarChart/BarChart.gd

415 lines
12 KiB
GDScript3
Raw Normal View History

2020-05-14 02:36:45 +02:00
tool
extends Control
2020-05-18 07:46:26 +02:00
"""
[BarChart] - General purpose node for Bar Charts
A bar chart or bar graph is a chart or graph that presents categorical data with
rectangular bars with heights or lengths proportional to the values that they represent.
The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes
called a column chart.
A bar graph shows comparisons among discrete categories. One axis of the chart shows
the specific categories being compared, and the other axis represents a measured value.
Some bar graphs present bars clustered in groups of more than one, showing the
values of more than one measured variable.
/ source : Wikipedia /
"""
2020-05-14 02:36:45 +02:00
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
2020-05-18 07:46:26 +02:00
var OFFSET : Vector2 = Vector2(0,0)
2020-05-14 02:36:45 +02:00
#-------------------------------------------------------------------------#
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
# quantization, representing the interval in which values will be displayed
var x_decim : float = 1.0
# 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 y_labels : Array
# 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
# ---------------------
2020-05-18 07:46:26 +02:00
#export (bool)
2020-05-14 02:36:45 +02:00
var SIZE : Vector2 = Vector2()
2020-05-18 07:46:26 +02:00
export (String, FILE) var source : String = ""
export (String) var delimiter : String = ";"
export (bool) var are_values_columns : bool = false
export (bool) var invert_xy : bool = false
export (int,0,100) var x_values : int = 0
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,10) var y_decim : float = 5.0
2020-05-14 02:36:45 +02:00
export (PoolColorArray) var function_colors = [Color("#1e1e1e")]
2020-05-18 07:46:26 +02:00
export (bool) var boxed : bool = true
2020-05-14 02:36:45 +02:00
export (Color) var v_lines_color : Color = Color("#cacaca")
export (Color) var h_lines_color : Color = Color("#cacaca")
export (Color) var outline_color : Color = Color("#1e1e1e")
export (Font) var font : Font
export (Font) var bold_font : Font
export (Color) var font_color : Color = Color("#1e1e1e")
2020-05-18 07:46:26 +02:00
export (String,"Default","Clean","Gradient","Minimal","Invert") var template : String = "Default" setget apply_template
var templates : Dictionary = {}
signal chart_plotted(chart)
signal point_pressed(point)
func _point_drawn():
pass
func _ready():
pass
2020-05-14 02:36:45 +02:00
2020-05-18 07:46:26 +02:00
func _script_changed():
_enter_tree()
_ready()
2020-05-14 02:36:45 +02:00
2020-05-18 07:46:26 +02:00
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_ : int):
2020-05-14 02:36:45 +02:00
randomize()
load_font()
PointData.hide()
datas = read_datas(source_,delimiter_)
count_functions()
structure_datas(datas,are_values_columns_,x_values_)
build_chart()
calculate_pass()
calculate_coordinates()
calculate_colors()
create_legend()
2020-05-18 07:46:26 +02:00
emit_signal("chart_plotted", self)
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)
build_chart()
calculate_pass()
calculate_coordinates()
calculate_colors()
create_legend()
emit_signal("chart_plotted", self)
func _draw():
draw_chart()
2020-05-14 02:36:45 +02:00
func calculate_colors():
if function_colors.empty() or function_colors.size() < functions:
for function in functions:
function_colors.append(Color("#1e1e1e"))
2020-05-18 07:46:26 +02:00
func draw_chart():
clear_points()
draw_outlines()
draw_v_grid()
draw_h_grid()
draw_functions()
func draw_outlines():
if boxed:
draw_line(Vector2(origin.x,0),Vector2(SIZE.x,0),outline_color,1,true)
draw_line(Vector2(SIZE.x,0),Vector2(SIZE.x,origin.y),outline_color,1,true)
draw_line(Vector2(SIZE.x,origin.y),origin,outline_color,1,true)
draw_line(origin,Vector2(origin.x,0),outline_color,1,true)
func draw_v_grid():
for p in x_chors.size():
var point : Vector2 = origin+Vector2((p)*x_pass + OFFSET.x/2,0)
# v grid
draw_line(Vector2(point.x,origin.y),Vector2(point.x,origin.y-5),v_lines_color,0.2,true)
draw_string(font,point+Vector2(-const_width/2*x_chors[p].length() + (column_width/2) * ( y_datas.size() if not invert_xy else y_datas[0].size()+1 ) + column_gap,font_size),x_chors[p],font_color)
func draw_h_grid():
for p in y_chors.size():
var point : Vector2 = origin-Vector2(0,(p)*y_pass)
# h grid
draw_line(point,Vector2(SIZE.x,point.y),h_lines_color,0.2,true)
draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,font_size/2),y_chors[p],font_color)
func draw_functions():
for function in point_positions.size():
draw_function(function,point_positions[function])
func draw_function(f_index : int, function : Array):
for point in function.size():
var pointv : Point
pointv = point_node.instance()
pointv.connect("_mouse_entered",self,"show_data",[pointv])
pointv.connect("_mouse_exited",self,"hide_data")
pointv.connect("_point_pressed",self,"point_pressed")
pointv.create_point(function_colors[f_index], Color.white, function[point]+Vector2(0,5),
pointv.format_value(point_values[f_index][point],false,true),(x_datas[f_index] if invert_xy else y_labels[f_index]))
add_child(pointv)
pointv.rect_size.y = origin.y - function[point].y
draw_line(
Vector2(function[point].x, origin.y),
function[point], function_colors[f_index], column_width, true)
func construct_column(f_index : int, function : Array):
draw_line(Vector2(function[f_index].x,origin.y), function[f_index], function_colors[f_index], column_width, true)
2020-05-14 02:36:45 +02:00
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 : int):
# @x_values 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
self.are_values_columns = 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 == x_values:
x_datas.append(database[row][column])
else:
if row != 0:
t_vals.append(float(database[row][column]))
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 == x_values:
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
var to_order : Array
for cluster in y_datas.size():
# define x_chors and y_chors
var margin = y_datas[cluster][y_datas[cluster].size()-1]
to_order.append(margin)
to_order.sort()
var margin = to_order.pop_back()
v_dist = y_decim * pow(10.0,str(margin).length()-2)
var multi = 0
var p = v_dist*multi
y_chors.append(p as String)
while p < margin:
multi+=1
p = v_dist*multi
y_chors.append(p as String)
func build_chart():
2020-05-18 07:46:26 +02:00
SIZE = get_size()
2020-05-14 02:36:45 +02:00
origin = Vector2(OFFSET.x,SIZE.y-OFFSET.y)
func calculate_pass():
if invert_xy:
x_chors = y_labels as PoolStringArray
else:
x_chors = x_datas as PoolStringArray
# calculate distance in pixel between 2 consecutive values/datas
2020-05-18 07:46:26 +02:00
x_pass = (SIZE.x - OFFSET.x*2 - (column_width) * ( y_datas.size() if not invert_xy else y_datas[0].size()+1 ) - column_gap - column_width/2) / (x_chors.size()-1)
2020-05-14 02:36:45 +02:00
y_pass = origin.y / (y_chors.size()-1)
func calculate_coordinates():
x_coordinates.clear()
y_coordinates.clear()
point_values.clear()
point_positions.clear()
if invert_xy:
for column in y_datas[0].size():
var single_coordinates : Array
for row in y_datas:
single_coordinates.append((row[column]*y_pass)/v_dist)
y_coordinates.append(single_coordinates)
else:
for cluster in y_datas:
var single_coordinates : Array
for value in cluster.size():
single_coordinates.append((cluster[value]*y_pass)/v_dist)
y_coordinates.append(single_coordinates)
for x in x_chors.size():
x_coordinates.append(x_pass*x)
for f in functions:
point_values.append([])
point_positions.append([])
if invert_xy:
2020-05-18 07:46:26 +02:00
for function in y_coordinates.size():
2020-05-14 02:36:45 +02:00
for function_value in y_coordinates[function].size():
2020-05-18 07:46:26 +02:00
point_positions[function].append(Vector2(OFFSET.x/2 + column_width/2 + (column_width + column_gap)*function + x_coordinates[function_value]+origin.x, origin.y-y_coordinates[function][function_value]))
2020-05-14 02:36:45 +02:00
point_values[function].append([x_chors[function_value],y_datas[function_value][function]])
else:
for cluster in y_coordinates.size():
for y in y_coordinates[cluster].size():
point_values[cluster].append([x_chors[y],y_datas[cluster][y]])
2020-05-18 07:46:26 +02:00
point_positions[cluster].append(Vector2(OFFSET.x/2 + column_width/2 + (column_width + column_gap)*cluster + x_coordinates[y]+origin.x, origin.y-y_coordinates[cluster][y]))
2020-05-14 02:36:45 +02:00
func redraw():
2020-05-18 07:46:26 +02:00
pass
2020-05-14 02:36:45 +02:00
2020-05-18 07:46:26 +02:00
func invert_chart():
invert_xy = !invert_xy
count_functions()
redraw()
create_legend()
func count_functions():
if are_values_columns:
if not invert_xy:
functions = datas[0].size()-1
else:
functions = datas.size()-1
else:
2020-05-14 02:36:45 +02:00
if invert_xy:
2020-05-18 07:46:26 +02:00
functions = datas[0].size()-1
2020-05-14 02:36:45 +02:00
else:
2020-05-18 07:46:26 +02:00
functions = datas.size()-1
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()
2020-05-14 02:36:45 +02:00
func create_legend():
legend.clear()
for function in functions:
var function_legend = FunctionLegend.instance()
var f_name : String
if invert_xy:
f_name = x_datas[function]
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)
func set_legend(l : Array):
legend = l
2020-05-18 07:46:26 +02:00
func get_legend() -> Array:
2020-05-14 02:36:45 +02:00
return legend
func apply_template(template_name : String):
if Engine.editor_hint:
if template_name!=null and template_name!="":
template = template_name
2020-05-18 07:46:26 +02:00
var custom_template = templates[template_name.to_lower()]
function_colors = custom_template.function_colors
2020-05-14 02:36:45 +02:00
v_lines_color = Color(custom_template.v_lines_color)
h_lines_color = Color(custom_template.h_lines_color)
outline_color = Color(custom_template.outline_color)
font_color = Color(custom_template.font_color)
property_list_changed_notify()
2020-05-18 07:46:26 +02:00
func point_pressed(point : Point):
emit_signal("point_pressed",point)
func _enter_tree():
templates = Utilities._load_templates()