refactor: apply GDScript style guide to line_chart.gd

This commit is contained in:
eddex 2020-10-05 18:12:23 +02:00
parent 836efde1b6
commit b871022ccb

View File

@ -8,14 +8,12 @@ extends Chart
# It is a basic type of chart common in many fields. It is similar to a scatter plot # It is a basic type of chart common in many fields. It is similar to a scatter plot
# except that the measurement points are ordered (typically by their x-axis value) # except that the measurement points are ordered (typically by their x-axis value)
# and joined with straight line segments. # and joined with straight line segments.
# A line chart is often used to visualize a trend in data over intervals of time # A line chart is often used to visualize a trend in data over intervals of time -
# a time series thus the line is often drawn chronologically. # a time series - thus the line is often drawn chronologically.
# In these cases they are known as run charts. # In these cases they are known as run charts.
# Source: Wikipedia # Source: Wikipedia
# ---------------------
func _get_property_list(): func _get_property_list():
return [ return [
# Chart Properties # Chart Properties
@ -58,8 +56,11 @@ func _get_property_list():
# Chart Style # Chart Style
{ {
"hint": 24, "hint": 24,
"hint_string": "%d/%d:%s"%[TYPE_INT, PROPERTY_HINT_ENUM, "hint_string":
PoolStringArray(PointShapes.keys()).join(",")], (
"%d/%d:%s"
% [TYPE_INT, PROPERTY_HINT_ENUM, PoolStringArray(PointShapes.keys()).join(",")]
),
"name": "Chart_Style/points_shape", "name": "Chart_Style/points_shape",
"type": TYPE_ARRAY, "type": TYPE_ARRAY,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE
@ -127,12 +128,11 @@ func _get_property_list():
}, },
] ]
func structure_datas(database: Array, are_values_columns: bool, x_values_index: int): 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 # @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
self.are_values_columns = are_values_columns self.are_values_columns = are_values_columns
match are_values_columns: if are_values_columns:
true:
for row in database.size(): for row in database.size():
var t_vals: Array var t_vals: Array
for column in database[row].size(): for column in database[row].size():
@ -154,7 +154,7 @@ func structure_datas(database : Array, are_values_columns : bool, x_values_index
if not t_vals.empty(): if not t_vals.empty():
y_datas.append(t_vals) y_datas.append(t_vals)
x_label = str(x_datas.pop_front()) x_label = str(x_datas.pop_front())
false: else:
for row in database.size(): for row in database.size():
if row == x_values_index: if row == x_values_index:
x_datas = (database[row]) x_datas = (database[row])
@ -212,10 +212,12 @@ func structure_datas(database : Array, are_values_columns : bool, x_values_index
p = (h_dist * multi) + ((x_margin_min) if not origin_at_zero else 0) p = (h_dist * multi) + ((x_margin_min) if not origin_at_zero else 0)
x_labels.append(p as String) x_labels.append(p as String)
func build_chart(): func build_chart():
SIZE = get_size() SIZE = get_size()
origin = Vector2(OFFSET.x, SIZE.y - OFFSET.y) origin = Vector2(OFFSET.x, SIZE.y - OFFSET.y)
func calculate_pass(): func calculate_pass():
if invert_chart: if invert_chart:
x_chors = y_labels as PoolStringArray x_chors = y_labels as PoolStringArray
@ -229,6 +231,7 @@ func calculate_pass():
x_pass = (SIZE.x - OFFSET.x) / (x_chors.size() - 1) x_pass = (SIZE.x - OFFSET.x) / (x_chors.size() - 1)
y_pass = origin.y / (y_chors.size() - 1) y_pass = origin.y / (y_chors.size() - 1)
func calculate_coordinates(): func calculate_coordinates():
x_coordinates.clear() x_coordinates.clear()
y_coordinates.clear() y_coordinates.clear()
@ -275,20 +278,30 @@ func calculate_coordinates():
for function in y_coordinates.size(): for function in y_coordinates.size():
for function_value in y_coordinates[function].size(): for function_value in y_coordinates[function].size():
if are_values_columns: if are_values_columns:
point_positions[function_value].append(Vector2(x_coordinates[function]+origin.x, origin.y-y_coordinates[function][function_value])) point_positions[function_value].append(Vector2(
point_values[function_value].append([x_datas[function_value],y_datas[function_value][function]]) x_coordinates[function] + origin.x,
origin.y - y_coordinates[function][function_value]))
point_values[function_value].append(
[x_datas[function_value], y_datas[function_value][function]])
else: else:
point_positions[function].append(Vector2(x_coordinates[function_value]+origin.x,origin.y-y_coordinates[function][function_value])) point_positions[function].append(Vector2(
point_values[function].append([x_datas[function_value],y_datas[function_value][function]]) x_coordinates[function_value] + origin.x,
origin.y - y_coordinates[function][function_value]))
point_values[function].append(
[x_datas[function_value], y_datas[function_value][function]])
else: else:
for cluster in y_coordinates.size(): for cluster in y_coordinates.size():
for y in y_coordinates[cluster].size(): for y in y_coordinates[cluster].size():
if are_values_columns: if are_values_columns:
point_values[y].append([x_datas[cluster], y_datas[cluster][y]]) point_values[y].append([x_datas[cluster], y_datas[cluster][y]])
point_positions[y].append(Vector2(x_coordinates[cluster]+origin.x,origin.y-y_coordinates[cluster][y])) point_positions[y].append(Vector2(
x_coordinates[cluster] + origin.x, origin.y - y_coordinates[cluster][y]))
else: else:
point_values[cluster].append([x_datas[y], y_datas[cluster][y]]) 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])) point_positions[cluster].append(Vector2(
x_coordinates[y] + origin.x,
origin.y - y_coordinates[cluster][y]))
func _draw(): func _draw():
clear_points() clear_points()
@ -301,8 +314,8 @@ func _draw():
defined_colors = true defined_colors = true
for _function in point_values.size(): for _function in point_values.size():
var PointContainer : Control = Control.new() var point_container: Control = Control.new()
Points.add_child(PointContainer) Points.add_child(point_container)
for function_point in point_values[_function].size(): for function_point in point_values[_function].size():
var point: Point = point_node.instance() var point: Point = point_node.instance()
@ -310,15 +323,23 @@ func _draw():
point.connect("_mouse_entered", self, "show_data") point.connect("_mouse_entered", self, "show_data")
point.connect("_mouse_exited", self, "hide_data") point.connect("_mouse_exited", self, "hide_data")
point.create_point(points_shape[_function], function_colors[function_point if invert_chart else _function], point.create_point(
Color.white, point_positions[_function][function_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), point.format_value(point_values[_function][function_point], false, false),
y_labels[function_point if invert_chart else _function] as String) y_labels[function_point if invert_chart else _function] as String)
PointContainer.add_child(point) point_container.add_child(point)
if function_point > 0: if function_point > 0:
draw_line(point_positions[_function][function_point-1], point_positions[_function][function_point], draw_line(
function_colors[function_point if invert_chart else _function], 2, false) point_positions[_function][function_point - 1],
point_positions[_function][function_point],
function_colors[function_point if invert_chart else _function],
2,
false)
func draw_grid(): func draw_grid():
# ascisse # ascisse
@ -328,7 +349,11 @@ func draw_grid():
draw_line(point, point - Vector2(0, SIZE.y - OFFSET.y), v_lines_color, 0.2, true) draw_line(point, point - Vector2(0, SIZE.y - OFFSET.y), v_lines_color, 0.2, true)
# ascisse # ascisse
draw_line(point - Vector2(0, 5), point, v_lines_color, 1, true) draw_line(point - Vector2(0, 5), point, v_lines_color, 1, true)
draw_string(font,point+Vector2(-const_width/2*x_chors[p].length(),font_size+const_height),x_chors[p],font_color) draw_string(
font,
point + Vector2(-const_width / 2 * x_chors[p].length(), font_size + const_height),
x_chors[p],
font_color)
# ordinate # ordinate
for p in y_chors.size(): for p in y_chors.size():
@ -337,7 +362,12 @@ func draw_grid():
draw_line(point, point + Vector2(SIZE.x - OFFSET.x, 0), h_lines_color, 0.2, true) draw_line(point, point + Vector2(SIZE.x - OFFSET.x, 0), h_lines_color, 0.2, true)
# ordinate # ordinate
draw_line(point, point + Vector2(5, 0), h_lines_color, 1, true) draw_line(point, point + Vector2(5, 0), h_lines_color, 1, true)
draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,-const_height),y_chors[p],font_color) draw_string(
font,
point - Vector2(y_chors[p].length() * const_width + font_size, -const_height),
y_chors[p],
font_color)
func draw_chart_outlines(): func draw_chart_outlines():
draw_line(origin, SIZE - Vector2(0, OFFSET.y), box_color, 1, true) draw_line(origin, SIZE - Vector2(0, OFFSET.y), box_color, 1, true)