From fd1826beb78da0393624c31b576fc2b3a36d78ea Mon Sep 17 00:00:00 2001 From: eddex Date: Mon, 5 Oct 2020 17:41:46 +0200 Subject: [PATCH 1/4] refactor: rename LineChart.gd to line_chart.gd --- addons/easy_charts/LineChart/LineChart.tscn | 3 ++- addons/easy_charts/LineChart/{LineChart.gd => line_chart.gd} | 0 2 files changed, 2 insertions(+), 1 deletion(-) rename addons/easy_charts/LineChart/{LineChart.gd => line_chart.gd} (100%) diff --git a/addons/easy_charts/LineChart/LineChart.tscn b/addons/easy_charts/LineChart/LineChart.tscn index 84c2210..e964306 100644 --- a/addons/easy_charts/LineChart/LineChart.tscn +++ b/addons/easy_charts/LineChart/LineChart.tscn @@ -1,7 +1,8 @@ [gd_scene load_steps=4 format=2] [ext_resource path="res://addons/easy_charts/Utilities/Point/PointData.tscn" type="PackedScene" id=1] -[ext_resource path="res://addons/easy_charts/LineChart/LineChart.gd" type="Script" id=4] +[ext_resource path="res://addons/easy_charts/LineChart/line_chart.gd" type="Script" id=4] + [sub_resource type="Theme" id=1] diff --git a/addons/easy_charts/LineChart/LineChart.gd b/addons/easy_charts/LineChart/line_chart.gd similarity index 100% rename from addons/easy_charts/LineChart/LineChart.gd rename to addons/easy_charts/LineChart/line_chart.gd From 79a2a6ff2ec0a2539e723ee31cd7ba29ad672f72 Mon Sep 17 00:00:00 2001 From: eddex Date: Mon, 5 Oct 2020 17:44:20 +0200 Subject: [PATCH 2/4] refactor: use # instread of """ for docstring --- addons/easy_charts/LineChart/line_chart.gd | 79 +++++++++++----------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/addons/easy_charts/LineChart/line_chart.gd b/addons/easy_charts/LineChart/line_chart.gd index b34b00e..56c79c1 100644 --- a/addons/easy_charts/LineChart/line_chart.gd +++ b/addons/easy_charts/LineChart/line_chart.gd @@ -1,19 +1,18 @@ tool extends Chart -""" -[Linechart] - General purpose node for Line Charts -A line chart or line plot or line graph or curve chart is a type of chart which -displays information as a series of data points called 'markers' -connected by straight line segments. -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) -and joined with straight line segments. -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. -In these cases they are known as run charts. -/ source : Wikipedia / -""" +# [Linechart] - General purpose node for Line Charts +# A line chart or line plot or line graph or curve chart is a type of chart which +# displays information as a series of data points called 'markers' +# connected by straight line segments. +# 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) +# and joined with straight line segments. +# 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. +# In these cases they are known as run charts. +# Source: Wikipedia + # --------------------- @@ -39,7 +38,7 @@ func _get_property_list(): "name": "Chart_Properties/show_x_values_as_labels", "type": TYPE_BOOL }, - + # Chart Display { "hint": PROPERTY_HINT_RANGE, @@ -55,14 +54,14 @@ func _get_property_list(): "name": "Chart_Display/y_decim", "type": TYPE_REAL }, - + # Chart Style - { - "hint": 24, + { + "hint": 24, "hint_string": "%d/%d:%s"%[TYPE_INT, PROPERTY_HINT_ENUM, PoolStringArray(PointShapes.keys()).join(",")], - "name": "Chart_Style/points_shape", - "type": TYPE_ARRAY, + "name": "Chart_Style/points_shape", + "type": TYPE_ARRAY, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE }, { @@ -118,7 +117,7 @@ func _get_property_list(): "name": "Chart_Style/template", "type": TYPE_INT }, - + # Chart Modifiers { "hint": PROPERTY_HINT_NONE, @@ -167,7 +166,7 @@ func structure_datas(database : Array, are_values_columns : bool, x_values_index for data in y_datas: for value in data.size(): data[value] = data[value] as float - + # draw y labels var to_order : Array var to_order_min : Array @@ -180,7 +179,7 @@ func structure_datas(database : Array, are_values_columns : bool, x_values_index var margin_min = ordered_cluster[0] to_order.append(margin_max) to_order_min.append(margin_min) - + to_order.sort() to_order_min.sort() var margin = to_order.pop_back() @@ -194,12 +193,12 @@ func structure_datas(database : Array, are_values_columns : bool, x_values_index multi+=1 p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0) y_chors.append(p as String) - + # draw x_labels if not show_x_values_as_labels: to_order.clear() to_order = x_datas as PoolIntArray - + to_order.sort() margin = to_order.pop_back() if not origin_at_zero: @@ -225,7 +224,7 @@ func calculate_pass(): x_chors = x_datas as PoolStringArray else: x_chors = x_labels - + # calculate distance in pixel between 2 consecutive values/datas x_pass = (SIZE.x - OFFSET.x) / (x_chors.size()-1) y_pass = origin.y / (y_chors.size()-1) @@ -235,7 +234,7 @@ func calculate_coordinates(): y_coordinates.clear() point_values.clear() point_positions.clear() - + if invert_chart: for column in y_datas[0].size(): var single_coordinates : Array @@ -254,7 +253,7 @@ func calculate_coordinates(): else: single_coordinates.append((cluster[value] - y_margin_min)*y_pass/v_dist) y_coordinates.append(single_coordinates) - + if show_x_values_as_labels: for x in x_datas.size(): x_coordinates.append(x_pass*x) @@ -267,11 +266,11 @@ func calculate_coordinates(): x_coordinates.append(x_datas[x]*x_pass/h_dist) else: x_coordinates.append((x_datas[x] - x_margin_min)*x_pass/h_dist) - + for f in functions: point_values.append([]) point_positions.append([]) - + if invert_chart: for function in y_coordinates.size(): for function_value in y_coordinates[function].size(): @@ -293,32 +292,32 @@ func calculate_coordinates(): func _draw(): clear_points() - + draw_grid() draw_chart_outlines() - + var defined_colors : bool = false if function_colors.size(): defined_colors = true - + for _function in point_values.size(): var PointContainer : Control = Control.new() Points.add_child(PointContainer) - + for function_point in point_values[_function].size(): var point : Point = point_node.instance() point.connect("_point_pressed",self,"point_pressed") point.connect("_mouse_entered",self,"show_data") point.connect("_mouse_exited",self,"hide_data") - - point.create_point(points_shape[_function], function_colors[function_point if invert_chart else _function], - Color.white, point_positions[_function][function_point], - point.format_value(point_values[_function][function_point], false, false), + + 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) - + PointContainer.add_child(point) if function_point > 0: - draw_line(point_positions[_function][function_point-1], point_positions[_function][function_point], + draw_line(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(): @@ -330,7 +329,7 @@ func draw_grid(): # ascisse draw_line(point-Vector2(0,5),point,v_lines_color,1,true) draw_string(font,point+Vector2(-const_width/2*x_chors[p].length(),font_size+const_height),x_chors[p],font_color) - + # ordinate for p in y_chors.size(): var point : Vector2 = origin-Vector2(0,(p)*y_pass) From 836efde1b648b23743ff28f64b70042e8793c581 Mon Sep 17 00:00:00 2001 From: eddex Date: Mon, 5 Oct 2020 17:49:31 +0200 Subject: [PATCH 3/4] refactor: convert indentation to tabs --- addons/easy_charts/LineChart/line_chart.gd | 588 ++++++++++----------- 1 file changed, 294 insertions(+), 294 deletions(-) diff --git a/addons/easy_charts/LineChart/line_chart.gd b/addons/easy_charts/LineChart/line_chart.gd index 56c79c1..b83f8e8 100644 --- a/addons/easy_charts/LineChart/line_chart.gd +++ b/addons/easy_charts/LineChart/line_chart.gd @@ -17,330 +17,330 @@ extends Chart # --------------------- 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 - }, + 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 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 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 - }, - ] + # 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 - # @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_index: - var x_data = database[row][column] - if x_data.is_valid_float() or x_data.is_valid_integer(): - x_datas.append(x_data as float) - else: - x_datas.append(x_data.replace(",",".") as float) - else: - if row != 0: - var y_data = database[row][column] - if y_data.is_valid_float() or y_data.is_valid_integer(): - t_vals.append(y_data as float) - else: - t_vals.append(y_data.replace(",",".") as float) - else: - y_labels.append(str(database[row][column])) - if not t_vals.empty(): - y_datas.append(t_vals) - x_label = str(x_datas.pop_front()) - false: - for row in database.size(): - if row == 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 + # @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 + match are_values_columns: + true: + 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()) + false: + 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 - # draw y labels - var to_order : Array - var to_order_min : Array - for cluster in y_datas.size(): - # define x_chors and y_chors - var ordered_cluster = y_datas[cluster] as Array - ordered_cluster.sort() - ordered_cluster = PoolIntArray(ordered_cluster) - var margin_max = ordered_cluster[ordered_cluster.size()-1] - var margin_min = ordered_cluster[0] - to_order.append(margin_max) - to_order_min.append(margin_min) + # draw y labels + var to_order : Array + var to_order_min : Array + for cluster in y_datas.size(): + # define x_chors and y_chors + var ordered_cluster = y_datas[cluster] as Array + ordered_cluster.sort() + ordered_cluster = PoolIntArray(ordered_cluster) + var margin_max = ordered_cluster[ordered_cluster.size()-1] + var margin_min = ordered_cluster[0] + to_order.append(margin_max) + to_order_min.append(margin_min) - to_order.sort() - to_order_min.sort() - var margin = to_order.pop_back() - if not origin_at_zero: - y_margin_min = to_order_min.pop_front() - v_dist = y_decim * pow(10.0,str(margin).length()-2) - var multi = 0 - var p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0) - y_chors.append(p as String) - while p < margin: - multi+=1 - p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0) - y_chors.append(p as String) + to_order.sort() + to_order_min.sort() + var margin = to_order.pop_back() + if not origin_at_zero: + y_margin_min = to_order_min.pop_front() + v_dist = y_decim * pow(10.0,str(margin).length()-2) + var multi = 0 + var p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0) + y_chors.append(p as String) + while p < margin: + multi+=1 + p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0) + y_chors.append(p as String) - # draw x_labels - if not show_x_values_as_labels: - to_order.clear() - to_order = x_datas as PoolIntArray + # draw x_labels + if not show_x_values_as_labels: + to_order.clear() + to_order = x_datas as PoolIntArray - to_order.sort() - margin = to_order.pop_back() - if not origin_at_zero: - x_margin_min = to_order.pop_front() - h_dist = x_decim * pow(10.0,str(margin).length()-2) - multi = 0 - p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0) - x_labels.append(p as String) - while p < margin: - multi+=1 - p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0) - x_labels.append(p as String) + to_order.sort() + margin = to_order.pop_back() + if not origin_at_zero: + x_margin_min = to_order.pop_front() + h_dist = x_decim * pow(10.0,str(margin).length()-2) + multi = 0 + p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0) + x_labels.append(p as String) + while p < margin: + multi+=1 + p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0) + x_labels.append(p as String) func build_chart(): - SIZE = get_size() - origin = Vector2(OFFSET.x,SIZE.y-OFFSET.y) + SIZE = get_size() + origin = Vector2(OFFSET.x,SIZE.y-OFFSET.y) func calculate_pass(): - if invert_chart: - x_chors = y_labels as PoolStringArray - else: - if show_x_values_as_labels: - x_chors = x_datas as PoolStringArray - else: - x_chors = x_labels + if invert_chart: + x_chors = y_labels as PoolStringArray + else: + if show_x_values_as_labels: + x_chors = x_datas as PoolStringArray + else: + x_chors = x_labels - # calculate distance in pixel between 2 consecutive values/datas - x_pass = (SIZE.x - OFFSET.x) / (x_chors.size()-1) - y_pass = origin.y / (y_chors.size()-1) + # calculate distance in pixel between 2 consecutive values/datas + x_pass = (SIZE.x - OFFSET.x) / (x_chors.size()-1) + y_pass = origin.y / (y_chors.size()-1) func calculate_coordinates(): - x_coordinates.clear() - y_coordinates.clear() - point_values.clear() - point_positions.clear() + x_coordinates.clear() + y_coordinates.clear() + point_values.clear() + point_positions.clear() - if invert_chart: - for column in y_datas[0].size(): - var single_coordinates : Array - for row in y_datas: - if origin_at_zero: - single_coordinates.append((row[column]*y_pass)/v_dist) - else: - single_coordinates.append((row[column] - y_margin_min)*y_pass/v_dist) - y_coordinates.append(single_coordinates) - else: - for cluster in y_datas: - var single_coordinates : Array - for value in cluster.size(): - if origin_at_zero: - single_coordinates.append((cluster[value]*y_pass)/v_dist) - else: - single_coordinates.append((cluster[value] - y_margin_min)*y_pass/v_dist) - y_coordinates.append(single_coordinates) + if invert_chart: + for column in y_datas[0].size(): + var single_coordinates : Array + for row in y_datas: + if origin_at_zero: + single_coordinates.append((row[column]*y_pass)/v_dist) + else: + single_coordinates.append((row[column] - y_margin_min)*y_pass/v_dist) + y_coordinates.append(single_coordinates) + else: + for cluster in y_datas: + var single_coordinates : Array + for value in cluster.size(): + if origin_at_zero: + single_coordinates.append((cluster[value]*y_pass)/v_dist) + else: + single_coordinates.append((cluster[value] - y_margin_min)*y_pass/v_dist) + y_coordinates.append(single_coordinates) - if show_x_values_as_labels: - for x in x_datas.size(): - x_coordinates.append(x_pass*x) - else: - for x in x_datas.size(): - if origin_at_zero: - if invert_chart: - x_coordinates.append(x_pass*x) - else: - x_coordinates.append(x_datas[x]*x_pass/h_dist) - else: - x_coordinates.append((x_datas[x] - x_margin_min)*x_pass/h_dist) + if show_x_values_as_labels: + for x in x_datas.size(): + x_coordinates.append(x_pass*x) + else: + for x in x_datas.size(): + if origin_at_zero: + if invert_chart: + x_coordinates.append(x_pass*x) + else: + x_coordinates.append(x_datas[x]*x_pass/h_dist) + else: + x_coordinates.append((x_datas[x] - x_margin_min)*x_pass/h_dist) - for f in functions: - point_values.append([]) - point_positions.append([]) + for f in functions: + point_values.append([]) + point_positions.append([]) - if invert_chart: - for function in y_coordinates.size(): - for function_value in y_coordinates[function].size(): - if are_values_columns: - point_positions[function_value].append(Vector2(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: - point_positions[function].append(Vector2(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: - for cluster in y_coordinates.size(): - for y in y_coordinates[cluster].size(): - if are_values_columns: - point_values[y].append([x_datas[cluster],y_datas[cluster][y]]) - point_positions[y].append(Vector2(x_coordinates[cluster]+origin.x,origin.y-y_coordinates[cluster][y])) - else: - point_values[cluster].append([x_datas[y],y_datas[cluster][y]]) - point_positions[cluster].append(Vector2(x_coordinates[y]+origin.x,origin.y-y_coordinates[cluster][y])) + if invert_chart: + for function in y_coordinates.size(): + for function_value in y_coordinates[function].size(): + if are_values_columns: + point_positions[function_value].append(Vector2(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: + point_positions[function].append(Vector2(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: + for cluster in y_coordinates.size(): + for y in y_coordinates[cluster].size(): + if are_values_columns: + point_values[y].append([x_datas[cluster],y_datas[cluster][y]]) + point_positions[y].append(Vector2(x_coordinates[cluster]+origin.x,origin.y-y_coordinates[cluster][y])) + else: + point_values[cluster].append([x_datas[y],y_datas[cluster][y]]) + point_positions[cluster].append(Vector2(x_coordinates[y]+origin.x,origin.y-y_coordinates[cluster][y])) func _draw(): - clear_points() + clear_points() - draw_grid() - draw_chart_outlines() + draw_grid() + draw_chart_outlines() - var defined_colors : bool = false - if function_colors.size(): - defined_colors = true + var defined_colors : bool = false + if function_colors.size(): + defined_colors = true - for _function in point_values.size(): - var PointContainer : Control = Control.new() - Points.add_child(PointContainer) + for _function in point_values.size(): + var PointContainer : Control = Control.new() + Points.add_child(PointContainer) - for function_point in point_values[_function].size(): - var point : Point = point_node.instance() - point.connect("_point_pressed",self,"point_pressed") - point.connect("_mouse_entered",self,"show_data") - point.connect("_mouse_exited",self,"hide_data") + for function_point in point_values[_function].size(): + var point : Point = point_node.instance() + point.connect("_point_pressed",self,"point_pressed") + point.connect("_mouse_entered",self,"show_data") + point.connect("_mouse_exited",self,"hide_data") - point.create_point(points_shape[_function], function_colors[function_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) + 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) - PointContainer.add_child(point) - if function_point > 0: - draw_line(point_positions[_function][function_point-1], point_positions[_function][function_point], - function_colors[function_point if invert_chart else _function], 2, false) + PointContainer.add_child(point) + if function_point > 0: + draw_line(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(): - # ascisse - for p in x_chors.size(): - var point : Vector2 = origin+Vector2((p)*x_pass,0) - # v grid - draw_line(point,point-Vector2(0,SIZE.y-OFFSET.y),v_lines_color,0.2,true) - # ascisse - draw_line(point-Vector2(0,5),point,v_lines_color,1,true) - draw_string(font,point+Vector2(-const_width/2*x_chors[p].length(),font_size+const_height),x_chors[p],font_color) + # ascisse + for p in x_chors.size(): + var point : Vector2 = origin+Vector2((p)*x_pass,0) + # v grid + draw_line(point,point-Vector2(0,SIZE.y-OFFSET.y),v_lines_color,0.2,true) + # ascisse + draw_line(point-Vector2(0,5),point,v_lines_color,1,true) + draw_string(font,point+Vector2(-const_width/2*x_chors[p].length(),font_size+const_height),x_chors[p],font_color) - # ordinate - for p in y_chors.size(): - var point : Vector2 = origin-Vector2(0,(p)*y_pass) - # h grid - draw_line(point,point+Vector2(SIZE.x-OFFSET.x,0),h_lines_color,0.2,true) - # ordinate - draw_line(point,point+Vector2(5,0),h_lines_color,1,true) - draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,-const_height),y_chors[p],font_color) + # ordinate + for p in y_chors.size(): + var point : Vector2 = origin-Vector2(0,(p)*y_pass) + # h grid + draw_line(point,point+Vector2(SIZE.x-OFFSET.x,0),h_lines_color,0.2,true) + # ordinate + draw_line(point,point+Vector2(5,0),h_lines_color,1,true) + draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,-const_height),y_chors[p],font_color) func draw_chart_outlines(): - draw_line(origin,SIZE-Vector2(0,OFFSET.y),box_color,1,true) - draw_line(origin,Vector2(OFFSET.x,0),box_color,1,true) - draw_line(Vector2(OFFSET.x,0),Vector2(SIZE.x,0),box_color,1,true) - draw_line(Vector2(SIZE.x,0),SIZE-Vector2(0,OFFSET.y),box_color,1,true) + draw_line(origin,SIZE-Vector2(0,OFFSET.y),box_color,1,true) + draw_line(origin,Vector2(OFFSET.x,0),box_color,1,true) + draw_line(Vector2(OFFSET.x,0),Vector2(SIZE.x,0),box_color,1,true) + draw_line(Vector2(SIZE.x,0),SIZE-Vector2(0,OFFSET.y),box_color,1,true) From b871022ccbfd45597d2b988f523c7b582185f073 Mon Sep 17 00:00:00 2001 From: eddex Date: Mon, 5 Oct 2020 18:12:23 +0200 Subject: [PATCH 4/4] refactor: apply GDScript style guide to line_chart.gd --- addons/easy_charts/LineChart/line_chart.gd | 228 ++++++++++++--------- 1 file changed, 129 insertions(+), 99 deletions(-) diff --git a/addons/easy_charts/LineChart/line_chart.gd b/addons/easy_charts/LineChart/line_chart.gd index b83f8e8..b283809 100644 --- a/addons/easy_charts/LineChart/line_chart.gd +++ b/addons/easy_charts/LineChart/line_chart.gd @@ -8,14 +8,12 @@ extends Chart # 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) # and joined with straight line segments. -# 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 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. # In these cases they are known as run charts. # Source: Wikipedia -# --------------------- - func _get_property_list(): return [ # Chart Properties @@ -58,8 +56,11 @@ func _get_property_list(): # Chart Style { "hint": 24, - "hint_string": "%d/%d:%s"%[TYPE_INT, PROPERTY_HINT_ENUM, - PoolStringArray(PointShapes.keys()).join(",")], + "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 @@ -127,55 +128,54 @@ 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 - # @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_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) + 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: - if row != 0: - var y_data = database[row][column] - if y_data.is_valid_float() or y_data.is_valid_integer(): - t_vals.append(y_data as float) - else: - t_vals.append(y_data.replace(",",".") as float) - else: - y_labels.append(str(database[row][column])) - if not t_vals.empty(): - y_datas.append(t_vals) - x_label = str(x_datas.pop_front()) - false: - for row in database.size(): - if row == x_values_index: - x_datas = (database[row]) - x_label = x_datas.pop_front() as String + x_datas.append(x_data.replace(",", ".") as float) 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 + 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 # draw y labels - var to_order : Array - var to_order_min : Array + var to_order: Array + var to_order_min: Array for cluster in y_datas.size(): # define x_chors and y_chors var ordered_cluster = y_datas[cluster] as Array ordered_cluster.sort() ordered_cluster = PoolIntArray(ordered_cluster) - var margin_max = ordered_cluster[ordered_cluster.size()-1] + var margin_max = ordered_cluster[ordered_cluster.size() - 1] var margin_min = ordered_cluster[0] to_order.append(margin_max) to_order_min.append(margin_min) @@ -185,13 +185,13 @@ func structure_datas(database : Array, are_values_columns : bool, x_values_index var margin = to_order.pop_back() if not origin_at_zero: y_margin_min = to_order_min.pop_front() - v_dist = y_decim * pow(10.0,str(margin).length()-2) + v_dist = y_decim * pow(10.0, str(margin).length() - 2) var multi = 0 - var p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0) + var p = (v_dist * multi) + ((y_margin_min) if not origin_at_zero else 0) y_chors.append(p as String) while p < margin: - multi+=1 - p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0) + multi += 1 + p = (v_dist * multi) + ((y_margin_min) if not origin_at_zero else 0) y_chors.append(p as String) # draw x_labels @@ -203,18 +203,20 @@ func structure_datas(database : Array, are_values_columns : bool, x_values_index margin = to_order.pop_back() if not origin_at_zero: x_margin_min = to_order.pop_front() - h_dist = x_decim * pow(10.0,str(margin).length()-2) + h_dist = x_decim * pow(10.0, str(margin).length() - 2) multi = 0 - p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0) + p = (h_dist * multi) + ((x_margin_min) if not origin_at_zero else 0) x_labels.append(p as String) while p < margin: - multi+=1 - p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0) + multi += 1 + p = (h_dist * multi) + ((x_margin_min) if not origin_at_zero else 0) x_labels.append(p as String) + func build_chart(): SIZE = get_size() - origin = Vector2(OFFSET.x,SIZE.y-OFFSET.y) + origin = Vector2(OFFSET.x, SIZE.y - OFFSET.y) + func calculate_pass(): if invert_chart: @@ -226,8 +228,9 @@ func calculate_pass(): x_chors = x_labels # calculate distance in pixel between 2 consecutive values/datas - x_pass = (SIZE.x - OFFSET.x) / (x_chors.size()-1) - y_pass = origin.y / (y_chors.size()-1) + x_pass = (SIZE.x - OFFSET.x) / (x_chors.size() - 1) + y_pass = origin.y / (y_chors.size() - 1) + func calculate_coordinates(): x_coordinates.clear() @@ -237,35 +240,35 @@ func calculate_coordinates(): if invert_chart: for column in y_datas[0].size(): - var single_coordinates : Array + var single_coordinates: Array for row in y_datas: if origin_at_zero: - single_coordinates.append((row[column]*y_pass)/v_dist) + single_coordinates.append((row[column] * y_pass) / v_dist) else: - single_coordinates.append((row[column] - y_margin_min)*y_pass/v_dist) + single_coordinates.append((row[column] - y_margin_min) * y_pass / v_dist) y_coordinates.append(single_coordinates) else: for cluster in y_datas: - var single_coordinates : Array + var single_coordinates: Array for value in cluster.size(): if origin_at_zero: - single_coordinates.append((cluster[value]*y_pass)/v_dist) + single_coordinates.append((cluster[value] * y_pass) / v_dist) else: - single_coordinates.append((cluster[value] - y_margin_min)*y_pass/v_dist) + single_coordinates.append((cluster[value] - y_margin_min) * y_pass / v_dist) y_coordinates.append(single_coordinates) if show_x_values_as_labels: for x in x_datas.size(): - x_coordinates.append(x_pass*x) + x_coordinates.append(x_pass * x) else: for x in x_datas.size(): if origin_at_zero: if invert_chart: - x_coordinates.append(x_pass*x) + x_coordinates.append(x_pass * x) else: - x_coordinates.append(x_datas[x]*x_pass/h_dist) + x_coordinates.append(x_datas[x] * x_pass / h_dist) else: - x_coordinates.append((x_datas[x] - x_margin_min)*x_pass/h_dist) + x_coordinates.append((x_datas[x] - x_margin_min) * x_pass / h_dist) for f in functions: point_values.append([]) @@ -275,20 +278,30 @@ func calculate_coordinates(): for function in y_coordinates.size(): for function_value in y_coordinates[function].size(): if are_values_columns: - point_positions[function_value].append(Vector2(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]]) + point_positions[function_value].append(Vector2( + 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: - point_positions[function].append(Vector2(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]]) + point_positions[function].append(Vector2( + 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: for cluster in y_coordinates.size(): for y in y_coordinates[cluster].size(): if are_values_columns: - point_values[y].append([x_datas[cluster],y_datas[cluster][y]]) - point_positions[y].append(Vector2(x_coordinates[cluster]+origin.x,origin.y-y_coordinates[cluster][y])) + point_values[y].append([x_datas[cluster], y_datas[cluster][y]]) + point_positions[y].append(Vector2( + x_coordinates[cluster] + origin.x, origin.y - y_coordinates[cluster][y])) else: - point_values[cluster].append([x_datas[y],y_datas[cluster][y]]) - point_positions[cluster].append(Vector2(x_coordinates[y]+origin.x,origin.y-y_coordinates[cluster][y])) + point_values[cluster].append([x_datas[y], y_datas[cluster][y]]) + point_positions[cluster].append(Vector2( + x_coordinates[y] + origin.x, + origin.y - y_coordinates[cluster][y])) + func _draw(): clear_points() @@ -296,51 +309,68 @@ func _draw(): draw_grid() draw_chart_outlines() - var defined_colors : bool = false + var defined_colors: bool = false if function_colors.size(): defined_colors = true for _function in point_values.size(): - var PointContainer : Control = Control.new() - Points.add_child(PointContainer) + var point_container: Control = Control.new() + Points.add_child(point_container) for function_point in point_values[_function].size(): - var point : Point = point_node.instance() - point.connect("_point_pressed",self,"point_pressed") - point.connect("_mouse_entered",self,"show_data") - point.connect("_mouse_exited",self,"hide_data") + var point: Point = point_node.instance() + point.connect("_point_pressed", self, "point_pressed") + point.connect("_mouse_entered", self, "show_data") + point.connect("_mouse_exited", self, "hide_data") - point.create_point(points_shape[_function], function_colors[function_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) + 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) - PointContainer.add_child(point) + point_container.add_child(point) if function_point > 0: - draw_line(point_positions[_function][function_point-1], point_positions[_function][function_point], - function_colors[function_point if invert_chart else _function], 2, false) + draw_line( + 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(): # ascisse for p in x_chors.size(): - var point : Vector2 = origin+Vector2((p)*x_pass,0) + var point: Vector2 = origin + Vector2((p) * x_pass, 0) # v grid - draw_line(point,point-Vector2(0,SIZE.y-OFFSET.y),v_lines_color,0.2,true) + draw_line(point, point - Vector2(0, SIZE.y - OFFSET.y), v_lines_color, 0.2, true) # ascisse - draw_line(point-Vector2(0,5),point,v_lines_color,1,true) - draw_string(font,point+Vector2(-const_width/2*x_chors[p].length(),font_size+const_height),x_chors[p],font_color) + draw_line(point - Vector2(0, 5), point, v_lines_color, 1, true) + draw_string( + font, + point + Vector2(-const_width / 2 * x_chors[p].length(), font_size + const_height), + x_chors[p], + font_color) # ordinate for p in y_chors.size(): - var point : Vector2 = origin-Vector2(0,(p)*y_pass) + var point: Vector2 = origin - Vector2(0, (p) * y_pass) # h grid - draw_line(point,point+Vector2(SIZE.x-OFFSET.x,0),h_lines_color,0.2,true) + draw_line(point, point + Vector2(SIZE.x - OFFSET.x, 0), h_lines_color, 0.2, true) # ordinate - draw_line(point,point+Vector2(5,0),h_lines_color,1,true) - draw_string(font,point-Vector2(y_chors[p].length()*const_width+font_size,-const_height),y_chors[p],font_color) + draw_line(point, point + Vector2(5, 0), h_lines_color, 1, true) + draw_string( + font, + point - Vector2(y_chors[p].length() * const_width + font_size, -const_height), + y_chors[p], + font_color) + func draw_chart_outlines(): - draw_line(origin,SIZE-Vector2(0,OFFSET.y),box_color,1,true) - draw_line(origin,Vector2(OFFSET.x,0),box_color,1,true) - draw_line(Vector2(OFFSET.x,0),Vector2(SIZE.x,0),box_color,1,true) - draw_line(Vector2(SIZE.x,0),SIZE-Vector2(0,OFFSET.y),box_color,1,true) + draw_line(origin, SIZE - Vector2(0, OFFSET.y), box_color, 1, true) + draw_line(origin, Vector2(OFFSET.x, 0), box_color, 1, true) + draw_line(Vector2(OFFSET.x, 0), Vector2(SIZE.x, 0), box_color, 1, true) + draw_line(Vector2(SIZE.x, 0), SIZE - Vector2(0, OFFSET.y), box_color, 1, true)