@plotdb/chart 0.0.2

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 (177) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +50 -0
  3. package/build +39 -0
  4. package/dist/chart.css +8 -0
  5. package/dist/chart.js +603 -0
  6. package/dist/chart.min.css +1 -0
  7. package/dist/chart.min.js +1 -0
  8. package/dist/data/index.js +73 -0
  9. package/dist/utils/axis.js +357 -0
  10. package/dist/utils/axis.min.js +1 -0
  11. package/dist/utils/config.js +33 -0
  12. package/dist/utils/config.min.js +1 -0
  13. package/dist/utils/data.js +238 -0
  14. package/dist/utils/data.min.js +1 -0
  15. package/dist/utils/format.js +70 -0
  16. package/dist/utils/format.min.js +1 -0
  17. package/dist/utils/legend.css +40 -0
  18. package/dist/utils/legend.js +263 -0
  19. package/dist/utils/legend.min.css +1 -0
  20. package/dist/utils/legend.min.js +1 -0
  21. package/dist/utils/tint.js +138 -0
  22. package/dist/utils/tint.min.js +1 -0
  23. package/dist/utils/tip.js +148 -0
  24. package/dist/utils/tip.min.js +1 -0
  25. package/doc/charts.md +58 -0
  26. package/doc/config.md +44 -0
  27. package/doc/draft/chart.md +146 -0
  28. package/doc/draft/data.md +8 -0
  29. package/doc/draft/definition.md +0 -0
  30. package/doc/draft/events.md +0 -0
  31. package/doc/draft/filter.md +0 -0
  32. package/doc/draft/lifecycle.md +89 -0
  33. package/doc/spec.md +89 -0
  34. package/doc/todo.md +27 -0
  35. package/doc/utils/tip.md +84 -0
  36. package/package.json +83 -0
  37. package/src/chart.ls +320 -0
  38. package/src/chart.styl +6 -0
  39. package/src/data/en-US/category +20 -0
  40. package/src/data/en-US/fruit +39 -0
  41. package/src/data/en-US/movie +125 -0
  42. package/src/data/en-US/name +58 -0
  43. package/src/data/index.ls +34 -0
  44. package/src/data/zh-TW/category +20 -0
  45. package/src/data/zh-TW/fruits +38 -0
  46. package/src/data/zh-TW/movie +125 -0
  47. package/src/data/zh-TW/names +105 -0
  48. package/src/utils/axis.ls +229 -0
  49. package/src/utils/config.ls +11 -0
  50. package/src/utils/data.ls +104 -0
  51. package/src/utils/format.ls +30 -0
  52. package/src/utils/legend.ls +134 -0
  53. package/src/utils/legend.styl +37 -0
  54. package/src/utils/tint.ls +66 -0
  55. package/src/utils/tip.ls +97 -0
  56. package/web/src/pug/base.pug +19 -0
  57. package/web/src/pug/block/axis/0.0.1/index.ls +38 -0
  58. package/web/src/pug/block/axis/0.0.1/index.pug +1 -0
  59. package/web/src/pug/block/axis-bubble/0.0.1/index.ls +141 -0
  60. package/web/src/pug/block/axis-bubble/0.0.1/index.pug +1 -0
  61. package/web/src/pug/block/bar/0.0.1/index.ls +274 -0
  62. package/web/src/pug/block/bar/0.0.1/index.pug +35 -0
  63. package/web/src/pug/block/bar-list/0.0.1/index.ls +85 -0
  64. package/web/src/pug/block/bar-list/0.0.1/index.pug +30 -0
  65. package/web/src/pug/block/bar-race/0.0.1/index.ls +83 -0
  66. package/web/src/pug/block/bar-race/0.0.1/index.pug +21 -0
  67. package/web/src/pug/block/base/0.0.1/index.ls +67 -0
  68. package/web/src/pug/block/base/0.0.1/index.pug +5 -0
  69. package/web/src/pug/block/blank/0.0.1/index.ls +25 -0
  70. package/web/src/pug/block/blank/0.0.1/index.pug +1 -0
  71. package/web/src/pug/block/bubble/0.0.1/index.ls +220 -0
  72. package/web/src/pug/block/bubble/0.0.1/index.pug +28 -0
  73. package/web/src/pug/block/curve-bar/0.0.1/index.ls +89 -0
  74. package/web/src/pug/block/curve-bar/0.0.1/index.pug +16 -0
  75. package/web/src/pug/block/curve-bar/0.0.1/sample.js +338 -0
  76. package/web/src/pug/block/dumbbell/0.0.1/index.ls +90 -0
  77. package/web/src/pug/block/dumbbell/0.0.1/index.pug +1 -0
  78. package/web/src/pug/block/empty/0.0.1/index.ls +27 -0
  79. package/web/src/pug/block/empty/0.0.1/index.pug +2 -0
  80. package/web/src/pug/block/gauge/0.0.1/index.ls +100 -0
  81. package/web/src/pug/block/gauge/0.0.1/index.pug +31 -0
  82. package/web/src/pug/block/gauge/0.0.1/sample.js +338 -0
  83. package/web/src/pug/block/line/0.0.1/index.ls +265 -0
  84. package/web/src/pug/block/line/0.0.1/index.pug +33 -0
  85. package/web/src/pug/block/number/0.0.1/index.ls +69 -0
  86. package/web/src/pug/block/number/0.0.1/index.pug +20 -0
  87. package/web/src/pug/block/percent-list/0.0.1/index.ls +167 -0
  88. package/web/src/pug/block/percent-list/0.0.1/index.pug +34 -0
  89. package/web/src/pug/block/pie/0.0.1/index.ls +125 -0
  90. package/web/src/pug/block/pie/0.0.1/index.pug +19 -0
  91. package/web/src/pug/block/pie/0.0.1/sample.js +338 -0
  92. package/web/src/pug/block/pie-bubble/0.0.1/index.ls +185 -0
  93. package/web/src/pug/block/pie-bubble/0.0.1/index.pug +40 -0
  94. package/web/src/pug/block/radar/0.0.1/index.ls +113 -0
  95. package/web/src/pug/block/radar/0.0.1/index.pug +1 -0
  96. package/web/src/pug/block/radar/0.0.1/xx +285 -0
  97. package/web/src/pug/block/scatter/0.0.1/index.ls +128 -0
  98. package/web/src/pug/block/scatter/0.0.1/index.pug +30 -0
  99. package/web/src/pug/block/sheet/0.0.1/index.ls +38 -0
  100. package/web/src/pug/block/sheet/0.0.1/index.pug +5 -0
  101. package/web/src/pug/block/table/0.0.1/index.ls +40 -0
  102. package/web/src/pug/block/table/0.0.1/index.pug +3 -0
  103. package/web/src/pug/block/taiwantown/0.0.1/index.ls +48 -0
  104. package/web/src/pug/block/taiwantown/0.0.1/index.pug +52 -0
  105. package/web/src/pug/block/taiwanvillage/0.0.1/index.ls +53 -0
  106. package/web/src/pug/block/taiwanvillage/0.0.1/index.pug +52 -0
  107. package/web/src/pug/block/treemap/0.0.1/index.ls +172 -0
  108. package/web/src/pug/block/treemap/0.0.1/index.pug +42 -0
  109. package/web/src/pug/block/voronoi-treemap/0.0.1/index.ls +174 -0
  110. package/web/src/pug/block/voronoi-treemap/0.0.1/index.pug +17 -0
  111. package/web/src/pug/block/voronoi-treemap/0.0.1/sample.js +241 -0
  112. package/web/src/pug/block/wordcloud/0.0.1/index.ls +61 -0
  113. package/web/src/pug/block/wordcloud/0.0.1/index.pug +1 -0
  114. package/web/src/pug/block/worldmap/0.0.1/index.ls +83 -0
  115. package/web/src/pug/block/worldmap/0.0.1/index.pug +12 -0
  116. package/web/src/pug/index.ls +72 -0
  117. package/web/src/pug/index.pug +52 -0
  118. package/web/src/styl/index.styl +0 -0
  119. package/web/static/assets/block/cache.js +4 -0
  120. package/web/static/assets/custom-lib/d3/d3-array.v2.min.js +2 -0
  121. package/web/static/assets/custom-lib/d3/d3-axis.v2.min.js +2 -0
  122. package/web/static/assets/custom-lib/d3/d3-brush.v2.min.js +2 -0
  123. package/web/static/assets/custom-lib/d3/d3-color.v2.min.js +2 -0
  124. package/web/static/assets/custom-lib/d3/d3-dispatch.v2.min.js +2 -0
  125. package/web/static/assets/custom-lib/d3/d3-drag.v2.min.js +2 -0
  126. package/web/static/assets/custom-lib/d3/d3-ease.v2.min.js +2 -0
  127. package/web/static/assets/custom-lib/d3/d3-force-boundary.v0.0.1.min.js +2 -0
  128. package/web/static/assets/custom-lib/d3/d3-force.v2.min.js +2 -0
  129. package/web/static/assets/custom-lib/d3/d3-format.v2.min.js +2 -0
  130. package/web/static/assets/custom-lib/d3/d3-hierarchy.v2.min.js +2 -0
  131. package/web/static/assets/custom-lib/d3/d3-interpolate.v2.min.js +2 -0
  132. package/web/static/assets/custom-lib/d3/d3-path.v2.min.js +2 -0
  133. package/web/static/assets/custom-lib/d3/d3-quadtree.v2.min.js +2 -0
  134. package/web/static/assets/custom-lib/d3/d3-random.v2.min.js +2 -0
  135. package/web/static/assets/custom-lib/d3/d3-scale-chromatic.v1.min.js +2 -0
  136. package/web/static/assets/custom-lib/d3/d3-selection.v2.min.js +2 -0
  137. package/web/static/assets/custom-lib/d3/d3-shape.v2.min.js +2 -0
  138. package/web/static/assets/custom-lib/d3/d3-timer.v2.min.js +2 -0
  139. package/web/static/assets/custom-lib/d3/d3-transition.v2.min.js +2 -0
  140. package/web/static/assets/custom-lib/d3/d3.v4.js +17178 -0
  141. package/web/static/assets/custom-lib/d3/d3.v4.min.js +2 -0
  142. package/web/static/assets/data/countrycode.json +1 -0
  143. package/web/static/assets/data/world-110m.json +1 -0
  144. package/web/static/assets/data/zh-to-alpha2.json +1 -0
  145. package/web/static/assets/img/fluid.svg +16 -0
  146. package/web/static/block/axis/0.0.1/index.html +79 -0
  147. package/web/static/block/axis-bubble/0.0.1/index.html +222 -0
  148. package/web/static/block/bar/0.0.1/index.html +581 -0
  149. package/web/static/block/bar-list/0.0.1/index.html +231 -0
  150. package/web/static/block/bar-race/0.0.1/index.html +184 -0
  151. package/web/static/block/base/0.0.1/index.html +197 -0
  152. package/web/static/block/blank/0.0.1/index.html +42 -0
  153. package/web/static/block/bubble/0.0.1/index.html +402 -0
  154. package/web/static/block/bubble/0.0.1/test.html +0 -0
  155. package/web/static/block/curve-bar/0.0.1/index.html +148 -0
  156. package/web/static/block/dumbbell/0.0.1/index.html +178 -0
  157. package/web/static/block/empty/0.0.1/index.html +76 -0
  158. package/web/static/block/gauge/0.0.1/index.html +150 -0
  159. package/web/static/block/line/0.0.1/index.html +540 -0
  160. package/web/static/block/number/0.0.1/index.html +152 -0
  161. package/web/static/block/percent-list/0.0.1/index.html +316 -0
  162. package/web/static/block/pie/0.0.1/index.html +281 -0
  163. package/web/static/block/pie-bubble/0.0.1/index.html +358 -0
  164. package/web/static/block/radar/0.0.1/index.html +149 -0
  165. package/web/static/block/scatter/0.0.1/index.html +251 -0
  166. package/web/static/block/sheet/0.0.1/index.html +103 -0
  167. package/web/static/block/table/0.0.1/index.html +73 -0
  168. package/web/static/block/taiwantown/0.0.1/index.html +210 -0
  169. package/web/static/block/taiwanvillage/0.0.1/index.html +220 -0
  170. package/web/static/block/treemap/0.0.1/index.html +360 -0
  171. package/web/static/block/voronoi-treemap/0.0.1/index.html +350 -0
  172. package/web/static/block/wordcloud/0.0.1/index.html +92 -0
  173. package/web/static/block/worldmap/0.0.1/index.html +207 -0
  174. package/web/static/css/index.css +0 -0
  175. package/web/static/css/index.min.css +0 -0
  176. package/web/static/favicon.ico +0 -0
  177. package/web/static/index.html +153 -0
