Add autoscale or user-defined range

This commit is contained in:
Jorge 2021-03-02 20:18:31 +01:00
parent 1252fe7a59
commit 16d144f418
3 changed files with 165 additions and 44 deletions

View File

@ -2,19 +2,17 @@ tool
extends ScatterChartBase extends ScatterChartBase
class_name ScatterChart class_name ScatterChart
"""
[ScatterChart] - General purpose node for Scatter Charts
A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) # [ScatterChart] - General purpose node for Scatter Charts
is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables # A scatter plot (also called a scatterplot, scatter graph, scatter chart,
for a set of data. If the points are coded (color/shape/size), one additional variable can be displayed. # scattergram, or scatter diagram) is a type of plot or mathematical diagram
The data are displayed as a collection of points, each having the value of one variable determining the position on # using Cartesian coordinates to display values for typically two variables
the horizontal axis and the value of the other variable determining the position on the vertical axis. # for a set of data. If the points are coded (color/shape/size), one additional
# variable can be displayed. The data are displayed as a collection of points,
/ source : Wikipedia / # each having the value of one variable determining the position on the
""" # horizontal axis and the value of the other variable determining the position
# on the vertical axis.
# --------------------- # Source: Wikipedia
func _get_property_list(): func _get_property_list():

View File

@ -10,54 +10,118 @@ class_name ScatterChartBase
var x_domain := [[], []] var x_domain := [[], []]
var y_domain := [[], []] var y_domain := [[], []]
var x_range var x_range := [0, 0]
var y_range var y_range := [0, 0]
var autoscale_x = true
var autoscale_y = true
var property_list = []
func _init():
build_property_list()
func build_property_list():
property_list.clear()
var property_list = [
# Chart Properties # Chart Properties
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_CATEGORY, "usage": PROPERTY_USAGE_CATEGORY,
"name": "ScatterChartBase", #TODO Changue this in the child classes "name": "ScatterChartBase",
"type": TYPE_STRING "type": TYPE_STRING
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Properties/are_values_columns", "name": "Chart_Properties/are_values_columns",
"type": TYPE_BOOL "type": TYPE_BOOL
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_RANGE, "hint": PROPERTY_HINT_RANGE,
"hint_string": "-1,100,1", "hint_string": "-1,100,1",
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Properties/labels_index", "name": "Chart_Properties/labels_index",
"type": TYPE_INT "type": TYPE_INT
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Properties/show_x_values_as_labels", "name": "Chart_Properties/show_x_values_as_labels",
"type": TYPE_BOOL "type": TYPE_BOOL
}, })
# Chart Display # Chart Display
property_list.append(
{
"hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/autoscale_x",
"type": TYPE_BOOL
})
if not autoscale_x:
property_list.append(
{
"hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/min_x_range",
"type": TYPE_REAL
})
property_list.append(
{
"hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/max_x_range",
"type": TYPE_REAL
})
property_list.append(
{ {
"hint": PROPERTY_HINT_RANGE, "hint": PROPERTY_HINT_RANGE,
"hint_string": "0.001, 10", "hint_string": "0.001, 10",
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/x_decim", "name": "Chart_Display/x_decim",
"type": TYPE_REAL "type": TYPE_REAL
}, })
property_list.append(
{
"hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/autoscale_y",
"type": TYPE_BOOL
})
if not autoscale_y:
property_list.append(
{
"hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/min_y_range",
"type": TYPE_REAL
})
property_list.append(
{
"hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/max_y_range",
"type": TYPE_REAL
})
property_list.append(
{ {
"hint": PROPERTY_HINT_RANGE, "hint": PROPERTY_HINT_RANGE,
"hint_string": "0.001, 10", "hint_string": "0.001, 10",
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Display/y_decim", "name": "Chart_Display/y_decim",
"type": TYPE_REAL "type": TYPE_REAL
}, })
# Chart Style # Chart Style
property_list.append(
{ {
"hint": 24, "hint": 24,
"hint_string": ("%d/%d:%s" "hint_string": ("%d/%d:%s"
@ -66,31 +130,36 @@ var property_list = [
"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
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/function_colors", "name": "Chart_Style/function_colors",
"type": TYPE_COLOR_ARRAY "type": TYPE_COLOR_ARRAY
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/box_color", "name": "Chart_Style/box_color",
"type": TYPE_COLOR "type": TYPE_COLOR
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/v_lines_color", "name": "Chart_Style/v_lines_color",
"type": TYPE_COLOR "type": TYPE_COLOR
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/h_lines_color", "name": "Chart_Style/h_lines_color",
"type": TYPE_COLOR "type": TYPE_COLOR
}, })
property_list.append(
{ {
"class_name": "Font", "class_name": "Font",
"hint": PROPERTY_HINT_RESOURCE_TYPE, "hint": PROPERTY_HINT_RESOURCE_TYPE,
@ -98,7 +167,8 @@ var property_list = [
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/font", "name": "Chart_Style/font",
"type": TYPE_OBJECT "type": TYPE_OBJECT
}, })
property_list.append(
{ {
"class_name": "Font", "class_name": "Font",
"hint": PROPERTY_HINT_RESOURCE_TYPE, "hint": PROPERTY_HINT_RESOURCE_TYPE,
@ -106,47 +176,98 @@ var property_list = [
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/bold_font", "name": "Chart_Style/bold_font",
"type": TYPE_OBJECT "type": TYPE_OBJECT
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/font_color", "name": "Chart_Style/font_color",
"type": TYPE_COLOR "type": TYPE_COLOR
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/use_template", "name": "Chart_Style/use_template",
"type": TYPE_BOOL "type": TYPE_BOOL
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_ENUM, "hint": PROPERTY_HINT_ENUM,
"hint_string": PoolStringArray(Utilities.templates.keys()).join(","), "hint_string": PoolStringArray(Utilities.templates.keys()).join(","),
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Style/template", "name": "Chart_Style/template",
"type": TYPE_INT "type": TYPE_INT
}, })
# Chart Modifiers # Chart Modifiers
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Modifiers/treshold", "name": "Chart_Modifiers/treshold",
"type": TYPE_VECTOR2 "type": TYPE_VECTOR2
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Modifiers/only_disp_values", "name": "Chart_Modifiers/only_disp_values",
"type": TYPE_VECTOR2 "type": TYPE_VECTOR2
}, })
property_list.append(
{ {
"hint": PROPERTY_HINT_NONE, "hint": PROPERTY_HINT_NONE,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE, "usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE,
"name": "Chart_Modifiers/invert_chart", "name": "Chart_Modifiers/invert_chart",
"type": TYPE_BOOL "type": TYPE_BOOL
}, })
]
func _set(property, value):
match property:
"Chart_Display/autoscale_x":
autoscale_x = value
build_property_list()
property_list_changed_notify()
return true
"Chart_Display/autoscale_y":
autoscale_y = value
build_property_list()
property_list_changed_notify()
return true
"Chart_Display/min_x_range":
x_range[0] = value
return true
"Chart_Display/max_x_range":
x_range[1] = value
return true
"Chart_Display/min_y_range":
y_range[0] = value
return true
"Chart_Display/max_y_range":
y_range[1] = value
return true
._set(property, value)
func _get(property):
match property:
"Chart_Display/autoscale_x":
return autoscale_x
"Chart_Display/autoscale_y":
return autoscale_y
"Chart_Display/min_x_range":
return x_range[0]
"Chart_Display/max_x_range":
return x_range[1]
"Chart_Display/min_y_range":
return y_range[0]
"Chart_Display/max_y_range":
return y_range[1]
._get(property)
@ -202,7 +323,6 @@ func plot_function(x:Array, y:Array, id=""):
plot() plot()
func update_function(x:Array, y:Array, id=""): func update_function(x:Array, y:Array, id=""):
var function = y_labels.find(id) var function = y_labels.find(id)
@ -326,8 +446,11 @@ func calculate_tics():
x_chors.clear() x_chors.clear()
# Chose the min/max from all functions # Chose the min/max from all functions
y_range = [y_domain[0].min() if not origin_at_zero else 0, y_domain[1].max()] if autoscale_x:
x_range = [x_domain[0].min() if not origin_at_zero else 0, x_domain[1].max()] x_range = [x_domain[0].min() if not origin_at_zero else 0, x_domain[1].max()]
if autoscale_y:
y_range = [y_domain[0].min() if not origin_at_zero else 0, y_domain[1].max()]
y_margin_min = y_range[0] y_margin_min = y_range[0]
var y_margin_max = y_range[1] var y_margin_max = y_range[1]