@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,125 +0,0 @@
1
- <-(->it!) _
2
-
3
- block-factory =
4
- pkg:
5
- name: 'pie', version: '0.0.1'
6
- extend: {name: "base", version: "0.0.1"}
7
- dependencies: []
8
- i18n:
9
- "zh-TW":
10
- value: "數值"
11
- name: "名稱"
12
- other: "其它"
13
- init: ({root, context, pubsub, t}) ->
14
- pubsub.fire \init, mod: mod {context, t}
15
-
16
- mod = ({context, t}) ->
17
- {chart,d3} = context
18
- sample: ->
19
- raw: [1 to 5].map (val) ~> {val: (10 * Math.random!).toFixed(2), name: val}
20
- binding:
21
- name: {key: \name}
22
- value: {key: \val}
23
- config: {}
24
- dimension:
25
- value: {type: \R, name: "value", priority: 20}
26
- name: {type: \NO, name: "name", priority: 10}
27
- init: ->
28
- @g = Object.fromEntries <[view legend]>.map ~> [it, d3.select(@layout.get-group it).append \g]
29
- @tint = tint = new chart.utils.tint!
30
- @tip = new chart.utils.tip {
31
- root: @root
32
- accessor: ({evt}) ~>
33
- if !(evt.target and data = d3.select(evt.target).datum!) => return null
34
- v = if isNaN(data.value) => '-' else "#{(data.value).toFixed(2)}#{@binding.value.unit or ''}"
35
- return {name: data.name, value: v}
36
- range: ~> @layout.get-node \view .getBoundingClientRect!
37
- }
38
- @legend = new chart.utils.legend do
39
- layout: @layout
40
- name: 'legend'
41
- root: @root
42
- shape: (d) -> d3.select(@).attr \fill, tint.get d.text
43
- cfg: selectable: true
44
- @legend.on \select, ~> @bind!; @resize!; @render!
45
- @arc = d3.arc!
46
- .innerRadius 0
47
- .outerRadius 100
48
- .startAngle 0
49
- .endAngle(Math.PI / 2)
50
- @total = {}
51
-
52
- destroy: ->
53
- @tip.destroy!
54
-
55
- parse: ->
56
- @tint.reset!
57
- @parsed = @data.map ->
58
- ret = {} <<< it
59
- if isNaN(ret.value) => ret.value = 0
60
- ret
61
-
62
- @parsed.sort (a,b) -> b.value - a.value
63
- if @cfg.collapse? and @cfg.collapse.enabled =>
64
- if @cfg.collapse.method == \count =>
65
- other = @parsed.splice(@cfg.collapse.threshold or 7)
66
- if other.length => @parsed.push {name: t("other"), value: other.reduce(((a,b) -> a + b.value),0)}
67
- else # == \percent
68
- [total,sum] = [@parsed.reduce(((a,b) -> a + b.value),0), 0]
69
- for idx from 0 til @parsed.length =>
70
- sum += @parsed[idx].value
71
- if (sum / total) > (@cfg.collapse.threshold or 0.75) => break
72
- other = @parsed.splice(idx + 1)
73
- if other.length => @parsed.push {name: t("other"), value: other.reduce(((a,b) -> a + b.value),0)}
74
- @legend.data @parsed.map -> {text: it.name, key: it.name}
75
- bind: ->
76
- @parsed.map ~> it.picked = @legend.is-selected it.name
77
- @total.old = @total.cur
78
- @total.cur = @parsed.reduce(((a,b) -> a + (if b.picked => b.value else 0)),0) or 1
79
- if !@total.old => @total.old = @total.cur
80
-
81
- offset = 0
82
- for i from 0 til @parsed.length =>
83
- obj = @parsed[i]
84
- if !obj.old => obj.old = {s: (if @parsed[i - 1] => that.old.e else 0), e: 0}
85
- if obj.cur => obj.old = obj.cur
86
- obj.cur =
87
- s: offset
88
- e: offset + (if obj.picked => obj.value else 0)
89
- obj.angle =
90
- old: s: 2 * Math.PI * obj.old.s / @total.old, e: 2 * Math.PI * obj.old.e / @total.old
91
- cur: s: 2 * Math.PI * obj.cur.s / @total.cur, e: 2 * Math.PI * obj.cur.e / @total.cur
92
- if obj.picked => offset += obj.value
93
-
94
- resize: ->
95
- @layout.update false
96
- box = @layout.get-box \view
97
- @layout.get-node \view .style.width = "#{box.height}px"
98
- @layout.update false
99
- box = @layout.get-box \view
100
- [w,h] = [box.width, box.height]
101
- size = Math.min(w,h)
102
- @arc
103
- .innerRadius((@cfg.donut-percent or 0.7) * size/2)
104
- .outerRadius size/2
105
- render: ->
106
- {tint} = @
107
-
108
- interpolate-arc = (a1, a2) ~> (t) ~>
109
- [s,e] = <[s e]>.map (i) -> (a2[i] - a1[i]) * t + a1[i]
110
- @arc.startAngle s .endAngle e
111
- @arc!
112
-
113
- if @cfg? and @cfg.palette => @tint.set(@cfg.palette.colors.map -> it.value or it)
114
- box = @layout.get-box \view
115
- @g.view.attr \transform, "translate(#{box.width / 2},#{box.height / 2})"
116
- @g.view.selectAll \path.data .data(@parsed, -> it.name or it._idx)
117
- ..exit!remove!
118
- ..enter!append \path .attr \class, \data
119
- @g.view.selectAll \path.data
120
- .attr \fill, (d,i) ~> @tint.get(d.name or d._idx)
121
- .transition!duration 350
122
- .attrTween \d, (d,i) -> interpolate-arc d.angle.old, d.angle.cur
123
- @legend.render!
124
-
125
- return block-factory
@@ -1,19 +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
- & > [data-name=legend]
17
- min-width: 6em
18
-
19
- 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
- }
@@ -1,185 +0,0 @@
1
- <-(->it!) _
2
-
3
- block-factory =
4
- pkg:
5
- name: 'pie-bubble', version: '0.0.1'
6
- extend: {name: "base", version: "0.0.1"}
7
- dependencies: []
8
- init: ({root, context, pubsub}) ->
9
- pubsub.fire \init, mod: mod {context} .then ~> it.0
10
-
11
- mod = ({context}) ->
12
- {d3,forceBoundary,ldcolor,chart} = context
13
- sample: ->
14
- raw: [0 to 50].map (val) ~> {name: "name#val", val1: Math.random! * 100, val2: Math.random! * 100}
15
- binding: do
16
- name: {key: \name}
17
- left: {key: \val1}
18
- right: {key: \val2}
19
- config: {} <<< chart.utils.config.preset.default <<<
20
- color:
21
- left: type: \color, default: \#f90
22
- right: type: \color, default: \#09f
23
- dimension:
24
- name: {type: \N, name: "name"}
25
- left: {type: \R, name: "size in left"}
26
- right: {type: \R, name: "size in right"}
27
- init: ->
28
- @tint = tint = new chart.utils.tint!
29
- @valid = []
30
- @g = Object.fromEntries <[view legend]>.map ~> [it, d3.select(@layout.get-group it)]
31
- @tip = new chart.utils.tip {
32
- root: @root
33
- node: @root.querySelector('[data-name=tip]')
34
- view:
35
- name: ({ctx}) -> if ctx => ctx.name else ''
36
- value1: ({ctx}) -> if ctx => ctx.value1 else ''
37
- value2: ({ctx}) -> if ctx => ctx.value2 else ''
38
- accessor: ({evt}) ~>
39
- if !(evt.target and data = d3.select(evt.target).datum!) => return null
40
- unit = @binding.left.unit or @binding.right.unit or ''
41
- fmt = d3.format('.3s')
42
- return {
43
- name: data.name or ''
44
- value1: "#{@legend-data.0.text}: #{fmt data.L}#{unit}"
45
- value2: "#{@legend-data.1.text}: #{fmt data.R}#{unit}"
46
- }
47
- range: ~> return @layout.get-node \view .getBoundingClientRect!
48
- }
49
- @legend = new chart.utils.legend do
50
- layout: @layout
51
- name: \legend
52
- root: @root
53
- shape: (d) -> d3.select(@).attr \fill, tint.get d.key
54
- cfg: selectable: true
55
- @legend.on \select, ~> @bind!; @resize!; @render!
56
-
57
- destroy: -> @tip.destroy!
58
-
59
- parse: ->
60
- @valid = @data.filter -> !isNaN(it.left + it.right)
61
- @legend-data = <[left right]>.map ~>
62
- v = if @binding[it] => (@binding[it].name or @binding[it].key) else it
63
- {key: v, text: v}
64
- @legend.data @legend-data
65
-
66
- resize: ->
67
- @sim = null
68
- @start!
69
- @cfg.{}tip
70
- @cfg.{}color
71
- @cfg.{}legend
72
- @tip.toggle(if @cfg.{}tip.enabled? => @cfg.tip.enabled else true)
73
- if @cfg.color.left == @cfg.color.right => @cfg.color-right = \#09f #debug
74
-
75
- @tint.set({
76
- colors: [
77
- (@cfg.color.left or @cfg.palette.colors.0 or \#09f)
78
- (@cfg.color.right or @cfg.palette.colors.1 or \#f90)
79
- ],
80
- maps: @legend-data.map -> it.key
81
- }, true)
82
- @legend.config @cfg.legend <<< {selectable: false}
83
- @legend.update!
84
- @layout.update false
85
-
86
- @vbox = vbox = @layout.get-box \view
87
- rng = d3.randomUniform.source(d3.randomLcg(@seed))(0, 1)
88
- @valid.map (obj) ~>
89
- [L, R] = [(obj.left >? 0), (obj.right >? 0)]
90
- r = sum = L + R
91
- rate = if !sum => 0.5 else R / sum
92
- [x, y] = [rate * vbox.width, rng! * vbox.height]
93
- obj <<< {r, sum, rate, L, R, cx: x}
94
- if !(obj.x?) => obj <<< {x, y}
95
- if !(obj._x?) => obj <<< {_x: x, _y: y}
96
- obj
97
-
98
- @rate = 0.9 * # make it slightly smaller. adjust as your wish
99
- Math.PI / (2 * Math.sqrt(3)) * # wasted space from exterior hexagon to circle
100
- Math.sqrt(vbox.width * vbox.height / @valid.map(-> Math.PI * (it.r ** 2)).reduce(((a,b) -> a + b),0))
101
- @valid.map (d,i) ~> d.rr = (d.r * @rate) >? 2
102
-
103
- render: ->
104
- {binding, tint, rate, legend-data} = @
105
-
106
- @g.view.selectAll \g.pie .data @valid
107
- ..exit!remove!
108
- ..enter!append \g .attr \class, "pie data"
109
- .each ->
110
- d3.select(@).append \path
111
- d3.select(@).append \path
112
- @g.view.selectAll \g.pie
113
- .each (d,i) ->
114
- d3.select(@)
115
- .attr \transform, "translate(#{d.x} #{d.y})"
116
- d3.select @ .selectAll \path
117
- .attr \fill, (e,j) -> tint.get(legend-data[j].key)
118
- .attr \d, (e,j) ~>
119
- r = d.rr
120
- rx = r * Math.cos(d.rate * Math.PI)
121
- ry1 = -r * Math.sin(d.rate * Math.PI)
122
- ry2 = r * Math.sin(d.rate * Math.PI)
123
- f = if d.rate > 0.5 => j else 1 - j
124
- return [
125
- "M", rx, ry1,
126
- "A", r, r, 0, f, j, rx, ry2,
127
- "L", 0, 0,
128
- "Z"
129
- ].join(" ")
130
-
131
- @g.view.selectAll \g.label .data @valid
132
- ..exit!remove!
133
- ..enter!append \g
134
- .attr \class, \label
135
- .each (d,i) ->
136
- [0]
137
- .map ~> d3.select(@).append \text
138
- .map ->
139
- it
140
- .attr \text-anchor, \middle
141
- .attr \dominant-baseline, \middle
142
- .attr \font-size, \.7em
143
- .style \pointer-event, \none
144
- @g.view.selectAll \g.label
145
- .attr \transform, (d,i) -> "translate(#{d.x},#{d.y})"
146
- .each (d,i) ->
147
- d3.select(@).selectAll \text
148
- .attr \opacity, (if (d.rr * 2) < "#{(d.sum).toFixed(2)}".length * 7 => 0 else 1)
149
- .text (e,i) ->
150
- ret = d3.format('.3s')(d.sum)
151
- if ret => ret = "#{ret}#{binding.left.unit or binding.right.unir or ''}"
152
- return ret
153
- @legend.render!
154
-
155
- tick: ->
156
- pad = @cfg.pad or 5
157
- if !@sim =>
158
- kickoff = true
159
- @fc = fc = d3.forceCollide!strength 0.6 .iterations 20 .radius ~> it.rr
160
- @fb = forceBoundary(
161
- (->it.rr + pad), (->it.rr + pad),
162
- (~>@vbox.width - it.rr - pad), (~>@vbox.height - it.rr - pad)
163
- ).strength 0.8
164
- @fy = d3.forceY!strength 0.2
165
- @fx = d3.forceX!strength 0.2 .x -> it.cx
166
- @sim = d3.forceSimulation!
167
- .force \b, @fb
168
- .force \collide, fc
169
- .force \fx, @fx
170
- .force \fy, @fy
171
- @sim.stop!
172
- @sim.alpha 0.9
173
- @fy.y(@vbox.height / 2)
174
- @sim.nodes(@valid)
175
- @sim.tick if kickoff => 10 else 1
176
- if @sim.alpha! < 0.01 => @stop!
177
- @valid.map ->
178
- it._x = it._x + (it.x - it._x) * 0.1
179
- it._y = it._y + (it.y - it._y) * 0.1
180
- @g.view.selectAll \g.label
181
- .attr \transform, (d,i) -> "translate(#{d._x},#{d._y})"
182
- @g.view.selectAll \g.pie
183
- .attr \transform, (d,i) -> "translate(#{d._x},#{d._y})"
184
-
185
- return block-factory