diff --git a/vizro-core/examples/visual-vocabulary/custom_charts.py b/vizro-core/examples/visual-vocabulary/custom_charts.py index 52fb6c226..aee03a461 100644 --- a/vizro-core/examples/visual-vocabulary/custom_charts.py +++ b/vizro-core/examples/visual-vocabulary/custom_charts.py @@ -330,33 +330,25 @@ def lollipop(data_frame: pd.DataFrame, **kwargs): Returns: go.Figure: Lollipop chart. """ - # Should we allow keyword arguments (kwargs) in this context, given the presence of multiple traces? # Unlike the column_and_line chart, where all traces hold equal significance, here the traces differ in importance. # The primary scatter plot is the main trace, while the additional traces merely serve as connecting lines. - # Therefore, should we apply the kwargs solely to the scatter plot, as illustrated below? + # Therefore, should we apply the kwargs solely to the main scatter plot, as illustrated below? fig = px.scatter(data_frame, **kwargs) - x_array = fig.data[0].x - y_array = fig.data[0].y - - # Is it necessary to always enable orientation? Unlike bar charts, scatter plots do not have an - # orientation argument. The calculation of the trace below needs to adapt based on the orientation. - # However, since we can't predict the user's choice for x and y axes, I am not sure how we can dynamically update - # the orientation. This chart does not always have a categorical and a numerical column. In can also be - # with both numeric columns. Should we introduce this ourselves? Does this have to be part of this example - # or can we hard-code for the visual-vocabulary? + + # Enable for both orientations + orientation = fig.data[0].orientation + x_array = fig.data[0]["x"] + y_array = fig.data[0]["y"] + for i in range(len(data_frame)): fig.add_trace( go.Scatter( - x=[0, x_array[i]], - y=[y_array[i], y_array[i]], + x=[0, x_array[i]] if orientation == "h" else [x_array[i], x_array[i]], + y=[y_array[i], y_array[i]] if orientation == "h" else [0, y_array[i]], mode="lines", ) ) fig.update_traces(marker_size=12, line_width=3, line_color=fig.layout.template.layout.colorway[0]) - fig.update_layout(showlegend=False) - - # These are use-case specific layout updates. Normally not suitable for a chart inside vizro.charts, as these - # depend on the data context. - fig.update_layout(yaxis_title="", yaxis_showgrid=False) + fig.update_layout(showlegend=False, yaxis_title="", yaxis_showgrid=False) return fig diff --git a/vizro-core/examples/visual-vocabulary/pages/examples/lollipop.py b/vizro-core/examples/visual-vocabulary/pages/examples/lollipop.py index 38c5f6fb6..53129f6e5 100644 --- a/vizro-core/examples/visual-vocabulary/pages/examples/lollipop.py +++ b/vizro-core/examples/visual-vocabulary/pages/examples/lollipop.py @@ -10,21 +10,22 @@ def lollipop(data_frame: pd.DataFrame, **kwargs): """Creates a lollipop chart using Plotly.""" fig = px.scatter(data_frame, **kwargs) - x_array = fig.data[0].x - y_array = fig.data[0].y + + orientation = fig.data[0].orientation + x_array = fig.data[0]["x"] + y_array = fig.data[0]["y"] for i in range(len(data_frame)): fig.add_trace( go.Scatter( - x=[0, x_array[i]], - y=[y_array[i], y_array[i]], + x=[0, x_array[i]] if orientation == "h" else [x_array[i], x_array[i]], + y=[y_array[i], y_array[i]] if orientation == "h" else [0, y_array[i]], mode="lines", ) ) fig.update_traces(marker_size=12, line_width=3, line_color=fig.layout.template.layout.colorway[0]) - fig.update_layout(showlegend=False) - fig.update_layout(yaxis_title="", yaxis_showgrid=False) + fig.update_layout(showlegend=False, yaxis_title="", yaxis_showgrid=False) return fig