-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpresentation-context.ls
More file actions
150 lines (132 loc) · 5.55 KB
/
presentation-context.ls
File metadata and controls
150 lines (132 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# the first require is used by browserify to import the prelude-ls module
# the second require is defined in the prelude-ls module and exports the object
require \prelude-ls
{Obj, Str, id, any, average, concat-map, drop, each, filter, find, foldr1, foldl,
map, maximum, minimum, obj-to-pairs, pairs-to-obj, sort, sum, tail, take, unique} = require \prelude-ls
# nvd3 requires d3 to be in global space
d3 = window.d3 = require \d3
if typeof global != \undefined
global.d3 = d3
nv = require \nvd3
require! \d3-tip
d3-tip d3
{fill-intervals, rextend} = require \./plottables/_utils
$ = require \jquery-browserify
# Plottable is a monad, run it by plot funciton
class Plottable
(@plotter, @options = {}, @continuations = ((..., callback) -> callback null), @projection = id) ->
_plotter: (view, result, parameters) ~>
options =
| typeof @options == \function => @options parameters
| _ => @options
@plotter view, (@projection result, options), options, @continuations
# Wraps f in a a Plottable
# plottable :: (View -> result -> {}:options -> IO ()) -> Plottable
plottable = (f) ->
new Plottable (view, result, options) !-->
f view, result, options
# Runs a Plottable
# plot :: Plottable -> DOMElement -> result -> parameters -> DOM()
plot = (p, view, result, parameters) -->
p._plotter view, result, parameters
# Attaches options to a Plottable
# with-options :: Plottable -> Either object, (Parameters -> object) -> Plottable
with-options = (p, f) ->
new-options =
| typeof f == \function =>
(parameters) ->
current-options = if typeof p.options == \function then p.options parameters else p.options
({} `rextend` current-options) `rextend` (f parameters)
| _ => ({} `rextend` p.options) `rextend` f
new Plottable do
p.plotter
new-options
p.continuations
p.projection
# more :: Plottable -> (... -> (Error? -> Void)) -> Plottable
more = (p, c) ->
new Plottable do
p.plotter
{} `rextend` p.options
(...init, callback) ->
try
c ...init
catch ex
return callback ex
callback null
p.projection
# projects the data of a Plottable with f
# project :: (result -> options -> result) -> Plottable -> Plottable
project = (f, p) -->
new Plottable do
p.plotter
{} `rextend` p.options
p.continuations
(result, options) -> p.projection (f result, options), options
# json :: View -> result -> DOM()
json = (view, result) ->
view.innerHTML = "<pre>#{JSON.stringify result, null, 4}</pre>"
# csv :: View -> result -> DOM()
csv = (view, result) ->
columns = Obj.keys result.0
(columns.join \,) + "\n" + do ->
result
|> foldl do
(acc, row) ->
acc.push do
columns
|> map (column) -> row[column]
|> Str.join \,
acc
[]
|> Str.join "\n"
pre = $ "<pre/>"
..text json-to-csv result
($ view).append pre
# plot-chart :: View -> result -> Chart -> DOM()
plot-chart = (view, result, chart) !->
d3.select view
.append \div .attr \style, "position: absolute; left: 0px; top: 0px; width: 100%; height: 100%"
.append \svg .datum result .call chart
plottables = {
pjson: new Plottable do
(view, result, {pretty, space}, continuation) !-->
pre = $ "<pre/>"
..html if not pretty then JSON.stringify result else JSON.stringify result, null, space
($ view).append pre
{pretty: true, space: 4}
table: (require \./plottables/table) {Plottable, d3, nv, plot-chart, plot}
histogram1: (require \./plottables/histogram1) {Plottable, d3, nv, plot-chart, plot}
histogram: (require \./plottables/histogram) {Plottable, d3, nv, plot-chart, plot}
stacked-area: (require \./plottables/stacked-area) {Plottable, nv, d3, plot-chart, plot}
scatter1: (require \./plottables/scatter1) {Plottable, d3, nv, plot-chart, plot}
scatter: (require \./plottables/scatter) {Plottable, d3, nv, plot-chart, plot}
correlation-matrix: (require \./plottables/correlation-matrix) {Plottable, d3, nv, plot-chart, plot}
regression: (require \./plottables/regression) {Plottable, d3, nv, plot-chart, plot}
timeseries1: (require \./plottables/timeseries1) {Plottable, d3, nv, plot-chart, plot}
timeseries: (require \./plottables/timeseries) {Plottable, d3, nv, plot-chart, plot}
multi-bar-horizontal: (require \./plottables/multi-bar-horizontal) {Plottable, d3, nv, plot-chart, plot}
heatmap: (require \./plottables/heatmap) {Plottable, d3, plot}
multi-chart: (require \./plottables/multi-chart) {Plottable, d3, nv, plot-chart, plot}
funnel: (require \./plottables/funnel) {Plottable, d3, nv, plot-chart, plot}
funnel1: (require \./plottables/funnel1) {Plottable, d3}
radar: (require \./plottables/radar) {Plottable, d3}
}
{layout-horizontal, layout-vertical} = layout-plottables = (require \./plottables/layout) {Plottable, d3, nv, plot-chart, plot}
..layout-horizontal = -> (layout-horizontal ...) `with-options` id
..layout-vertical = -> (layout-vertical ...) `with-options` id
# all functions defined here are accessibly by the presentation code
module.exports = ->
{} <<< plottables <<< layout-plottables <<< {
Plottable
plot
plottable
with-options
more
project
json
csv
fill-intervals
React: require \react
ReactDOM: require \react-dom
} <<< (require \prelude-ls)