Skip to content

Commit

Permalink
Merge pull request #75 from WarwickCIM/second-attempt-y-axis-trouble
Browse files Browse the repository at this point in the history
Share y axis between "density" and "colours used" plot, but not main plot
  • Loading branch information
edwardchalstrey1 authored Dec 2, 2021
2 parents ddeba30 + 54e63a2 commit 604d83b
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 34 deletions.
12 changes: 6 additions & 6 deletions tests/expected_meta_hist_choropleth.json
Original file line number Diff line number Diff line change
Expand Up @@ -363743,6 +363743,10 @@
"maxbins": 100
},
"field": "pct_estimate",
"scale": {
"nice": true,
"zero": false
},
"title": "",
"type": "quantitative"
}
Expand Down Expand Up @@ -363801,6 +363805,7 @@
},
"field": "y",
"scale": {
"nice": true,
"zero": false
},
"title": "",
Expand Down Expand Up @@ -363861,10 +363866,5 @@
"title": "test_choropleth",
"width": 500
}
],
"resolve": {
"scale": {
"y": "shared"
}
}
]
}
2 changes: 1 addition & 1 deletion tests/expected_meta_hist_choropleth.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 15 additions & 6 deletions tests/expected_meta_hist_choropleth_extent.json
Original file line number Diff line number Diff line change
Expand Up @@ -363747,6 +363747,13 @@
"maxbins": 100
},
"field": "pct_estimate",
"scale": {
"domain": [
0,
100
],
"nice": true
},
"title": "",
"type": "quantitative"
}
Expand Down Expand Up @@ -363808,6 +363815,13 @@
"orient": "right"
},
"field": "y",
"scale": {
"domain": [
0,
100
],
"nice": true
},
"title": "",
"type": "quantitative"
},
Expand Down Expand Up @@ -363874,10 +363888,5 @@
"title": "test_choropleth_extent",
"width": 500
}
],
"resolve": {
"scale": {
"y": "shared"
}
}
]
}
2 changes: 1 addition & 1 deletion tests/expected_meta_hist_choropleth_extent.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 2 additions & 6 deletions tests/expected_meta_hist_scatterplot.json
Original file line number Diff line number Diff line change
Expand Up @@ -57721,6 +57721,7 @@
},
"field": "y",
"scale": {
"nice": true,
"zero": false
},
"title": "",
Expand Down Expand Up @@ -57774,10 +57775,5 @@
"mark": "circle",
"title": "test_scatterplot"
}
],
"resolve": {
"scale": {
"y": "shared"
}
}
]
}
2 changes: 1 addition & 1 deletion tests/expected_meta_hist_scatterplot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 1 addition & 9 deletions tests/expected_meta_hist_scatterplot_bin_False.json
Original file line number Diff line number Diff line change
Expand Up @@ -57715,9 +57715,6 @@
"orient": "right"
},
"field": "y",
"scale": {
"zero": false
},
"title": "",
"type": "quantitative"
},
Expand Down Expand Up @@ -57754,10 +57751,5 @@
"mark": "circle",
"title": "test_scatterplot_bin_False"
}
],
"resolve": {
"scale": {
"y": "shared"
}
}
]
}
2 changes: 1 addition & 1 deletion tests/expected_meta_hist_scatterplot_bin_False.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 12 additions & 3 deletions ways_py/ways.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ def field(src: alt.Chart) -> str:
@staticmethod
def density_chart(src: alt.Chart) -> alt.Chart:
if src.encoding.color.bin and is_defined(src.encoding.color.bin.extent):
bin = alt.Bin(maxbins=100, extent=src.encoding.color.bin.extent)
extent = src.encoding.color.bin.extent
bin = alt.Bin(maxbins=100, extent=extent)
y_scale = alt.Scale(domain=extent, nice=True)
else:
bin = alt.Bin(maxbins=100)
y_scale = alt.Scale(zero=False, nice=True)
ys = src.data[Ways.field(src)] # assume src.data array-like in an appropriate way
y_min, y_max = min(ys), max(ys)
# tickCount/tickMinStep Axis properties are ignored (perhaps because we specify bins), so hard code
Expand All @@ -35,6 +38,7 @@ def density_chart(src: alt.Chart) -> alt.Chart:
bin=bin,
axis=alt.Axis(orient='left', grid=False, values=sorted([0, 50] + [y_min, y_max])),
title="",
scale=y_scale
)
x_axis = alt.X(
'sum(proportion):Q',
Expand All @@ -53,13 +57,18 @@ def density_chart(src: alt.Chart) -> alt.Chart:
def used_colours(src: alt.Chart) -> alt.Chart:
y_axis = alt.Axis(orient='right', grid=False)
x_axis = alt.Axis(labels=False, tickSize=0, grid=False, titleAngle=270, titleAlign='right')
if src.encoding.color.bin and is_defined(src.encoding.color.bin.extent):
extent = src.encoding.color.bin.extent
y_scale = alt.Scale(domain=extent, nice=True)
else:
y_scale = alt.Scale(zero=False, nice=True)
if src.encoding.color.bin:
chart = alt.Chart(src.data) \
.mark_rect() \
.transform_bin(as_=['y', 'y2'], bin=src.encoding.color.bin, field=Ways.field(src)) \
.transform_calculate(x='5') \
.encode(
y=alt.Y('y:Q', axis=y_axis, title=""),
y=alt.Y('y:Q', axis=y_axis, title="", scale=y_scale),
y2='y2:Q',
x=alt.X('x:Q', sort='descending', axis=x_axis, title="colours used")
) # noqa: E123
Expand Down Expand Up @@ -92,7 +101,7 @@ def altair_meta_hist(src: alt.Chart) -> alt.Chart:
if not is_defined(src.encoding.color.bin):
raise Exception("Can only apply decorator to chart with color.bin defined.")

meta_chart: alt.Chart = (Ways.density_chart(src) | Ways.used_colours(src)).resolve_scale(y='shared')
meta_chart: alt.Chart = (Ways.density_chart(src) | Ways.used_colours(src))
return (meta_chart | src) \
.configure_view(strokeWidth=0) \
.configure_concat(spacing=5)
Expand Down

0 comments on commit 604d83b

Please sign in to comment.