@plotdb/chart 0.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +3 -3
  2. package/dist/chart.js +18 -12
  3. package/dist/chart.min.js +1 -1
  4. package/dist/index.css +8 -0
  5. package/dist/index.js +609 -0
  6. package/dist/index.min.css +1 -0
  7. package/dist/index.min.js +1 -0
  8. package/dist/utils/axis.js +6 -0
  9. package/dist/utils/axis.min.js +1 -1
  10. package/package.json +22 -11
  11. package/CHANGELOG.md +0 -11
  12. package/build +0 -39
  13. package/doc/charts.md +0 -58
  14. package/doc/config.md +0 -44
  15. package/doc/draft/chart.md +0 -146
  16. package/doc/draft/data.md +0 -8
  17. package/doc/draft/definition.md +0 -0
  18. package/doc/draft/events.md +0 -0
  19. package/doc/draft/filter.md +0 -0
  20. package/doc/draft/lifecycle.md +0 -89
  21. package/doc/spec.md +0 -89
  22. package/doc/todo.md +0 -27
  23. package/doc/utils/tip.md +0 -84
  24. package/src/chart.ls +0 -320
  25. package/src/chart.styl +0 -6
  26. package/src/data/en-US/category +0 -20
  27. package/src/data/en-US/fruit +0 -39
  28. package/src/data/en-US/movie +0 -125
  29. package/src/data/en-US/name +0 -58
  30. package/src/data/index.ls +0 -34
  31. package/src/data/zh-TW/category +0 -20
  32. package/src/data/zh-TW/fruits +0 -38
  33. package/src/data/zh-TW/movie +0 -125
  34. package/src/data/zh-TW/names +0 -105
  35. package/src/utils/axis.ls +0 -229
  36. package/src/utils/config.ls +0 -11
  37. package/src/utils/data.ls +0 -104
  38. package/src/utils/format.ls +0 -30
  39. package/src/utils/legend.ls +0 -134
  40. package/src/utils/legend.styl +0 -37
  41. package/src/utils/tint.ls +0 -66
  42. package/src/utils/tip.ls +0 -97
  43. package/web/src/pug/base.pug +0 -19
  44. package/web/src/pug/block/axis/0.0.1/index.ls +0 -38
  45. package/web/src/pug/block/axis/0.0.1/index.pug +0 -1
  46. package/web/src/pug/block/axis-bubble/0.0.1/index.ls +0 -141
  47. package/web/src/pug/block/axis-bubble/0.0.1/index.pug +0 -1
  48. package/web/src/pug/block/bar/0.0.1/index.ls +0 -274
  49. package/web/src/pug/block/bar/0.0.1/index.pug +0 -35
  50. package/web/src/pug/block/bar-list/0.0.1/index.ls +0 -85
  51. package/web/src/pug/block/bar-list/0.0.1/index.pug +0 -30
  52. package/web/src/pug/block/bar-race/0.0.1/index.ls +0 -83
  53. package/web/src/pug/block/bar-race/0.0.1/index.pug +0 -21
  54. package/web/src/pug/block/base/0.0.1/index.ls +0 -67
  55. package/web/src/pug/block/base/0.0.1/index.pug +0 -5
  56. package/web/src/pug/block/blank/0.0.1/index.ls +0 -25
  57. package/web/src/pug/block/blank/0.0.1/index.pug +0 -1
  58. package/web/src/pug/block/bubble/0.0.1/index.ls +0 -220
  59. package/web/src/pug/block/bubble/0.0.1/index.pug +0 -28
  60. package/web/src/pug/block/curve-bar/0.0.1/index.ls +0 -89
  61. package/web/src/pug/block/curve-bar/0.0.1/index.pug +0 -16
  62. package/web/src/pug/block/curve-bar/0.0.1/sample.js +0 -338
  63. package/web/src/pug/block/dumbbell/0.0.1/index.ls +0 -90
  64. package/web/src/pug/block/dumbbell/0.0.1/index.pug +0 -1
  65. package/web/src/pug/block/empty/0.0.1/index.ls +0 -27
  66. package/web/src/pug/block/empty/0.0.1/index.pug +0 -2
  67. package/web/src/pug/block/gauge/0.0.1/index.ls +0 -100
  68. package/web/src/pug/block/gauge/0.0.1/index.pug +0 -31
  69. package/web/src/pug/block/gauge/0.0.1/sample.js +0 -338
  70. package/web/src/pug/block/line/0.0.1/index.ls +0 -265
  71. package/web/src/pug/block/line/0.0.1/index.pug +0 -33
  72. package/web/src/pug/block/number/0.0.1/index.ls +0 -69
  73. package/web/src/pug/block/number/0.0.1/index.pug +0 -20
  74. package/web/src/pug/block/percent-list/0.0.1/index.ls +0 -167
  75. package/web/src/pug/block/percent-list/0.0.1/index.pug +0 -34
  76. package/web/src/pug/block/pie/0.0.1/index.ls +0 -125
  77. package/web/src/pug/block/pie/0.0.1/index.pug +0 -19
  78. package/web/src/pug/block/pie/0.0.1/sample.js +0 -338
  79. package/web/src/pug/block/pie-bubble/0.0.1/index.ls +0 -185
  80. package/web/src/pug/block/pie-bubble/0.0.1/index.pug +0 -40
  81. package/web/src/pug/block/radar/0.0.1/index.ls +0 -113
  82. package/web/src/pug/block/radar/0.0.1/index.pug +0 -1
  83. package/web/src/pug/block/radar/0.0.1/xx +0 -285
  84. package/web/src/pug/block/scatter/0.0.1/index.ls +0 -128
  85. package/web/src/pug/block/scatter/0.0.1/index.pug +0 -30
  86. package/web/src/pug/block/sheet/0.0.1/index.ls +0 -38
  87. package/web/src/pug/block/sheet/0.0.1/index.pug +0 -5
  88. package/web/src/pug/block/table/0.0.1/index.ls +0 -40
  89. package/web/src/pug/block/table/0.0.1/index.pug +0 -3
  90. package/web/src/pug/block/taiwantown/0.0.1/index.ls +0 -48
  91. package/web/src/pug/block/taiwantown/0.0.1/index.pug +0 -52
  92. package/web/src/pug/block/taiwanvillage/0.0.1/index.ls +0 -53
  93. package/web/src/pug/block/taiwanvillage/0.0.1/index.pug +0 -52
  94. package/web/src/pug/block/treemap/0.0.1/index.ls +0 -172
  95. package/web/src/pug/block/treemap/0.0.1/index.pug +0 -42
  96. package/web/src/pug/block/voronoi-treemap/0.0.1/index.ls +0 -174
  97. package/web/src/pug/block/voronoi-treemap/0.0.1/index.pug +0 -17
  98. package/web/src/pug/block/voronoi-treemap/0.0.1/sample.js +0 -241
  99. package/web/src/pug/block/wordcloud/0.0.1/index.ls +0 -61
  100. package/web/src/pug/block/wordcloud/0.0.1/index.pug +0 -1
  101. package/web/src/pug/block/worldmap/0.0.1/index.ls +0 -83
  102. package/web/src/pug/block/worldmap/0.0.1/index.pug +0 -12
  103. package/web/src/pug/index.ls +0 -72
  104. package/web/src/pug/index.pug +0 -52
  105. package/web/src/styl/index.styl +0 -0
  106. package/web/static/assets/block/cache.js +0 -4
  107. package/web/static/assets/custom-lib/d3/d3-array.v2.min.js +0 -2
  108. package/web/static/assets/custom-lib/d3/d3-axis.v2.min.js +0 -2
  109. package/web/static/assets/custom-lib/d3/d3-brush.v2.min.js +0 -2
  110. package/web/static/assets/custom-lib/d3/d3-color.v2.min.js +0 -2
  111. package/web/static/assets/custom-lib/d3/d3-dispatch.v2.min.js +0 -2
  112. package/web/static/assets/custom-lib/d3/d3-drag.v2.min.js +0 -2
  113. package/web/static/assets/custom-lib/d3/d3-ease.v2.min.js +0 -2
  114. package/web/static/assets/custom-lib/d3/d3-force-boundary.v0.0.1.min.js +0 -2
  115. package/web/static/assets/custom-lib/d3/d3-force.v2.min.js +0 -2
  116. package/web/static/assets/custom-lib/d3/d3-format.v2.min.js +0 -2
  117. package/web/static/assets/custom-lib/d3/d3-hierarchy.v2.min.js +0 -2
  118. package/web/static/assets/custom-lib/d3/d3-interpolate.v2.min.js +0 -2
  119. package/web/static/assets/custom-lib/d3/d3-path.v2.min.js +0 -2
  120. package/web/static/assets/custom-lib/d3/d3-quadtree.v2.min.js +0 -2
  121. package/web/static/assets/custom-lib/d3/d3-random.v2.min.js +0 -2
  122. package/web/static/assets/custom-lib/d3/d3-scale-chromatic.v1.min.js +0 -2
  123. package/web/static/assets/custom-lib/d3/d3-selection.v2.min.js +0 -2
  124. package/web/static/assets/custom-lib/d3/d3-shape.v2.min.js +0 -2
  125. package/web/static/assets/custom-lib/d3/d3-timer.v2.min.js +0 -2
  126. package/web/static/assets/custom-lib/d3/d3-transition.v2.min.js +0 -2
  127. package/web/static/assets/custom-lib/d3/d3.v4.js +0 -17178
  128. package/web/static/assets/custom-lib/d3/d3.v4.min.js +0 -2
  129. package/web/static/assets/data/countrycode.json +0 -1
  130. package/web/static/assets/data/world-110m.json +0 -1
  131. package/web/static/assets/data/zh-to-alpha2.json +0 -1
  132. package/web/static/assets/img/fluid.svg +0 -16
  133. package/web/static/block/axis/0.0.1/index.html +0 -79
  134. package/web/static/block/axis-bubble/0.0.1/index.html +0 -222
  135. package/web/static/block/bar/0.0.1/index.html +0 -581
  136. package/web/static/block/bar-list/0.0.1/index.html +0 -231
  137. package/web/static/block/bar-race/0.0.1/index.html +0 -184
  138. package/web/static/block/base/0.0.1/index.html +0 -197
  139. package/web/static/block/blank/0.0.1/index.html +0 -42
  140. package/web/static/block/bubble/0.0.1/index.html +0 -402
  141. package/web/static/block/bubble/0.0.1/test.html +0 -0
  142. package/web/static/block/curve-bar/0.0.1/index.html +0 -148
  143. package/web/static/block/dumbbell/0.0.1/index.html +0 -178
  144. package/web/static/block/empty/0.0.1/index.html +0 -76
  145. package/web/static/block/gauge/0.0.1/index.html +0 -150
  146. package/web/static/block/line/0.0.1/index.html +0 -540
  147. package/web/static/block/number/0.0.1/index.html +0 -152
  148. package/web/static/block/percent-list/0.0.1/index.html +0 -316
  149. package/web/static/block/pie/0.0.1/index.html +0 -281
  150. package/web/static/block/pie-bubble/0.0.1/index.html +0 -358
  151. package/web/static/block/radar/0.0.1/index.html +0 -149
  152. package/web/static/block/scatter/0.0.1/index.html +0 -251
  153. package/web/static/block/sheet/0.0.1/index.html +0 -103
  154. package/web/static/block/table/0.0.1/index.html +0 -73
  155. package/web/static/block/taiwantown/0.0.1/index.html +0 -210
  156. package/web/static/block/taiwanvillage/0.0.1/index.html +0 -220
  157. package/web/static/block/treemap/0.0.1/index.html +0 -360
  158. package/web/static/block/voronoi-treemap/0.0.1/index.html +0 -350
  159. package/web/static/block/wordcloud/0.0.1/index.html +0 -92
  160. package/web/static/block/worldmap/0.0.1/index.html +0 -207
  161. package/web/static/css/index.css +0 -0
  162. package/web/static/css/index.min.css +0 -0
  163. package/web/static/favicon.ico +0 -0
  164. package/web/static/index.html +0 -153