@@ -0,0 +1,338 @@
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
+ }
@@ -0,0 +1,185 @@
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
@@ -0,0 +1,40 @@
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
+ div(data-name="tip") #[div(ld="name")]#[div(ld="value1")]#[div(ld="value2")]
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
+ & > .pdl-cell[data-name=view]
15
+ grid-area: 1 / 1
16
+ & > .pdl-cell[data-name=legend]
17
+ grid-column: 2
18
+ grid-row: 1
19
+ padding: 0 .5em 0 .5em
20
+
21
+ [data-name=tip]
22
+ padding: .4em .5em
23
+ background: #13171a
24
+ color: #fff
25
+ border-radius: 3px
26
+ box-shadow: 0 2px 4px rgba(0,0,0,.3)
27
+ line-height: 1.2em
28
+ font-size: .9em
29
+ max-width: 10em
30
+ word-break: break-all
31
+ [ld=name]
32
+ font-size: .85em
33
+ [ld=value1], [ld=value2]
34
+ font-weight: bold
35
+ font-size: 1.1em
36
+ line-height: 1.2em
37
+
38
+
39
+
40
+ script(type="@plotdb/block"): include:lsc index.ls
@@ -0,0 +1,113 @@
1
+ <-(->it!) _
2
+
3
+ block-factory =
4
+ pkg:
5
+ name: 'radar', 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,repeatString$} = context
13
+ sample: ->
14
+ raw: [0 to 4].map (idx) ~> {val: [0 to 2].map(->0.5 + 0.5 * Math.random!), cat: idx}
15
+ binding:
16
+ radius: {key: \val}
17
+ order: {key: \cat}
18
+ config: {}
19
+ dimension:
20
+ radius: { type: \R, name: "radius of point on radar line" },
21
+ order: { type: \R, name: "order of data point" }
22
+ init: ->
23
+ svg = d3.select @svg
24
+ @gs = gs = {}
25
+ gs.all = svg.append \g
26
+ @line = d3.line!
27
+ .x (d,i) ~> @scale.r(d) * Math.cos(@scale.a(@orders[i]))
28
+ .y (d,i) ~> @scale.r(d) * -Math.sin(@scale.a(@orders[i]))
29
+ parse: ->
30
+ @orders = Array.from(new Set(@data.map(->it.order)))
31
+ @parsed = d3.transpose(@data.map(-> it.radius))
32
+ @parsed.map (d,i) -> d.idx = i
33
+ @names = <[1 2 3]> #@dimension.radius.fieldName || <[1 2 3]>
34
+ @radiusRange = d3.extent(@parsed
35
+ .map (d) -> d3.extent d
36
+ .reduce(((a,b) -> a.concat(b)), [])
37
+ )
38
+ if @radiusRange.0 == @radiusRange.1 => @radiusRange.1++
39
+
40
+ bind: ->
41
+ @gs.all.selectAll \path.data .data @parsed
42
+ ..exit!
43
+ .attr \class, ""
44
+ .transition \exit .duration 500
45
+ .attr \opacity, 0
46
+ .remove!
47
+ ..enter!
48
+ .append \path
49
+ .attr \class, \data
50
+ .attr \opacity, 0
51
+ @gs.all.selectAll \g.data-group .data @parsed
52
+ ..exit!
53
+ .attr \class, ""
54
+ .transition \exit .duration 500
55
+ .attr \opacity, 0
56
+ .remove!
57
+ ..enter!
58
+ .append \g
59
+ .attr \class, \data-group
60
+ .attr \opacity, 0
61
+ @gs.all.selectAll \g.data-group .each (d,i) ->
62
+ d3.select @ .selectAll \circle.node .data d
63
+ ..exit!
64
+ .attr \class, ''
65
+ .transition \exit .duration 500
66
+ .attr \opacity, 0
67
+ .remove!
68
+ ..enter!
69
+ .append \circle
70
+ .attr \class, \node
71
+ .attr \opacity, 0
72
+ resize: ->
73
+ size = (Math.min(@box.width, @box.height) / 2) >? 10
74
+ @scale =
75
+ r: d3.scaleLinear!domain [0, @radiusRange.1] .range [0, size]
76
+ a: d3.scalePoint!domain @orders .range [0, 2 * Math.PI]
77
+ c: d3.interpolateTurbo
78
+ if @cfg? and @cfg.palette =>
79
+ @scale.c = d3.interpolateDiscrete @cfg.palette.colors.map -> ldColor.web(it.value or it)
80
+ @aTicks = d3.range(@data.length)
81
+ render: ->
82
+ {scale, names, line, orders, orders} = @
83
+ @gs.all.attr \transform, "translate(#{@box.width/2},#{@box.height/2})"
84
+
85
+ @gs.all.selectAll \path.data
86
+ .each (d,i) ->
87
+ color = scale.c(i / (names.length - 1))
88
+ d3.select @
89
+ .attr \fill, color
90
+ .attr \fill-opacity, 0.2
91
+ .attr \stroke, color
92
+ .attr \stroke-width, 1
93
+ .transition \morph .duration 350
94
+ .attr \d, (d,i) -> line(d,i) + "Z"
95
+ .transition \opacity .duration 350
96
+ .attr \opacity, 1
97
+
98
+ @gs.all.selectAll \g.data-group
99
+ .each (d,i) ->
100
+ color = scale.c(i / (names.length - 1))
101
+ d3.select @ .selectAll \circle.node
102
+ .attr \fill, \#fff
103
+ .attr \stroke, color
104
+ .attr \stroke-width, 1
105
+ .transition \morph .duration 350
106
+ .attr \cx, (d,i) ~> scale.r(d) * Math.cos(scale.a(orders[i]))
107
+ .attr \cy, (d,i) ~> scale.r(d) * -Math.sin(scale.a(orders[i]))
108
+ .attr \r, 3
109
+ .attr \opacity, 1
110
+ .transition \opacity .duration 350
111
+ .attr \opacity, 1
112
+
113
+ return block-factory
@@ -0,0 +1 @@
1
+ div: script(type="@plotdb/block"): include:lsc index.ls