From 16e4fdd7b77ff22abfee7e576da0d4d2be6bf1f3 Mon Sep 17 00:00:00 2001 From: "Weng, Chia-Ling" Date: Fri, 13 Oct 2023 14:47:26 +0800 Subject: [PATCH 1/2] [Doc] Update Bar Chart Hightlighted Segment: 2 Thresholds --- .../bar_chart_with_highlighted_segment.py | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py b/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py index 09292fe55..fb073edae 100644 --- a/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py +++ b/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py @@ -1,7 +1,7 @@ """ Bar Chart with Highlighted Segment ---------------------------------- -This example shows a bar chart that highlights values beyond a threshold. +This example shows a bar chart that highlights values beyond two threshold. """ # category: bar charts import altair as alt @@ -9,23 +9,39 @@ from vega_datasets import data source = data.wheat() -threshold = pd.DataFrame([{"threshold": 90}]) +threshold = pd.DataFrame( + data={"threshold_value": [20, 60], "threshold_label": ["> 20", "> 60"]} + ) -bars = alt.Chart(source).mark_bar().encode( +bars = alt.Chart(source).mark_bar(color="#9CC8E2").encode( x="year:O", - y="wheat:Q", + y=alt.Y("wheat:Q",title="Wheat") ) -highlight = alt.Chart(source).mark_bar(color="#e45755").encode( +highlight = alt.Chart(source).mark_bar(color="#5BA3CF").encode( x='year:O', y='baseline:Q', y2='wheat:Q' ).transform_filter( - alt.datum.wheat > 90 -).transform_calculate("baseline", "90") + alt.datum.wheat > 20 +).transform_calculate("baseline", "20") + +highlight2 = alt.Chart(source).mark_bar(color="#2978B8").encode( + x='year:O', + y='baseline:Q', + y2='wheat:Q' +).transform_filter( + alt.datum.wheat > 60 +).transform_calculate("baseline", "60") rule = alt.Chart(threshold).mark_rule().encode( - y='threshold:Q' + y=alt.Y("threshold_value") +) + +label = rule.mark_text( + x=alt.expr('width'), align="right",dx=20, dy=-5, fontWeight="bold", fontSize=12 +).encode( + text="threshold_label", ) -(bars + highlight + rule).properties(width=600) +(bars + highlight + highlight2 + rule +label).properties(width=600) \ No newline at end of file From e443388d91f3e9c1aabbaa960767a59c918dc76d Mon Sep 17 00:00:00 2001 From: mattijn Date: Fri, 13 Oct 2023 09:39:49 +0200 Subject: [PATCH 2/2] use datum as baseline remove calculate transform --- .../bar_chart_with_highlighted_segment.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py b/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py index fb073edae..0322e0ce8 100644 --- a/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py +++ b/tests/examples_arguments_syntax/bar_chart_with_highlighted_segment.py @@ -20,19 +20,19 @@ highlight = alt.Chart(source).mark_bar(color="#5BA3CF").encode( x='year:O', - y='baseline:Q', - y2='wheat:Q' + y='wheat:Q', + y2=alt.datum(20) ).transform_filter( alt.datum.wheat > 20 -).transform_calculate("baseline", "20") +) highlight2 = alt.Chart(source).mark_bar(color="#2978B8").encode( x='year:O', - y='baseline:Q', - y2='wheat:Q' + y='wheat:Q', + y2=alt.datum(60) ).transform_filter( alt.datum.wheat > 60 -).transform_calculate("baseline", "60") +) rule = alt.Chart(threshold).mark_rule().encode( y=alt.Y("threshold_value")