@@ -1,220 +0,0 @@
1
- <-(->it!) _
2
-
3
- block-factory =
4
- pkg:
5
- name: 'bubble', version: '0.0.1'
6
- extend: {name: "base", version: "0.0.1"}
7
- dependencies: []
8
- i18n:
9
- "zh-TW":
10
- "K": \千
11
- "M": \百萬
12
- init: ({root, context, pubsub, t}) ->
13
- pubsub.fire \init, mod: mod {context, t} .then ~> it.0
14
-
15
- mod = ({context, t}) ->
16
- {d3,forceBoundary,ldcolor,chart} = context
17
- sample: ->
18
- raw: [0 to 100].map (val) ~> {val: val, cat: Math.floor(Math.random! * 5), name: val}
19
- binding:
20
- group: {key: \cat}
21
- name: {key: \name}
22
- color: {key: \cat}
23
- area: {key: \val}
24
- config: {}
25
- dimension:
26
- group: {type: \C, name: "group", priority: 2}
27
- color: {type: \NR, name: "color", priority: 4}
28
- area: {type: \R, name: "area", priority: 1}
29
- name: {type: \NC, name: "name", priority: 3}
30
- init: ->
31
- @tint = tint = new chart.utils.tint!
32
- @parsed = @parsed or []
33
- @g = Object.fromEntries <[view unit legend]>.map ~> [it, d3.select(@layout.get-group it)]
34
- node = @root.querySelector '[ld=tip]'
35
- @tip = new chart.utils.tip {
36
- root: @root
37
- accessor: ({evt}) ~>
38
- if !(evt.target and data = d3.select(evt.target).datum!) => return null
39
- return {name: data.name, value: "#{data.area}#{@binding.area.unit or ''}"}
40
- range: ~> return @layout.get-node \view .getBoundingClientRect!
41
- }
42
- @legend = new chart.utils.legend do
43
- layout: @layout
44
- name: \legend
45
- root: @root
46
- shape: (d) -> d3.select(@).attr \fill, tint.get d.key
47
- cfg: selectable: true
48
- @legend.on \select, ~> @bind!; @resize!; @render!
49
-
50
- destroy: -> @tip.destroy!
51
-
52
- parse: ->
53
- @valid = @data.filter -> !isNaN(it.area)
54
- @groups = Array.from(new Set(@valid.map -> it.group)).filter -> it
55
- @sim = null
56
- @legend.data @groups.map -> {key: it, text: it}
57
- resize: ->
58
- @tip.toggle(if @cfg.{}tip.enabled? => @cfg.tip.enabled else true)
59
- @cfg.{}legend
60
- @random = d3.randomUniform.source(d3.randomLcg(+Date.now!))(0, 1)
61
- list = (
62
- [{key: 'root', root: true}] ++
63
- @groups.map(->{key: "group-#it"}) ++
64
- @valid.filter(~> !@cfg.legend.enabled or @legend.is-selected it.group)
65
- )
66
- if @legend.enabled! => list = list.filter ~> !it._raw or @legend.is-selected it.group
67
-
68
- @legend.config @cfg.legend
69
- @legend.update!
70
- @layout.update false
71
-
72
- ret = d3.hierarchy(
73
- d3.stratify!
74
- .id(->if it._idx? => it._idx else it.key)
75
- # no group-based position
76
- #.parentId(-> if it.root => '' else 'root' ) list
77
- # group-based position
78
- .parentId(-> if it.root => '' else if it.group => "group-#that" else \root) list
79
- )
80
- .sum -> it.data.area
81
- .sort (a,b) -> b.value - a.value # sort or not
82
-
83
- box = @vbox = @layout.get-box \view
84
- pack = d3.pack!size([box.width, box.height]) ret
85
- @parsed = ret.leaves!
86
- .map (d) ~>
87
- d._raw = d.data.data._raw
88
- ddata = d.data.data
89
- pos = if (!(@cfg.grouping?) and @cfg.grouping) => d else
90
- # randomized position regardless of grouping
91
- x: Math.random! * box.width * 0.8 + box.width * 0.1
92
- y: Math.random! * box.height * 0.8 + box.height * 0.1
93
-
94
- # if @parsed exists, then this is a data / size / config update.
95
- # we use the old data position for a smooth transition effect.
96
- od = if @parsed =>
97
- p = @parsed
98
- .filter (p) ->
99
- if (p.name? or ddata.name?) => (p.name == ddata.name)
100
- else (p._idx == ddata._idx)
101
- .0
102
- if p => p else d
103
- else d
104
- {x,y} = d
105
- d <<< {radius: ddata.area ** 0.5, x, y}
106
- d <<< ddata{_idx, name, color, group, area}
107
- if od._x => d._x = od._x else if !d._x => d._x = d.x
108
- if od._y => d._y = od._y else if !d._y => d._y = d.y
109
- d
110
- .filter -> it.area?
111
- @sim = null
112
- @start!
113
-
114
-
115
- render: ->
116
- {binding, tint} = @
117
- box = @vbox
118
- # automatically adjust circle size based on canvas size.
119
- @rate = rate = 0.85 * # make it slightly smaller. adjust as your wish
120
- Math.PI / (2 * Math.sqrt(3)) * # wasted space from exterior hexagon to circle
121
- Math.sqrt(box.width * box.height / @parsed.map(-> Math.PI * (it.r ** 2)).reduce(((a,b) -> a + b),0))
122
- @parsed.map (d,i) ~> d.rr = (d.r * @rate) >? 2
123
- tint.set(@cfg.palette)
124
- @g.view.selectAll \circle.bubble .data @parsed, (->it.name or it._idx)
125
- ..exit!remove!
126
- ..enter!append \circle
127
- .attr \class, 'bubble data'
128
- .attr \r, (d,i) ~> 0
129
- .attr \fill, (d,i) ~> tint.get(d.color or d.group or d.name)
130
- .attr \cx, (d,i) -> d._x
131
- .attr \cy, (d,i) -> d._y
132
-
133
- @g.view.selectAll \circle.bubble
134
- .attr \cx, (d,i) -> d._x
135
- .attr \cy, (d,i) -> d._y
136
- .transition!duration 350
137
- .attr \fill, (d,i) ~> tint.get(d.color or d.group or d.name)
138
- .attr \r, (d,i) ~> d.rr
139
-
140
- @g.view.selectAll \g.label .data @parsed, (->it.name or it._idx)
141
- ..exit!remove!
142
- ..enter!append \g
143
- .attr \class, 'label data'
144
- .attr \transform, (d,i) -> "translate(#{d.x},#{d.y})"
145
- .attr \opacity, 0
146
- .style \pointer-events, \none
147
- .style \cursor, \pointer
148
- .each (d,i) ->
149
- [0,1]
150
- .map ~> d3.select(@).append \text
151
- .map ->
152
- it
153
- .attr \dy, \-.28em
154
- .attr \text-anchor, \middle
155
- .style \pointer-event, \none
156
- @g.view.selectAll \g.label
157
- .attr \transform, (d,i) -> "translate(#{d._x},#{d._y})"
158
- .each (d,i) ->
159
- d3.select(@).selectAll \text
160
- .attr \dy, (e,i) -> if i == 0 => '-.28em' else '.88em'
161
- .text (e,i) ->
162
- if i == 0 =>
163
- area = d3.format('.3s')(d.area)
164
- /*
165
- ret = if area > 1000000 => (area/1000000).toFixed(1) + t(\M)
166
- else if area > 1000 => (area/1000).toFixed(1) + t(\K)
167
- else (area).toFixed(0)
168
- */
169
- ret = area
170
- if ret => ret = "#{ret}#{binding.area.unit or ''}"
171
- return ret
172
- else return d.name or ''
173
- .attr \font-size, (d,i) -> return if i => '0.9em' else '1.1em'
174
-
175
- .transition!duration 350
176
- .attr \fill, ->
177
- hcl = ldcolor.hcl(tint.get d.color or d.group or d.name)
178
- if hcl.l < 50 => \#fff else \#000
179
- @g.view.selectAll \g.label
180
- .transition!duration 350
181
- .attr \opacity, (d,i) ->
182
- box = @getBoundingClientRect!
183
- [w,h] = [box.width, box.height]
184
- if (2 * d.rr) < w or (2 * d.rr) < h => 0 else 1
185
- @legend.render!
186
-
187
- tick: ->
188
- cfg = @cfg
189
- pad = cfg.pad or 5
190
- box = @vbox
191
- if !@sim =>
192
- kickoff = true
193
- @fc = fc = d3.forceCollide!strength 0.5 .iterations 20 .radius ~> @rate * it.r
194
- @fg = d3.forceCenter!strength 0.5
195
- @fb = forceBoundary(
196
- (~> it.rr + pad), (~> it.rr + pad),
197
- (~> box.width - it.rr - pad), (~> box.height - it.rr - pad)
198
- ).strength 0.5
199
- @sim = d3.forceSimulation!
200
- # we can't make it center properly so disable it for now.
201
- #.force \center, @fg
202
- .force \b, @fb
203
- .force \collide, @fc
204
- @sim.stop!
205
- @sim.alpha 0.9
206
- @fg.x(box.width / 2)
207
- @fg.y(box.height / 2)
208
- @sim.nodes(@parsed)
209
- @sim.tick if kickoff => 10 else 1
210
- @parsed.map ->
211
- it._x = it._x + (it.x - it._x) * 0.1
212
- it._y = it._y + (it.y - it._y) * 0.1
213
-
214
- @g.view.selectAll \circle.bubble
215
- .attr \cx, (d,i) -> d._x
216
- .attr \cy, (d,i) -> d._y
217
- @g.view.selectAll \g.label
218
- .attr \transform, (d,i) -> "translate(#{d._x},#{d._y})"
219
-
220
- return block-factory
@@ -1,28 +0,0 @@
1
- div
2
- div(plug="layout")
3
- .pdl-layout
4
- div(data-type="layout")
5
- .pdl-cell(data-name="view")
6
- .pdl-cell(data-name="unit")
7
- .pdl-cell(data-name="legend")
8
- div(data-type="render")
9
- style(type="text/css"): :stylus
10
- /*.pdl-cell { width: 100%; height: 100% }*/
11
- .pdl-layout > div[data-type=layout]
12
- display: grid
13
- grid-template-columns: 1fr fit-content(10em)
14
- grid-template-rows: 1fr fit-content(1em)
15
- & > .pdl-cell[data-name=view]
16
- grid-area: 1 / 1
17
- & > .pdl-cell[data-name=legend]
18
- grid-column: 2
19
- grid-row: 1 / span 2
20
- padding: 0 .5em 0 .5em
21
- & > .pdl-cell[data-name=unit]
22
- grid-column: 1
23
- grid-row: 2
24
- line-height: 1em
25
- text-align: right
26
- padding: .25em 0
27
-
28
- script(type="@plotdb/block"): include:lsc index.ls
@@ -1,89 +0,0 @@
1
- <-(->it!) _
2
-
3
- block-factory =
4
- pkg:
5
- name: 'curve-bar', version: '0.0.1'
6
- extend: {name: "base", version: "0.0.1"}
7
- dependencies: []
8
-
9
- init: ({root, context, pubsub}) ->
10
- pubsub.fire \init, mod: mod {context}
11
-
12
- mod = ({context}) ->
13
- {d3,legend,chart} = context
14
- sample: ->
15
- raw: [1 to 5].map (val) ~> {val: Math.random!, name: val}
16
- binding:
17
- name: {key: \name}
18
- value: {key: \val}
19
- config: {}
20
- dimension:
21
- value: {type: \R, name: "value"}
22
- name: {type: \N, name: "name"}
23
- init: ->
24
-
25
- @gview = d3.select @layout.get-group \view
26
- @g = @gview.append \g
27
- @tint = tint = new chart.utils.tint!
28
-
29
- @arc = d3.arc!
30
- .innerRadius 0
31
- .outerRadius 100
32
- .startAngle 0
33
- .endAngle(Math.PI / 2)
34
-
35
- @legend = new chart.utils.legend do
36
- root: @root
37
- name: 'legend'
38
- shape: (d) -> d3.select(@).attr \fill, tint.get d.text
39
- layout: @layout
40
-
41
- parse: ->
42
- @tint.reset!
43
- @extent =
44
- v: d3.extent @data.map -> it.value
45
- @data.sort (a,b) -> a.value - b.value
46
- @legend.data @data.map -> {key: it.name, text: it.name}
47
-
48
- resize: ->
49
- box = @layout.get-box \view
50
- @layout.get-node \view .style.width = "#{box.height}px"
51
- @layout.update false
52
- [w,h] = [box.width, box.height]
53
- size = Math.min(w,h)
54
- @scale =
55
- v: d3.scaleLinear!domain [0, @extent.v.1] .range [0, Math.PI * 3 / 2]
56
- r: d3.scaleBand!domain (@data.map (d,i) -> i) .range [24, size/2]
57
- render: ->
58
- {scale} = @
59
- bw = @scale.r.bandwidth!
60
- @arc.cornerRadius bw/2
61
- if @cfg? and @cfg.palette => @tint.set(@cfg.palette.colors.map -> it.value or it)
62
- @legend.render!
63
-
64
- box = @layout.get-box \view
65
- @g.attr \transform, "translate(#{box.width / 2},#{box.height / 2})"
66
- @g.selectAll \path.data .data @data
67
- ..exit!remove!
68
- ..enter!append \path .attr \class, \data
69
- @g.selectAll \path.data
70
- .attr \d, (d,i) ~>
71
- @arc
72
- .startAngle 0
73
- .endAngle scale.v d.value
74
- .innerRadius scale.r(i)
75
- .outerRadius scale.r(i) + bw - 2
76
- @arc!
77
- .attr \fill, (d,i) ~> @tint.get d.name or d._idx
78
- @g.selectAll \text.label .data @data
79
- ..exit!remove!
80
- ..enter!append \text .attr \class, \label
81
- @g.selectAll \text.label
82
- .attr \dominant-baseline, \center
83
- .attr \text-anchor, \end
84
- .attr \x, \-.5em
85
- .attr \y, (d,i) -> -scale.r(i)
86
- .attr \dy, -bw * 1/4
87
- .text (d,i) -> d.name
88
-
89
- return block-factory
@@ -1,16 +0,0 @@
1
- div
2
- div(plug="layout")
3
- .pdl-layout
4
- div(data-type="layout")
5
- .pdl-cell(data-name="view")
6
- .pdl-cell(data-name="legend")
7
- div(data-type="render")
8
- style(type="text/css"): :stylus
9
- .pdl-layout > div[data-type=layout]
10
- display: flex
11
- justify-content: center
12
- align-items: center
13
- [data-name=view]
14
- height: 100%
15
- margin-right: 1em
16
- script(type="@plotdb/block"): include:lsc index.ls
@@ -1,338 +0,0 @@
1
-
2
- {
3
- sample: function() {
4
- var list = d3.range(Math.round(Math.random() * 3 + 4));
5
- var value = [{name: "Amount", data: list.map(function() { return Math.random(); })}];
6
- var name = [{name: "Fruit", data: list.map(function(it) {
7
- return plotdb.data.sample.fruit[it % plotdb.data.sample.fruit.length]
8
- })}];
9
- return {name: name, value: value};
10
- },
11
- dimension: {
12
- value: { type: [plotdb.Number], require: true, desc: "size of pie" },
13
- name: { type: [], require: false, desc: "name of pie" },
14
- },
15
- config: {
16
- fontFamily: {},
17
- background: {},
18
- textFill: {},
19
- fontSize: {},
20
- margin: {},
21
- palette: {},
22
- stroke: {default: "#fff"},
23
- strokeWidth: {},
24
- thick: { name: "thick (0~1)", type: [plotdb.Number], default: 1 },
25
- boxRoundness: {},
26
- labelShow: {},
27
- labelShadowSize: {},
28
- otherLimit: {},
29
- otherLabel: {},
30
- labelPosition: {},
31
- legendLabel: {},
32
- legendShow: {},
33
- legendPosition: {},
34
- legendInline: {
35
- name: "Show Inline Legend",
36
- type: [plotdb.Boolean],
37
- default: false,
38
- desc: "[Experiment feature] Show legend with wedges instead of a standalone legend panel",
39
- category: "Legend Inline"
40
- },
41
- legendInlineColor: {
42
- name: "Color",
43
- type: [plotdb.Color],
44
- default: "#222",
45
- desc: "Dot / Line Color",
46
- category: "Legend Inline"
47
- },
48
- legendInlineStrokeWidth: {
49
- name: "Stroke Width",
50
- type: [plotdb.Number],
51
- default: 2,
52
- desc: "Dot / line width of inline legend",
53
- category: "Legend Inline"
54
- },
55
- unit: {},
56
- sort: { name: "Sort Values", type: [plotdb.Boolean], default: true, rebindOnChange: true },
57
- showPercent: {name: "Show Percent", type: [plotdb.Boolean], default: false},
58
- popupShow: {}
59
- },
60
-
61
- init: function() {
62
- var that = this;
63
- this.svg = d3.select(this.root).append("svg");
64
- this.pieGroup = this.svg.append("g").attr("class", "pie-group");
65
- this.legendGroup = this.svg.append("g").attr({"class": "legend-group"});
66
- this.popup = plotd3.html.tooltip(this.root).on("mousemove", function(d,i,popup) {
67
- popup.select(".title").text(d.data.name || "-");
68
- popup.select(".value").html([
69
- Math.ceil(100 * d.data.value)/100 + (that.config.unit || ""),
70
- d.data.percent
71
- ].join("<br>"));
72
- });
73
- this.popup.coord(function(d,i) {
74
- var r = d.data.centralRadius;
75
- var a = (d.endAngle + d.startAngle)/2;
76
- var x = Math.sin(a) * r;
77
- var y = -Math.cos(a) * r;
78
- var cx = (that.width - that.legendOffset[0] * (that.config.legendPosition == "right" ? 1 : -1 )) / 2;
79
- var cy = (that.height - that.legendOffset[1] * (that.config.legendPosition == "bottom" ? 1 : -1 )) / 2;
80
- return [
81
- cx + x, cy + y, 1,1
82
- ];
83
- });
84
- this.pielayout = d3.layout.pie().value(function(it) { return it.value; });
85
- },
86
- parse: function() {
87
- if(!this.dimension.name.fields.length) this.data.map(function(d,i) { d.name = i; });
88
- if(!this.dimension.value.fields.length) this.data.map(function(d,i) { d.value= 1; });
89
- this.names = d3.map(this.data, function(d,i) { return d.name || ""; }).keys();
90
- },
91
- bind: function() {
92
- var that = this, sel, sel1, sel2;
93
- sel = this.pieGroup.selectAll("path.data.pie").data(this.parsed);
94
- sel.exit().attr({class: ""}).transition("exit").duration(500).attr({opacity: 0}).remove();
95
- sel1 = sel.enter().append("path").attr({class: "data pie", opacity: 0});
96
- sel = this.pieGroup.selectAll("g.label").data(this.parsed);
97
- sel.exit().attr({class: ""}).transition("exit").duration(500).attr({opacity: 0}).remove();
98
- sel2 = sel.enter().append("g").attr({class: "label", opacity: 0});
99
- this.pieGroup.selectAll("g.label").each(function(d,i) {
100
- var node = d3.select(this),sel;
101
- sel = node.selectAll("text").data([d,d]);
102
- sel.exit().remove();
103
- sel.enter().append("text").attr({
104
- class: function(d,i) { return "data label " + (i ? "value" : "shadow"); },
105
- });
106
- });
107
- sel = this.pieGroup.selectAll("g.marker").data(this.parsed);
108
- sel.exit().remove();
109
- sel.enter().append("g").attr({class: "marker"}).each(function(d,i) {
110
- var node = d3.select(this);
111
- node.append("path"); node.append("circle"); node.append("circle"); node.append("text");
112
- });
113
- this.pieGroup.selectAll("g.marker").each(function(d,i) {
114
- var node = d3.select(this);
115
- node.select("path").datum(d);node.selectAll("circle").data([d,d]);node.select("text").datum(d);
116
- });
117
- if(this.config.popupShow) this.popup.nodes(sel1).nodes(sel2);
118
- [sel1,sel2].map(function(sel) {
119
- sel.on("click", function(d,i) {
120
- that.fire("filter", {type: "value", value: d.data.name, field: "name"});
121
- });
122
- });
123
- },
124
- resize: function() {
125
- var that = this;
126
- var other = {
127
- name: that.config.otherLabel,
128
- value: this.data.filter(function(d,i) { return d.value < that.config.otherLimit; }).reduce(function(a,b) { return a + b.value; }, 0)
129
- };
130
- this.fdata = this.data.filter(function(d,i) { return d.value >= that.config.otherLimit; });
131
- if(other.value != 0) this.fdata.push(other);
132
- this.names = this.fdata.map(function(it) { return it.name; });
133
-
134
- var box = this.root.getBoundingClientRect();
135
- var svgbox = this.svg[0][0].getBoundingClientRect();
136
- var width = this.width = box.width;
137
- var height = this.height = box.height;
138
- this.svg.attr({
139
- width: width + "px", height: height + "px",
140
- viewBox: [0,0,width,height].join(" "),
141
- preserveAspectRatio: "xMidYMid"
142
- });
143
- this.popup.fontSize(this.config.fontSize);
144
- this.legendVertical = ["right","left"].indexOf(this.config.legendPosition) >= 0;
145
- this.cScale = plotdb.Palette.scale.ordinal(this.config.palette);
146
- this.legend = plotd3.rwd.legend()
147
- .size(this.legendVertical ? [220, this.height - 2 * this.config.margin] : [this.width - 2 * this.config.margin, 220])
148
- .orient(this.config.legendPosition).scale(this.cScale)
149
- .label(this.config.legendLabel || "")
150
- .tickValues(this.names).fontSize(this.config.fontSize);
151
- this.legendGroup.call(this.legend);
152
- this.legendGroup.selectAll(".legend").on("mouseover", function(d,i) {
153
- that.activeGroup = d; that.render();
154
- }).on("mouseout", function(d,i) {
155
- that.activeGroup = null; that.render();
156
- });
157
- this.legendSize = (this.config.legendShow ? this.legend.offset() : [0,0]);
158
- this.legendOffset = this.config.legendShow ?
159
- this.legendVertical ? [this.legendSize[0] + this.config.fontSize,0] : [0, this.legendSize[1] + this.config.fontSize]
160
- : [0,0];
161
- var w = this.width - this.legendOffset[0];
162
- var h = this.height - this.legendOffset[1] - (this.config.legendInline ? 60 : 0);
163
- this.radius = ( w > h ? h : w )/2 - this.config.margin
164
- - (!that.config.labelShow || (that.config.labelPosition == 'in') ? 0 : 2 * that.config.fontSize);
165
- this.pielayout.sort(this.config.sort ? function(a,b) { return b.value - a.value; } : null);
166
- var parsed = this.pielayout(this.fdata);
167
- this.parsed = parsed;
168
- var sum = d3.sum(this.fdata, function(d,i) { return d.value; });
169
- parsed.map(function(d,i) {
170
- var a = (d.startAngle + d.endAngle ) / 2;
171
- var r = that.radius;
172
- if(that.config.labelPosition != 'in') r += that.config.fontSize * 2;
173
- else r = (that.radius + that.radius * 0.9 * (1 - that.config.thick/2))/2;
174
- if(!d.data.parsed) d.data.parsed = d;
175
- d.data.width = (d.endAngle - d.startAngle) * r;
176
- d.data.centralRadius = r;
177
- d.data.innerRadius = that.radius * 0.9 * (1 - that.config.thick/2);
178
- d.data.x = Math.sin(a) * r;
179
- d.data.y = -Math.cos(a) * r;
180
- d.data.startAngle = d.startAngle;
181
- d.data.endAngle = d.endAngle;
182
- d.data.padAngle = d.padAngle;
183
- d.data.percent = (parseInt(1000 * d.data.value / sum) / 10) + "%";
184
- });
185
- if(this.config.sort) {
186
- this.parsed.sort(function(a,b) { return b.data.value - a.data.value; });
187
- } else {
188
- this.parsed.sort(function(a,b) { return parseInt(Math.random() * 2 - 1); });
189
- }
190
- if(this.config.thick>1) this.config.thick = 1;
191
- if(this.config.thick<0) this.config.thick = 0;
192
- this.arc = d3.svg.arc()
193
- .innerRadius(function(d,i) { return d.data.innerRadius; })
194
- .outerRadius(this.radius)
195
- .cornerRadius(this.config.boxRoundness);
196
- },
197
- render: function() {
198
- var that = this;
199
- if(this.config.fontFamily) d3.select(this.root).style("font-family", this.config.fontFamily);
200
- d3.select(this.root).style("background-color", this.config.background);
201
- this.svg.selectAll("g.label text, .legend text, g.marker text").attr({
202
- "font-size": that.config.fontSize,
203
- "fill": that.config.textFill
204
- });
205
- (this.pieGroup.attr("transform")
206
- ? this.pieGroup.transition("morph").duration(500)
207
- : this.pieGroup
208
- ).attr({
209
- transform: [
210
- "translate(",
211
- (that.width - this.legendOffset[0] * (this.config.legendPosition == "right" ? 1 : -1 )) / 2,
212
- (that.height - this.legendOffset[1] * (this.config.legendPosition == "bottom" ? 1 : -1 )) / 2,
213
- ")"
214
- ].join(" ")
215
- });
216
- this.pieGroup.selectAll("path.data.pie").attr({
217
- fill: function(d,i) {
218
- return that.cScale(d.data.name);
219
- },
220
- stroke: this.config.stroke,
221
- "stroke-width": this.config.strokeWidth
222
- }).transition("opacity").duration(500).attr({
223
- opacity: function(d,i) {
224
- return (!that.activeGroup || that.activeGroup == d.data.name ? 1 : 0.1);
225
- }
226
- });
227
- this.pieGroup.selectAll("path.data.pie").transition("morph").duration(500).tween("morph", function(d,i) {
228
- var oldAngles = (
229
- this.lastAngles || [d.startAngle,d.startAngle,d.data.innerRadius, d.data.innerRadius]
230
- );
231
- var curAngles = [d.startAngle, d.endAngle, that.radius, d.data.innerRadius];
232
- var node = d3.select(this);
233
- this.lastAngles = curAngles;
234
- return function(t) {
235
- node.attr({
236
- d: that.arc
237
- .startAngle((curAngles[0] - oldAngles[0]) * t + oldAngles[0])
238
- .endAngle((curAngles[1] - oldAngles[1]) * t + oldAngles[1])
239
- .outerRadius((curAngles[2] - oldAngles[2]) * t + oldAngles[2])
240
- .innerRadius((curAngles[3] - oldAngles[3]) * t + oldAngles[3])
241
- });
242
- };
243
- });
244
- var labels = this.pieGroup.selectAll("g.label");
245
- labels.filter(function(d,i) { return !d3.select(this).attr("transform"); }).attr({
246
- transform: function(d,i) {
247
- return ["translate(", d.data.x, d.data.y,")"].join(" ");
248
- }
249
- });
250
- labels.each(function(d,i) {
251
- var node = d3.select(this);
252
- node.selectAll("text").attr({
253
- fill: function(d,i) {
254
- var c = d3.hsl(that.cScale(d.data.name));
255
- return (c.l > 0.6 || (that.config.labelPosition != 'in') ? "#000":"#fff");
256
- },
257
- "font-size": that.config.fontSize,
258
- "text-anchor": "middle",
259
- dy: "0.38em"
260
- }).style({
261
- display: (that.config.labelShow?"block":"none")
262
- }).text(function(d,i) {
263
- return (that.config.showPercent ? d.data.percent : parseInt(100*d.data.value)/100);
264
- });
265
- node.select("text.label.shadow").attr({
266
- stroke: function(d,i) {
267
- var c = d3.hsl(that.cScale(d.data.name));
268
- return (c.l > 0.6 || that.config.labelPosition != 'in' ? "#fff":"#000");
269
- },
270
- "stroke-width": that.config.labelShadowSize,
271
- }).style({
272
- display: (that.config.labelShadowSize && that.config.labelShow ? "block" : "none")
273
- });
274
- if(that.config.fontSize * 2 > d.data.width * 1.5) {
275
- node.selectAll("text").style({display: "none"});
276
- }
277
- })
278
- labels.transition("move").duration(500).attr({
279
- transform: function(d,i) {
280
- return ["translate(", d.data.x, d.data.y,")"].join(" ");
281
- }
282
- });
283
- labels.transition("opacity").duration(500).attr({opacity: 1});
284
- this.pieGroup.selectAll("g.marker").attr({
285
- display: (this.config.legendInline ? "block" : "none")
286
- }).each(function(d,i) {
287
- var node = d3.select(this);
288
- var x1, y1, x2, y2, xm, ym;
289
- var a = (d.startAngle + d.endAngle)/2;
290
- x1 = Math.sin(a) * that.radius;
291
- y1 = -Math.cos(a) * that.radius;
292
- x2 = x1 + (x1 < 0 ? -100 : 100);
293
- y2 = y1 + (y1 < 0 ? -20 : 20);
294
- if(Math.abs(x1) < Math.abs(y1)) {
295
- xm = x1 + (x1 < 0 ? -20 : 20);
296
- ym = y2;
297
- } else {
298
- xm = x2 + (x1 < 0 ? 20 : -20);
299
- ym = y1;
300
- }
301
- node.select("circle").attr({
302
- cx: x1, cy: y1, fill: that.config.legendInlineColor,
303
- r: that.config.legendInlineStrokeWidth * 1.5
304
- });
305
- node.select("circle:last-of-type").attr({
306
- cx: x2, cy: y2, fill: that.config.legendInlineColor,
307
- r: that.config.legendInlineStrokeWidth * 1.5
308
- });
309
- node.select("path").attr({
310
- d: function() {
311
- return [
312
- "M", x1, y1,
313
- "L", xm, ym,
314
- "L", x2, y2
315
- ].join(" ");
316
- },
317
- fill: "none",
318
- stroke: that.config.legendInlineColor,
319
- "stroke-width": that.config.legendInlineStrokeWidth
320
- });
321
- node.select("text").attr({
322
- x: x2, y: y2,
323
- dy: "0.38em",
324
- dx: (x1<0?-0.5:0.5) + "em",
325
- "text-anchor": (x1<0?"end": "start")
326
- }).text(d.data.name);
327
- });
328
- var minus = ["left","top"].indexOf(this.config.legendPosition) >= 0;
329
- var offset = (this.legendVertical
330
- ? [this.width/2 + (this.radius + this.config.fontSize/2 - (minus?-1:1)*this.legendSize[0]/2) * (minus ? -1 : 1),(this.height - this.legendSize[1])/2]
331
- : [(this.width - this.legendSize[0])/2,this.height/2 + (this.radius + this.config.fontSize + (!minus?-this.legendSize[1]:0)) * (minus?-1:1)]
332
- );
333
- this.legendGroup.attr({
334
- transform: ["translate(",offset[0], offset[1], ")"].join(" "),
335
- display: this.config.legendShow ? "block" : "none"
336
- });
337
- }
338
- }