Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
yonkmanjl committed Oct 4, 2024
1 parent 4b10fb7 commit 9f71883
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 112 deletions.
120 changes: 60 additions & 60 deletions vizro-core/examples/visual-vocabulary/README.md

Large diffs are not rendered by default.

43 changes: 27 additions & 16 deletions vizro-core/examples/visual-vocabulary/custom_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,26 @@ def categorical_column(data_frame: pd.DataFrame, x: str, y: str):
fig.update_xaxes(type="category")
return fig


def offset_signal(signal: float, marker_offset: float):
"""Offsets the signal value by the marker_offset, reducing for positive signal values
and increasing for negative values. Used to reduce the length of lines on lollipop charts
to end just under the dot """
"""Offsets a signal value by marker_offset.
Reduces for positive signal values and increasing for negative values. Used to reduce the length of
lines on lollipop charts to end just before the dot.
Args:
signal (float): the value to be updated.
marker_offset (float): the offset to be added/subtracted.
Returns:
float: the updated value.
"""
if abs(signal) <= marker_offset:
return 0
return signal - marker_offset if signal > 0 else signal + marker_offset


@capture("graph")
def lollipop(data_frame: pd.DataFrame, x: str, y: str, y_offset: float):
"""Creates a lollipop chart using Plotly.
Expand All @@ -171,18 +183,17 @@ def lollipop(data_frame: pd.DataFrame, x: str, y: str, y_offset: float):

shapes = []
for i, row in data_frame.iterrows():
shapes.append(dict(
type='line',
xref='x',
yref='y',
x0=row[x],
y0=0,
x1=row[x],
y1=offset_signal(row[y], y_offset),
line=dict(
color='grey',
width=2
)
))
shapes.append(
{
"type": "line",
"xref": "x",
"yref": "y",
"x0": row[x],
"y0": 0,
"x1": row[x],
"y1": offset_signal(row[y], y_offset),
"line": {"color": "grey", "width": 2},
}
)
fig = data.update_layout(shapes=shapes)
return fig
19 changes: 8 additions & 11 deletions vizro-core/examples/visual-vocabulary/pages/_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def column_and_line_factory(group: str):
],
)


def lollipop_factory(group: str):
"""Reusable function to create the page content for the lollipop chart with a unique ID."""
return vm.Page(
Expand All @@ -123,25 +124,21 @@ def lollipop_factory(group: str):
text="""
#### What is a lollipop chart?
A lollipop chart is a type of chart that combines elements of a bar chart and a scatter plot. And a line extends from the
x-axis to a dot, which marks the value for that category. This makes it easy to compare different categories visually.
A lollipop chart is a type of chart that combines elementsof a bar chart and a scatter plot.
A line extends from the x-axis to a dot, which marks the value for that category. This makes
it easy to compare different categories visually.
&nbsp;
#### When should I use it?
Use a lollipop chart when you want to highlight individual data points and make comparisons across categories. It’s
particularly useful for displaying ranking or distribution data, and it can be more visually appealing and easier to
read than traditional bar charts.
Use a lollipop chart when you want to highlight individual data points and make comparisons across
categories. It is particularly useful for displaying ranking or distribution data, and it can be more
visually appealing and easier to read than traditional bar charts.
"""
),
vm.Graph(
figure=lollipop(
gapminder.query("year == 2007 and gdpPercap > 36000"),
'country',
'gdpPercap',
5.0
)
figure=lollipop(gapminder.query("year == 2007 and gdpPercap > 36000"), "country", "gdpPercap", 5.0)
),
make_code_clipboard_from_py_file("lollipop.py"),
],
Expand Down
55 changes: 32 additions & 23 deletions vizro-core/examples/visual-vocabulary/pages/examples/lollipop.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import pandas as pd
import vizro.models as vm
import vizro.plotly.express as px
import pandas as pd

from vizro.models.types import capture
from vizro import Vizro
from vizro.models.types import capture


def offset_signal(signal: float, marker_offset: float):
"""Offsets the signal value by the marker_offset, reducing for positive signal values
and increasing for negative values. Used to reduce the length of lines on lollipop charts
to end just under the dot """
"""Offsets a signal value by marker_offset.
Reduces for positive signal values and increasing for negative values. Used to reduce the length of
lines on lollipop charts to end just before the dot.
Args:
signal (float): the value to be updated.
marker_offset (float): the offset to be added/subtracted.
Returns:
float: the updated value.
"""
if abs(signal) <= marker_offset:
return 0
return signal - marker_offset if signal > 0 else signal + marker_offset


@capture("graph")
def lollipop(data_frame: pd.DataFrame, x: str, y: str, y_offset: float):
"""Creates a lollipop chart using Plotly.
Expand All @@ -33,32 +44,30 @@ def lollipop(data_frame: pd.DataFrame, x: str, y: str, y_offset: float):

shapes = []
for i, row in data_frame.iterrows():
shapes.append(dict(
type='line',
xref='x',
yref='y',
x0=row[x],
y0=0,
x1=row[x],
y1=offset_signal(row[y], y_offset),
line=dict(
color='grey',
width=2
)
))
shapes.append(
{
"type": "line",
"xref": "x",
"yref": "y",
"x0": row[x],
"y0": 0,
"x1": row[x],
"y1": offset_signal(row[y], y_offset),
"line": {"color": "grey", "width": 2},
}
)
fig = data.update_layout(shapes=shapes)
return fig


gapminder = px.data.gapminder()
df = gapminder.query("year == 2007 and gdpPercap > 36000")
marker_offset = 5.0

page = vm.Page(
title="Lollipop",
components=[
vm.Graph(figure=lollipop(df, 'country', 'gdpPercap', marker_offset))
],
components=[vm.Graph(figure=lollipop(df, "country", "gdpPercap", marker_offset))],
)

dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()
Vizro().build(dashboard).run()
2 changes: 1 addition & 1 deletion vizro-core/examples/visual-vocabulary/pages/magnitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import vizro.models as vm
import vizro.plotly.express as px

from pages._pages_utils import PAGE_GRID, gapminder, iris, make_code_clipboard_from_py_file, tips
from pages._factories import lollipop_factory
from pages._pages_utils import PAGE_GRID, gapminder, iris, make_code_clipboard_from_py_file, tips

bar = vm.Page(
title="Bar",
Expand Down
2 changes: 1 addition & 1 deletion vizro-core/examples/visual-vocabulary/pages/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import vizro.models as vm
import vizro.plotly.express as px

from pages._pages_utils import PAGE_GRID, gapminder, make_code_clipboard_from_py_file
from pages._factories import lollipop_factory
from pages._pages_utils import PAGE_GRID, gapminder, make_code_clipboard_from_py_file

ordered_bar = vm.Page(
title="Ordered bar",
Expand Down

0 comments on commit 9f71883

Please sign in to comment.