bubble.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. (function() {
  2. var extra = {};
  3. c3.chart.internal.fn.additionalConfig = {
  4. data_pairs: [],
  5. };
  6. c3.chart.internal.fn.beforeInit = function (config) {
  7. var that = this;
  8. // update internals only when chart type is "bubble"
  9. if (config.data.type !== 'bubble') {
  10. return;
  11. }
  12. // Set extra to ba able to be used in other part
  13. this.extra = extra;
  14. extra.getKey = function (x, y) {
  15. return x + '::' + y;
  16. };
  17. this.config.data_type = 'scatter';
  18. this.config.axis_x_padding = 0;
  19. this.config.axis_y_padding = 0;
  20. this.config.axis_x_tick_centered = true;
  21. this.config.axis_x_tick_format = function (d) {
  22. return extra.names[d];
  23. };
  24. this.config.axis_y_tick_format = function (d) {
  25. return extra.names[d];
  26. };
  27. if (!config.color || !config.color.pattern) {
  28. this.config.color_pattern = ['#1f77b4'];
  29. }
  30. this.config.point_r = function (d) {
  31. var names = extra.names, values = extra.values, base_length = extra.base_length,
  32. x = names[d.x], y = d.id,
  33. key = extra.getKey(x, y), value = !values[key] ? 0 : values[key],
  34. max, max_r, max_area, a, area, r;
  35. if (!base_length) {
  36. base_length = extra.base_length = d3.min([
  37. that.svg.select('.c3-axis.c3-axis-y path').node().getTotalLength(),
  38. that.svg.select('.c3-axis.c3-axis-x path').node().getTotalLength(),
  39. ]);
  40. }
  41. max = d3.max(Object.keys(values).map(function (key) { return values[key]; }));
  42. max_r = (base_length / (names.length * 2));
  43. max_area = max_r * max_r * Math.PI;
  44. a = max_area / max;
  45. area = value * a;
  46. r = Math.sqrt(area / Math.PI);
  47. return r;
  48. };
  49. this.config.point_sensitivity = 25;
  50. this.config.point_focus_expand_enabled = false;
  51. this.config.legend_show = false;
  52. if (!config.tooltip || !config.tooltip.contents) {
  53. this.config.tooltip_contents = function (d, defaultTitleFormat, defaultValueFormat, color) {
  54. var x = extra.names[d[0].x], y = d[0].name, v = extra.values[extra.getKey(x, y)], text;
  55. text = "<table class='" + this.CLASS.tooltip + "'>";
  56. text += "<tr><th colspan='2'>" + x + "&nbsp;/&nbsp;" + y + "</th></tr>";
  57. text += "<tr><td class='value'>" + (!v ? 0 : v) + "</td></tr>";
  58. text += "</table>";
  59. return text;
  60. };
  61. }
  62. // construct bubble chart data and setup config based on the values
  63. var xs = this.config.data_pairs.map(function (pair) { return pair.x; }),
  64. ys = this.config.data_pairs.map(function (pair) { return pair.y; });
  65. extra.names = d3.set(xs.concat(ys)).values().sort();
  66. this.config.axis_y_tick_values = extra.names.map(function (name, i) { return i; });
  67. var data_xs = {};
  68. extra.names.forEach(function (name) {
  69. data_xs[name] = name + '_x';
  70. });
  71. var data_columns_xs = Object.keys(data_xs).map(function (key) {
  72. return [data_xs[key]].concat(extra.names.map(function (name, i) { return i; }));
  73. });
  74. var data_columns_values = extra.names.map(function (name, i) {
  75. return [name].concat(extra.names.map(function (name) { return i; }));
  76. });
  77. this.config.data_xs = data_xs;
  78. this.config.data_columns = data_columns_xs.concat(data_columns_values);
  79. var values = {};
  80. this.config.data_pairs.forEach(function (pair) {
  81. if (!pair.x || !pair.y) {
  82. throw "x and y are required in data.";
  83. }
  84. values[extra.getKey(pair.x, pair.y)] = pair.value;
  85. });
  86. extra.values = values;
  87. this.config.axis_x_min = this.config.axis_y_min = -0.5;
  88. this.config.axis_x_max = this.config.axis_y_max = extra.names.length - 0.5;
  89. };
  90. })(window);