Skip to content

Commit ba751d8

Browse files
committed
Add axis labels feature to geonodes
1 parent f086b30 commit ba751d8

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed
15.9 KB
Binary file not shown.

data_vis/geonodes/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from .data import DV_DataProperties
1414
from .components import (
1515
DV_AddAxis,
16-
DV_AddHeading,
17-
DV_AddAxisLabel,
1816
DV_AddDataLabels,
1917
)
2018
from .animations import (
@@ -35,8 +33,6 @@
3533
DV_GN_SurfaceChart,
3634
DV_GN_PieChart,
3735
DV_AddAxis,
38-
DV_AddHeading,
39-
DV_AddAxisLabel,
4036
DV_AddDataLabels,
4137
DV_ChartPanel,
4238
DV_AxisPanel,

data_vis/geonodes/components.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def execute(self, context: bpy.types.Context):
156156
if self.axis_type == AxisType.CATEGORICAL:
157157
self._setup_categorical_axis(obj, mod)
158158

159+
self._add_labels_if_available(obj, mod)
160+
159161
modifier_utils.add_used_materials_to_object(mod, obj)
160162
return {"FINISHED"}
161163

@@ -180,12 +182,35 @@ def _setup_categorical_axis(
180182
assert is_chart(obj)
181183
data_from_obj = data.get_chart_data_info(obj)
182184
if data_from_obj is None:
183-
logger.error("No data found on the chart {obj.name}")
185+
logger.error(f"No data found on the chart {obj.name}")
184186
return
185187

186188
modifier_utils.set_input(mod, "Tick Count", len(data_from_obj["categories"]))
187189
modifier_utils.set_input(mod, "Labels", ",".join(data_from_obj["categories"]))
188190

191+
def _add_labels_if_available(
192+
self, obj: bpy.types.Object, mod: bpy.types.NodesModifier
193+
) -> None:
194+
assert is_chart(obj)
195+
data_from_obj = data.get_chart_data_info(obj)
196+
if data_from_obj is None:
197+
logger.error(f"No data found on the chart {obj.name}")
198+
return
199+
200+
axis_labels = data_from_obj["axis_labels"]
201+
if len(axis_labels) == 0:
202+
return
203+
if self.axis == "X" and len(axis_labels) > 0:
204+
modifier_utils.set_input(mod, "Axis Label Text", axis_labels[0])
205+
206+
if self.axis == "Y" and len(axis_labels) > 1:
207+
modifier_utils.set_input(mod, "Axis Label Text", axis_labels[1])
208+
209+
if self.axis == "Z" and len(axis_labels) == 2:
210+
modifier_utils.set_input(mod, "Axis Label Text", axis_labels[1])
211+
elif len(axis_labels) == 3:
212+
modifier_utils.set_input(mod, "Axis Label Text", axis_labels[2])
213+
189214

190215
@data_vis_logging.logged_operator
191216
class DV_AddDataLabels(bpy.types.Operator):
@@ -204,27 +229,3 @@ def execute(self, context: bpy.types.Context):
204229
mod.node_group = library.load_above_data_labels()
205230
modifier_utils.add_used_materials_to_object(mod, obj)
206231
return {"FINISHED"}
207-
208-
209-
# TODO: Create a object that's in the middle of the axis and parented to the chart object
210-
@data_vis_logging.logged_operator
211-
class DV_AddAxisLabel(bpy.types.Operator):
212-
bl_idname = "data_vis.add_axis_label"
213-
bl_label = "Add Axis Label"
214-
bl_options = {"REGISTER", "UNDO"}
215-
216-
# Adds a axis label to the selected chart
217-
def execute(self, context):
218-
return {"FINISHED"}
219-
220-
221-
# TODO: Add heading to the chart
222-
@data_vis_logging.logged_operator
223-
class DV_AddHeading(bpy.types.Operator):
224-
# Adds a heading to the selected chart
225-
bl_idname = "data_vis.add_heading"
226-
bl_label = "Add Heading"
227-
bl_options = {"REGISTER", "UNDO"}
228-
229-
def execute(self, context):
230-
return {"FINISHED"}

data_vis/geonodes/data.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class PreprocessedData:
135135
ws: np.ndarray | None = None
136136
z_ns: np.ndarray | None = None
137137
categories: np.ndarray | None = None
138+
axis_labels: typing.List[str] = dataclasses.field(default_factory=list)
138139

139140

140141
def _store_chart_data_info(
@@ -144,8 +145,11 @@ def _store_chart_data_info(
144145
"data_type": data_type,
145146
"shape": verts.shape,
146147
}
147-
if data is not None and data.categories is not None:
148-
data_dict["categories"] = data.categories.tolist()
148+
if data is not None:
149+
if data.categories is not None:
150+
data_dict["categories"] = data.categories.tolist()
151+
if data.axis_labels is not None:
152+
data_dict["axis_labels"] = data.axis_labels
149153

150154
obj[DATA_TYPE_PROPERTY] = json.dumps(data_dict)
151155

@@ -205,7 +209,7 @@ def _preprocess_data(data, data_type: str) -> PreprocessedData:
205209
else:
206210
raise RuntimeError(f"Unknown DataType {data_type}")
207211

208-
return PreprocessedData(vert_positions, ws, z_ns, categories)
212+
return PreprocessedData(vert_positions, ws, z_ns, categories, axis_labels=[])
209213

210214

211215
@dataclasses.dataclass
@@ -220,7 +224,9 @@ def _convert_data_to_geometry(
220224
connect_edges: bool = False,
221225
interpolation_config: InterpolationConfig | None = None,
222226
) -> tuple[list, list, list, PreprocessedData]:
223-
data = _preprocess_data(DataManager().get_chart_data().parsed_data, data_type)
227+
chart_data = DataManager().get_chart_data()
228+
data = _preprocess_data(chart_data.parsed_data, data_type)
229+
data.axis_labels = chart_data.labels
224230
verts = []
225231
edges = []
226232
faces = []

0 commit comments

Comments
 (0)