update reading methods

This commit is contained in:
fenix-hub 2022-01-09 18:47:39 +01:00
parent e48083802e
commit 38b78bfd64
4 changed files with 44 additions and 110 deletions

View File

@ -45,6 +45,7 @@ func _get_property_list():
return property_list return property_list
# @Override
func calculate_coordinates(): func calculate_coordinates():
point_values.clear() point_values.clear()
point_positions.clear() point_positions.clear()
@ -55,8 +56,8 @@ func calculate_coordinates():
for function in y_labels.size(): for function in y_labels.size():
for val in x_datas[function].size(): for val in x_datas[function].size():
var value_x: float = (int(x_datas[function][val]) - x_margin_min) * x_pass / h_dist if h_dist else 0 \ var value_x: float = (int(x_datas[function][val]) - x_margin_min) * x_pass / h_dist if h_dist else \
if not show_x_values_as_labels else x_chors.find(String(x_datas[function][val])) * x_pass x_chors.find(String(x_datas[function][val])) * x_pass
var value_y: float = (y_datas[function][val] - y_margin_min) * y_pass / v_dist if v_dist else 0 var value_y: float = (y_datas[function][val] - y_margin_min) * y_pass / v_dist if v_dist else 0
var column_offset: float = column_width/2 + (column_width + column_gap)*function - (column_width + column_gap)*functions/2 var column_offset: float = column_width/2 + (column_width + column_gap)*function - (column_width + column_gap)*functions/2
point_values[function].append([x_datas[function][val], y_datas[function][val]]) point_values[function].append([x_datas[function][val], y_datas[function][val]])

View File

@ -78,7 +78,6 @@ export (String) var delimiter : String = ";" setget set_delimiter
var origin_at_zero : bool = false setget set_origin_at_zero#, get_origin_at_zero var origin_at_zero : bool = false setget set_origin_at_zero#, get_origin_at_zero
var are_values_columns : bool = true setget set_are_values_columns#, get_are_values_columns var are_values_columns : bool = true setget set_are_values_columns#, get_are_values_columns
var show_x_values_as_labels : bool = false 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 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 var function_names_index : int = 0 setget set_function_names_index#, get_function_names_index
@ -159,8 +158,6 @@ func _get(property):
return origin_at_zero return origin_at_zero
"Chart_Properties/are_values_columns": "Chart_Properties/are_values_columns":
return are_values_columns return are_values_columns
"Chart_Properties/show_x_values_as_labels":
return show_x_values_as_labels
"Chart_Properties/labels_index": "Chart_Properties/labels_index":
return labels_index return labels_index
"Chart_Properties/function_names_index": "Chart_Properties/function_names_index":
@ -228,9 +225,6 @@ func _set(property, value):
"Chart_Properties/are_values_columns": "Chart_Properties/are_values_columns":
are_values_columns = value are_values_columns = value
return true return true
"Chart_Properties/show_x_values_as_labels":
show_x_values_as_labels = value
return true
"Chart_Properties/labels_index": "Chart_Properties/labels_index":
labels_index = value labels_index = value
return true return true
@ -339,7 +333,7 @@ func plot(_dataset: Array = read_data(source, delimiter)) -> void:
ECUtilities._print_message("Can't plot a chart with an empty Array.",1) ECUtilities._print_message("Can't plot a chart with an empty Array.",1)
return return
data = _dataset.duplicate(true) data = _dataset
structure_data(slice_data(data)) structure_data(slice_data(data))
compute_display() compute_display()
display_plot() display_plot()
@ -350,7 +344,7 @@ func plot_from_source(file : String, _delimiter : String = delimiter) -> void:
plot(read_data(file, _delimiter)) plot(read_data(file, _delimiter))
func plot_from_dataframe(dataframe : DataFrame) -> void: func plot_from_dataframe(dataframe : DataFrame) -> void:
plot(dataframe.get_dataframe()) plot(dataframe.get_dataset())
func plot_placeholder() -> void: func plot_placeholder() -> void:
pass pass
@ -358,12 +352,8 @@ func plot_placeholder() -> void:
# Append new data (in array format) to the already plotted data. # Append new data (in array format) to the already plotted data.
# The new data will be appended as a new row of the dataset. # The new data will be appended as a new row of the dataset.
# All data are stored. # All data are stored.
func update_plot(new_data : Array) -> void: func update_plot(new_data : Array = []) -> void:
if new_data.empty(): if not new_data.empty(): data.append(new_data)
ECUtilities._print_message("Can't plot a chart with an empty Array.",1)
return
data.append(new_data.duplicate(true))
plot(data) plot(data)
# Append a new column to data # Append a new column to data
@ -382,11 +372,10 @@ func read_data(source : String, _delimiter : String = delimiter):
var content : Array var content : Array
while not file.eof_reached(): while not file.eof_reached():
var line : PoolStringArray = file.get_csv_line(_delimiter) var line : PoolStringArray = file.get_csv_line(_delimiter)
if line.empty() or line.size() < 2:
continue
content.append(line) content.append(line)
file.close() file.close()
for data in content:
if data.size() < 2 or data.empty():
content.erase(data)
return content.duplicate(true) return content.duplicate(true)
func slice_data(data: Array) -> Array: func slice_data(data: Array) -> Array:
@ -565,10 +554,6 @@ func set_origin_at_zero(b : bool):
func set_are_values_columns(b : bool): func set_are_values_columns(b : bool):
are_values_columns = b 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): func set_labels_index(i : int):
labels_index = i labels_index = i

