|
1 |
| -(function() { |
2 |
| - var extra = {}; |
3 |
| - |
4 |
| - c3.chart.internal.fn.additionalConfig = { |
5 |
| - data_pairs: [], |
6 |
| - }; |
7 |
| - |
8 |
| - c3.chart.internal.fn.beforeInit = function (config) { |
9 |
| - |
10 |
| - var that = this; |
11 |
| - |
12 |
| - // update internals only when chart type is "bubble" |
13 |
| - if (config.data.type !== 'bubble') { |
14 |
| - return; |
15 |
| - } |
16 |
| - |
17 |
| - // Set extra to ba able to be used in other part |
18 |
| - this.extra = extra; |
19 |
| - |
20 |
| - extra.getKey = function (x, y) { |
21 |
| - return x + '::' + y; |
22 |
| - }; |
23 |
| - |
24 |
| - this.config.data_type = 'scatter'; |
25 |
| - |
26 |
| - this.config.axis_x_padding = 0; |
27 |
| - this.config.axis_y_padding = 0; |
28 |
| - this.config.axis_x_tick_centered = true; |
29 |
| - this.config.axis_x_tick_format = function (d) { |
30 |
| - return extra.names[d]; |
31 |
| - }; |
32 |
| - this.config.axis_y_tick_format = function (d) { |
33 |
| - return extra.names[d]; |
34 |
| - }; |
35 |
| - |
36 |
| - if (!config.color || !config.color.pattern) { |
37 |
| - this.config.color_pattern = ['#1f77b4']; |
38 |
| - } |
39 |
| - |
40 |
| - this.config.point_r = function (d) { |
41 |
| - var names = extra.names, values = extra.values, base_length = extra.base_length, |
42 |
| - x = names[d.x], y = d.id, |
43 |
| - key = extra.getKey(x, y), value = !values[key] ? 0 : values[key], |
44 |
| - max, max_r, max_area, a, area, r; |
45 |
| - |
46 |
| - if (!base_length) { |
47 |
| - base_length = extra.base_length = d3.min([ |
48 |
| - that.svg.select('.c3-axis.c3-axis-y path').node().getTotalLength(), |
49 |
| - that.svg.select('.c3-axis.c3-axis-x path').node().getTotalLength(), |
50 |
| - ]); |
51 |
| - } |
52 |
| - |
53 |
| - max = d3.max(Object.keys(values).map(function (key) { return values[key]; })); |
54 |
| - max_r = (base_length / (names.length * 2)); |
55 |
| - max_area = max_r * max_r * Math.PI; |
56 |
| - |
57 |
| - a = max_area / max; |
58 |
| - |
59 |
| - area = value * a; |
60 |
| - r = Math.sqrt(area / Math.PI); |
61 |
| - |
62 |
| - return r; |
63 |
| - }; |
64 |
| - this.config.point_sensitivity = 25; |
65 |
| - this.config.point_focus_expand_enabled = false; |
66 |
| - |
67 |
| - this.config.legend_show = false; |
68 |
| - |
69 |
| - if (!config.tooltip || !config.tooltip.contents) { |
70 |
| - this.config.tooltip_contents = function (d, defaultTitleFormat, defaultValueFormat, color) { |
71 |
| - var x = extra.names[d[0].x], y = d[0].name, v = extra.values[extra.getKey(x, y)], text; |
72 |
| - |
73 |
| - text = "<table class='" + this.CLASS.tooltip + "'>"; |
74 |
| - text += "<tr><th colspan='2'>" + x + " / " + y + "</th></tr>"; |
75 |
| - text += "<tr><td class='value'>" + (!v ? 0 : v) + "</td></tr>"; |
76 |
| - text += "</table>"; |
77 |
| - |
78 |
| - return text; |
79 |
| - }; |
80 |
| - } |
81 |
| - |
82 |
| - // construct bubble chart data and setup config based on the values |
83 |
| - |
84 |
| - var xs = this.config.data_pairs.map(function (pair) { return pair.x; }), |
85 |
| - ys = this.config.data_pairs.map(function (pair) { return pair.y; }); |
86 |
| - |
87 |
| - extra.names = d3.set(xs.concat(ys)).values().sort(); |
88 |
| - |
89 |
| - this.config.axis_y_tick_values = extra.names.map(function (name, i) { return i; }); |
90 |
| - |
91 |
| - var data_xs = {}; |
92 |
| - extra.names.forEach(function (name) { |
93 |
| - data_xs[name] = name + '_x'; |
94 |
| - }); |
95 |
| - var data_columns_xs = Object.keys(data_xs).map(function (key) { |
96 |
| - return [data_xs[key]].concat(extra.names.map(function (name, i) { return i; })); |
97 |
| - }); |
98 |
| - var data_columns_values = extra.names.map(function (name, i) { |
99 |
| - return [name].concat(extra.names.map(function (name) { return i; })); |
100 |
| - }); |
101 |
| - this.config.data_xs = data_xs; |
102 |
| - this.config.data_columns = data_columns_xs.concat(data_columns_values); |
103 |
| - |
104 |
| - var values = {}; |
105 |
| - this.config.data_pairs.forEach(function (pair) { |
106 |
| - if (!pair.x || !pair.y) { |
107 |
| - throw "x and y are required in data."; |
108 |
| - } |
109 |
| - values[extra.getKey(pair.x, pair.y)] = pair.value; |
110 |
| - }); |
111 |
| - extra.values = values; |
112 |
| - |
113 |
| - this.config.axis_x_min = this.config.axis_y_min = -0.5; |
114 |
| - this.config.axis_x_max = this.config.axis_y_max = extra.names.length - 0.5; |
115 |
| - }; |
116 |
| -})(window); |
| 1 | +;(function() { |
| 2 | + var extra = {} |
| 3 | + |
| 4 | + c3.chart.internal.fn.additionalConfig = { |
| 5 | + data_pairs: [] |
| 6 | + } |
| 7 | + |
| 8 | + c3.chart.internal.fn.beforeInit = function(config) { |
| 9 | + var that = this |
| 10 | + |
| 11 | + // update internals only when chart type is "bubble" |
| 12 | + if (config.data.type !== 'bubble') { |
| 13 | + return |
| 14 | + } |
| 15 | + |
| 16 | + // Set extra to ba able to be used in other part |
| 17 | + this.extra = extra |
| 18 | + |
| 19 | + extra.getKey = function(x, y) { |
| 20 | + return x + '::' + y |
| 21 | + } |
| 22 | + |
| 23 | + this.config.data_type = 'scatter' |
| 24 | + |
| 25 | + this.config.axis_x_padding = 0 |
| 26 | + this.config.axis_y_padding = 0 |
| 27 | + this.config.axis_x_tick_centered = true |
| 28 | + this.config.axis_x_tick_format = function(d) { |
| 29 | + return extra.names[d] |
| 30 | + } |
| 31 | + this.config.axis_y_tick_format = function(d) { |
| 32 | + return extra.names[d] |
| 33 | + } |
| 34 | + |
| 35 | + if (!config.color || !config.color.pattern) { |
| 36 | + this.config.color_pattern = ['#1f77b4'] |
| 37 | + } |
| 38 | + |
| 39 | + this.config.point_r = function(d) { |
| 40 | + var names = extra.names, |
| 41 | + values = extra.values, |
| 42 | + base_length = extra.base_length, |
| 43 | + x = names[d.x], |
| 44 | + y = d.id, |
| 45 | + key = extra.getKey(x, y), |
| 46 | + value = !values[key] ? 0 : values[key], |
| 47 | + max, |
| 48 | + max_r, |
| 49 | + max_area, |
| 50 | + a, |
| 51 | + area, |
| 52 | + r |
| 53 | + |
| 54 | + if (!base_length) { |
| 55 | + base_length = extra.base_length = d3.min([ |
| 56 | + that.svg |
| 57 | + .select('.c3-axis.c3-axis-y path') |
| 58 | + .node() |
| 59 | + .getTotalLength(), |
| 60 | + that.svg |
| 61 | + .select('.c3-axis.c3-axis-x path') |
| 62 | + .node() |
| 63 | + .getTotalLength() |
| 64 | + ]) |
| 65 | + } |
| 66 | + |
| 67 | + max = d3.max( |
| 68 | + Object.keys(values).map(function(key) { |
| 69 | + return values[key] |
| 70 | + }) |
| 71 | + ) |
| 72 | + max_r = base_length / (names.length * 2) |
| 73 | + max_area = max_r * max_r * Math.PI |
| 74 | + |
| 75 | + a = max_area / max |
| 76 | + |
| 77 | + area = value * a |
| 78 | + r = Math.sqrt(area / Math.PI) |
| 79 | + |
| 80 | + return r |
| 81 | + } |
| 82 | + this.config.point_sensitivity = 25 |
| 83 | + this.config.point_focus_expand_enabled = false |
| 84 | + |
| 85 | + this.config.legend_show = false |
| 86 | + |
| 87 | + if (!config.tooltip || !config.tooltip.contents) { |
| 88 | + this.config.tooltip_contents = function( |
| 89 | + d, |
| 90 | + defaultTitleFormat, |
| 91 | + defaultValueFormat, |
| 92 | + color |
| 93 | + ) { |
| 94 | + var x = extra.names[d[0].x], |
| 95 | + y = d[0].name, |
| 96 | + v = extra.values[extra.getKey(x, y)], |
| 97 | + text |
| 98 | + |
| 99 | + text = "<table class='" + this.CLASS.tooltip + "'>" |
| 100 | + text += "<tr><th colspan='2'>" + x + ' / ' + y + '</th></tr>' |
| 101 | + text += "<tr><td class='value'>" + (!v ? 0 : v) + '</td></tr>' |
| 102 | + text += '</table>' |
| 103 | + |
| 104 | + return text |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // construct bubble chart data and setup config based on the values |
| 109 | + |
| 110 | + var xs = this.config.data_pairs.map(function(pair) { |
| 111 | + return pair.x |
| 112 | + }), |
| 113 | + ys = this.config.data_pairs.map(function(pair) { |
| 114 | + return pair.y |
| 115 | + }) |
| 116 | + |
| 117 | + extra.names = d3 |
| 118 | + .set(xs.concat(ys)) |
| 119 | + .values() |
| 120 | + .sort() |
| 121 | + |
| 122 | + this.config.axis_y_tick_values = extra.names.map(function(name, i) { |
| 123 | + return i |
| 124 | + }) |
| 125 | + |
| 126 | + var data_xs = {} |
| 127 | + extra.names.forEach(function(name) { |
| 128 | + data_xs[name] = name + '_x' |
| 129 | + }) |
| 130 | + var data_columns_xs = Object.keys(data_xs).map(function(key) { |
| 131 | + return [data_xs[key]].concat( |
| 132 | + extra.names.map(function(name, i) { |
| 133 | + return i |
| 134 | + }) |
| 135 | + ) |
| 136 | + }) |
| 137 | + var data_columns_values = extra.names.map(function(name, i) { |
| 138 | + return [name].concat( |
| 139 | + extra.names.map(function(name) { |
| 140 | + return i |
| 141 | + }) |
| 142 | + ) |
| 143 | + }) |
| 144 | + this.config.data_xs = data_xs |
| 145 | + this.config.data_columns = data_columns_xs.concat(data_columns_values) |
| 146 | + |
| 147 | + var values = {} |
| 148 | + this.config.data_pairs.forEach(function(pair) { |
| 149 | + if (!pair.x || !pair.y) { |
| 150 | + throw 'x and y are required in data.' |
| 151 | + } |
| 152 | + values[extra.getKey(pair.x, pair.y)] = pair.value |
| 153 | + }) |
| 154 | + extra.values = values |
| 155 | + |
| 156 | + this.config.axis_x_min = this.config.axis_y_min = -0.5 |
| 157 | + this.config.axis_x_max = this.config.axis_y_max = extra.names.length - 0.5 |
| 158 | + } |
| 159 | +})(window) |
0 commit comments