Skip to content

Commit 51c73f8

Browse files
author
Ben Lerner
committed
feature request: x-min and x-max for dot plots
using these options can _only widen_ the domain of the chart; they will throw an error when rendering a chart whose data lie outside that range.
1 parent c6625b2 commit 51c73f8

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/arr/trove/charts.arr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import string-dict as SD
2727
import valueskeleton as VS
2828
import statistics as ST
2929
import color as C
30+
import error as ERR
3031
import render-error-display as RED
3132

3233
################################################################################
@@ -1389,13 +1390,17 @@ type DotChartWindowObject = {
13891390
y-axis :: String,
13901391
x-axis-type :: AxisType,
13911392
y-axis-type :: AxisType,
1393+
x-min :: Option<Number>,
1394+
x-max :: Option<Number>,
13921395
}
13931396

13941397
default-dot-chart-window-object :: DotChartWindowObject = default-chart-window-object.{
13951398
x-axis: '',
13961399
y-axis: '',
13971400
x-axis-type: at-linear,
13981401
y-axis-type: at-linear,
1402+
x-min: none,
1403+
x-max: none,
13991404
}
14001405

14011406
type BarChartWindowObject = {
@@ -1734,6 +1739,8 @@ data ChartWindow:
17341739
constr: {(): dot-chart-window},
17351740
x-axis: x-axis-method,
17361741
y-axis: y-axis-method,
1742+
x-min: x-min-method,
1743+
x-max: x-max-method,
17371744
x-axis-type: x-axis-type-method,
17381745
y-axis-type: y-axis-type-method,
17391746
| box-plot-chart-window(obj :: BoxChartWindowObject) with:
@@ -2449,6 +2456,16 @@ fun check-render-y-axis(self) -> Nothing:
24492456
end
24502457
end
24512458

2459+
fun check-data-range(min, max, vals) -> Nothing:
2460+
fun too-small(v): v.value < min.or-else(v.value) end
2461+
fun too-big(v): v.value > max.or-else(v.value) end
2462+
if vals.any(too-small) or vals.any(too-big):
2463+
raise(ERR.message-exception("render: All values must be between specified x-min and x-max bounds"))
2464+
else:
2465+
nothing
2466+
end
2467+
end
2468+
24522469
fun render-chart(s :: DataSeries) -> ChartWindow:
24532470
doc: 'Render it!'
24542471
cases (DataSeries) s:
@@ -2459,6 +2476,8 @@ fun render-chart(s :: DataSeries) -> ChartWindow:
24592476
| dot-plot-series(obj) =>
24602477
default-dot-chart-window-object.{
24612478
method render(self) block:
2479+
_ = check-render-x-axis(self)
2480+
_ = check-data-range(self.x-min, self.x-max, obj.ps)
24622481
CL.dot-chart(self, obj)
24632482
end
24642483
} ^ dot-chart-window

src/js/trove/charts-lib.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,8 @@
18091809
const height = globalOptions['height'];
18101810
const xAxisLabel = globalOptions['x-axis'];
18111811
const yAxisLabel = globalOptions['y-axis'];
1812+
const xMinValue = getNumOrDefault(globalOptions['x-min'], undefined);
1813+
const xMaxValue = getNumOrDefault(globalOptions['x-max'], undefined);
18121814
const yAxisType = globalOptions['y-axis-type'];
18131815
const background = getColorOrDefault(globalOptions['backgroundColor'], 'transparent');
18141816

@@ -1861,15 +1863,18 @@
18611863
{ name: 'binSize', update: 'invert("binScale", dotSize)' },
18621864
{ name: 'actualDotSize', update: 'scale("dotScale", 0) - scale("dotScale", 1)' },
18631865
{ name: 'headspace', value: '0.25' },
1864-
{ name: 'wrapMaxY', update: 'floor(domain("dotScale")[1] * (1 - headspace))' }
1866+
{ name: 'wrapMaxY', update: 'floor(domain("dotScale")[1] * (1 - headspace))' },
1867+
{ name: 'xMinValue', value: xMinValue },
1868+
{ name: 'xMaxValue', value: xMaxValue },
18651869
];
18661870
const scales = [
18671871
{
18681872
name: 'binScale',
18691873
type: 'linear',
18701874
zero: false,
18711875
range: { signal: '[0, width - dotSize / 2]' },
1872-
domain: { data: 'rawTable', field: 'value' }
1876+
domain: { data: 'rawTable', field: 'value' },
1877+
domainMin: { signal: 'xMinValue' }, domainMax: { signal: 'xMaxValue' },
18731878
},
18741879
{
18751880
name: 'dotScale',
@@ -2171,8 +2176,6 @@
21712176
const trendlineWidth = toFixnum(get(rawData, 'trendlineWidth'));
21722177
const trendlineOpacity = toFixnum(get(rawData, 'trendlineOpacity'));
21732178
const trendlineDegree = toFixnum(get(rawData, 'trendlineDegree'));
2174-
const xMinValue = getNumOrDefault(globalOptions['x-min'], undefined);
2175-
const xMaxValue = getNumOrDefault(globalOptions['x-max'], undefined);
21762179
const yMinValue = getNumOrDefault(globalOptions['y-min'], undefined);
21772180
const yMaxValue = getNumOrDefault(globalOptions['y-max'], undefined);
21782181
const imageScaleFactorX = autosizeImage ? '-datum.imageWidth' : -pointSize;

0 commit comments

Comments
 (0)