Skip to content

Commit

Permalink
Fix lollipop
Browse files Browse the repository at this point in the history
  • Loading branch information
huong-li-nguyen committed Nov 15, 2024
1 parent 7f447da commit 8545946
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
28 changes: 10 additions & 18 deletions vizro-core/examples/visual-vocabulary/custom_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions vizro-core/examples/visual-vocabulary/pages/examples/lollipop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 8545946

Please sign in to comment.