View File

@ -41,13 +41,6 @@ func build_property_list():
"name": "Chart_Properties/labels_index", "name": "Chart_Properties/labels_index",
"type": TYPE_INT "type": TYPE_INT
}) })
property_list.append(
{
"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 # Chart Display
property_list.append( property_list.append(
@ -370,72 +363,47 @@ func generate_identifier():
return "f%d" % (y_labels.size() + 1) return "f%d" % (y_labels.size() + 1)
func structure_data(database : Array): func structure_data(dataset : Array):
# @labels_index can be either a column or a row relative to x values # @labels_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 # @y_values can be either a column or a row relative to y values
#This is done to make sure this arrays are empty on subsecuent calls of this function. #This is done to make sure this arrays are empty on subsecuent calls of this function.
#This function is called from the "old" methods such as plot_from_array and #This function is called from the "old" methods such as plot_from_array and
#for the moment it doesn't clean this variables on clean_variable. #for the moment it doesn't clean this variables on clean_variable.
are_values_columns = invert_chart != are_values_columns
# Read the dataset in the proper way
var database : Array = dataset \
if not are_values_columns \
else MatrixGenerator.transpose(Matrix.new(dataset)).to_array()
var x_values := []
var y_values := []
x_domain = [[], []] x_domain = [[], []]
y_domain = [[], []] y_domain = [[], []]
are_values_columns = invert_chart != are_values_columns var database_size = range(database.size())
var x_values := [] if database_size.has(labels_index):
x_values = database[labels_index]
x_label = x_values.pop_front() as String
x_labels = x_values
database_size.erase(labels_index) #Remove x row from the iterator
for row in database_size:
y_values = database[row] as Array
y_labels.append(y_values.pop_front() as String)
for val in y_values.size():
y_values[val] = y_values[val] as float
y_datas.append(y_values)
for x_value in x_values:
if str(x_value).is_valid_float():
x_datas.append(x_values if not x_values.empty() else range(y_values.size()))
else:
x_datas.append(x_labels)
if are_values_columns:
var y_values := []
var y_columns = database[0].size()
if range(database.size()).has(labels_index): # x column is present
y_columns -= 1
else:
x_values = range(database.size()) #If no x column is given, a generic one is generated
x_values.push_front("")
for _i in y_columns: #Resize to number of y columns
y_values.append([])
for row in database.size():
var y_column = 0
for column in database[row].size():
if column == labels_index:
var x_data = database[row][column]
if typeof(x_data) == TYPE_INT or typeof(x_data) == TYPE_REAL:
x_values.append(x_data as float)
else:
x_values.append(x_data.replace(",", ".") as float)
else:
if row != 0:
var y_data = database[row][column]
if typeof(y_data) == TYPE_INT or typeof(y_data) == TYPE_REAL:
y_values[y_column].append(y_data as float)
else:
y_values[y_column].append(y_data.replace(",",".") as float)
else:
y_labels.append(str(database[row][column]))
y_column += 1
x_label = str(x_values.pop_front())
for function in y_values.size():
y_datas.append(y_values[function])
x_datas.append(x_values)
else:
var database_size = range(database.size())
if database_size.has(labels_index):
x_values = database[labels_index]
x_label = x_values.pop_front() as String
database_size.erase(labels_index) #Remove x row from the iterator
for row in database_size:
var y_values = database[row] as Array
y_labels.append(y_values.pop_front() as String)
for val in y_values.size():
y_values[val] = y_values[val] as float
y_datas.append(y_values)
x_datas.append(x_values if not x_values.empty() else range(y_values.size()))
for function in y_labels: for function in y_labels:
y_domain[0].append(null) y_domain[0].append(null)
y_domain[1].append(null) y_domain[1].append(null)
@ -488,27 +456,7 @@ func calculate_tics():
calculate_interval_tics(y_margin_min, y_margin_max, v_dist, y_chors) calculate_interval_tics(y_margin_min, y_margin_max, v_dist, y_chors)
for i in y_chors.size(): for i in y_chors.size():
y_chors[i] = String(y_chors[i]) #Can't cast directly on calculate_interval_tics because it mess up with the sorting y_chors[i] = String(y_chors[i]) #Can't cast directly on calculate_interval_tics because it mess up with the sorting
x_chors = x_labels
if not show_x_values_as_labels:
x_margin_min = x_range[0]
var x_margin_max = x_range[1]
h_dist = x_decim * pow(10.0, calculate_position_significant_figure(x_margin_max - x_margin_min) - 1)
if x_margin_min < 0 and x_margin_max >= 0:
calculate_interval_tics(0, x_margin_min, -h_dist, x_labels) #Negative tics
calculate_interval_tics(0, x_margin_max, h_dist, x_labels, false) #Positive tics
x_labels.sort()
x_margin_min = min(x_margin_min, x_labels[0])
else:
calculate_interval_tics(x_margin_min, x_margin_max, h_dist, x_labels)
for i in x_labels.size():
x_labels[i] = String(x_labels[i])
x_chors = x_labels
else:
for function in y_labels.size():
for value in x_datas[function]:
if not x_chors.has(value as String): #Don't append repeated values
x_chors.append(value as String)
func build_chart(): func build_chart():
@ -547,8 +495,8 @@ func calculate_coordinates():
for function in y_labels.size(): for function in y_labels.size():
for val in x_datas[function].size(): for val in x_datas[function].size():
var value_x = (int(x_datas[function][val]) - x_margin_min) * x_pass / h_dist if h_dist else 0 \ var value_x = (int(x_datas[function][val]) - x_margin_min) * x_pass / h_dist if h_dist else \
if not show_x_values_as_labels else x_chors.find(String(x_datas[function][val])) * x_pass x_chors.find(String(x_datas[function][val])) * x_pass
var value_y = (y_datas[function][val] - y_margin_min) * y_pass / v_dist if v_dist else 0 var value_y = (y_datas[function][val] - y_margin_min) * y_pass / v_dist if v_dist else 0
point_values[function].append([x_datas[function][val], y_datas[function][val]]) point_values[function].append([x_datas[function][val], y_datas[function][val]])

View File

@ -62,7 +62,7 @@ func _to_string() -> String:
var string : String = "" var string : String = ""
for row_i in dataset.size(): for row_i in dataset.size():
if row_i == 0: if row_i == 0:
string+="%*s" % [last_string_len+1, ""] string+="%*s" % [last_string_len+1, ""] if get_dataset()[0].size() < get_dataset()[1].size() else ""
for column_i in dataset[row_i].size(): for column_i in dataset[row_i].size():
string+="%*s" % [last_string_len+1, dataset[row_i][column_i]] string+="%*s" % [last_string_len+1, dataset[row_i][column_i]]
string+="\n" string+="\n"