Skip to content

Commit 8b388c1

Browse files
authored
Merge pull request #566 from ds26gte/dot-chart-repeated-string-args
categorical dot-chart() takes a list of (possibly repeated) strings only (not list of strings and a list of counts)
2 parents 531973c + 28a3f77 commit 8b388c1

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/web/arr/trove/chart.arr

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,28 +1964,38 @@ fun labeled-num-dot-chart-from-list(labels :: P.LoS, x-values :: P.LoN) -> DataS
19641964
} ^ scatter-plot-series
19651965
end
19661966

1967-
fun dot-chart-from-list(labels :: P.LoS, values :: P.LoN) -> DataSeries block:
1967+
fun dot-chart-from-list(input-labels :: P.LoS) -> DataSeries block:
19681968
doc: ```
1969-
Consume labels, a list of string, and values, a list of numbers
1970-
and construct a dot chart
1969+
Consume a list of string-values and construct a dot chart
19711970
```
1972-
# Type Checking
1973-
values.each(check-num)
1974-
labels.each(check-string)
1975-
1976-
# Constants
1977-
label-length = labels.length()
1978-
value-length = values.length()
1979-
rational-values = map(num-to-rational, values)
19801971

19811972
# Edge Case Error Checking
1982-
when value-length == 0:
1973+
when input-labels.length() == 0:
19831974
raise("dot-chart: can't have empty data")
19841975
end
1985-
when label-length <> value-length:
1986-
raise('dot-chart: labels and values should have the same length')
1987-
end
19881976

1977+
# Type Checking
1978+
input-labels.each(check-string)
1979+
1980+
# Walk through the (sorted) values, creating lists of labels and counts
1981+
unique-counts = foldl(
1982+
lam(acc, elt):
1983+
labels = acc.{0}
1984+
counts = acc.{1}
1985+
if labels.member(elt):
1986+
{labels; counts.set(0, counts.get(0) + 1)}
1987+
else:
1988+
{link(elt, labels); link(1, counts)}
1989+
end
1990+
end,
1991+
{[list: ]; [list: ]},
1992+
input-labels.sort())
1993+
1994+
labels = unique-counts.{0}
1995+
values = unique-counts.{1}
1996+
rational-values = map(num-to-rational, values)
1997+
1998+
# set the vAxis values, and create the data series
19891999
{max-positive-height; max-negative-height} = prep-axis(rational-values)
19902000

19912001
data-series = default-bar-chart-series.{

test-util/pyret-programs/charts/dot-chart-test.arr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import color as C
44
# include image-structs
55
include math
66

7-
labels = [list: "cats", "dogs", "ants", "elephants"]
8-
count = [list: 3, 7, 4, 9]
7+
labels = [list: "cats", "cats", "cats",
8+
"dogs", "dogs", "dogs", "dogs", "dogs", "dogs", "dogs",
9+
"ants", "ants", "ants", "ants",
10+
"elephants", "elephants", "elephants", "elephants", "elephants", "elephants", "elephants", "elephants", "elephants"]
911

10-
zoo-series = from-list.dot-chart(labels, count)
12+
zoo-series = from-list.dot-chart(labels)
1113

1214
just-red = [list: C.red]
1315
rainbow-colors = [list: C.red, C.orange, C.yellow, C.green, C.blue, C.indigo, C.violet]

0 commit comments

Comments
 (0)