component/bars.js

  1. import * as d3 from "d3";
  2. import dataTransform from "../dataTransform.js";
  3. import { attachEventListners, dispatch } from "../events.js";
  4. import { colorParse } from "../colorHelper.js";
  5. /**
  6. * Reusable 3D Bar Chart Component
  7. *
  8. * @module
  9. */
  10. export default function() {
  11. /* Default Properties */
  12. let dimensions = { x: 40, y: 40, z: 2 };
  13. let colors = ["orange", "red", "yellow", "steelblue", "green"];
  14. let classed = "d3X3dBars";
  15. let transparency = 0;
  16. /* Scales */
  17. let xScale;
  18. let yScale;
  19. let colorScale;
  20. /**
  21. * Initialise Data and Scales
  22. *
  23. * @private
  24. * @param {Array} data - Chart data.
  25. */
  26. const init = function(data) {
  27. const { columnKeys, valueMax } = dataTransform(data).summary();
  28. const valueExtent = [0, valueMax];
  29. const { x: dimensionX, y: dimensionY } = dimensions;
  30. if (typeof xScale === "undefined") {
  31. xScale = d3.scaleBand()
  32. .domain(columnKeys)
  33. .rangeRound([0, dimensionX])
  34. .padding(0.3);
  35. }
  36. if (typeof yScale === "undefined") {
  37. yScale = d3.scaleLinear()
  38. .domain(valueExtent)
  39. .range([0, dimensionY]);
  40. }
  41. if (typeof colorScale === "undefined") {
  42. colorScale = d3.scaleOrdinal()
  43. .domain(columnKeys)
  44. .range(colors);
  45. }
  46. };
  47. /**
  48. * Constructor
  49. *
  50. * @constructor
  51. * @alias bars
  52. * @param {d3.selection} selection - The chart holder D3 selection.
  53. */
  54. const my = function(selection) {
  55. selection.each(function(data) {
  56. init(data);
  57. const element = d3.select(this)
  58. .classed(classed, true)
  59. .attr("id", (d) => d.key);
  60. const shape = (el) => {
  61. const shape = el.append("Shape");
  62. attachEventListners(shape);
  63. shape.append("Box")
  64. .attr("size", "1.0 1.0 1.0");
  65. shape.append("Appearance")
  66. .append("Material")
  67. .attr("diffuseColor", (d) => {
  68. // If colour scale is linear then use value for scale.
  69. let color = colorScale(d.key);
  70. if (typeof colorScale.interpolate === "function") {
  71. color = colorScale(d.value);
  72. }
  73. return colorParse(color);
  74. })
  75. .attr("ambientIntensity", 0.1)
  76. .attr("transparency", transparency);
  77. return shape;
  78. };
  79. const bars = element.selectAll(".bar")
  80. .data((d) => d.values, (d) => d.key);
  81. bars.enter()
  82. .append("Transform")
  83. .classed("bar", true)
  84. .call(shape)
  85. .merge(bars)
  86. .transition()
  87. .attr("scale", (d) => {
  88. let x = xScale.bandwidth();
  89. let y = yScale(d.value);
  90. let z = dimensions.z;
  91. return x + " " + y + " " + z;
  92. })
  93. .attr("translation", (d) => {
  94. let x = xScale(d.key);
  95. let y = yScale(d.value) / 2;
  96. let z = 0.0;
  97. return x + " " + y + " " + z;
  98. });
  99. bars.exit()
  100. .remove();
  101. });
  102. };
  103. /**
  104. * Dimensions Getter / Setter
  105. *
  106. * @param {{x: number, y: number, z: number}} _v - 3D object dimensions.
  107. * @returns {*}
  108. */
  109. my.dimensions = function(_v) {
  110. if (!arguments.length) return dimensions;
  111. dimensions = _v;
  112. return this;
  113. };
  114. /**
  115. * X Scale Getter / Setter
  116. *
  117. * @param {d3.scale} _v - D3 scale.
  118. * @returns {*}
  119. */
  120. my.xScale = function(_v) {
  121. if (!arguments.length) return xScale;
  122. xScale = _v;
  123. return my;
  124. };
  125. /**
  126. * Y Scale Getter / Setter
  127. *
  128. * @param {d3.scale} _v - D3 scale.
  129. * @returns {*}
  130. */
  131. my.yScale = function(_v) {
  132. if (!arguments.length) return yScale;
  133. yScale = _v;
  134. return my;
  135. };
  136. /**
  137. * Color Scale Getter / Setter
  138. *
  139. * @param {d3.scale} _v - D3 scale.
  140. * @returns {*}
  141. */
  142. my.colorScale = function(_v) {
  143. if (!arguments.length) return colorScale;
  144. colorScale = _v;
  145. return my;
  146. };
  147. /**
  148. * Colors Getter / Setter
  149. *
  150. * @param {Array} _v - Array of colours used by color scale.
  151. * @returns {*}
  152. */
  153. my.colors = function(_v) {
  154. if (!arguments.length) return colors;
  155. colors = _v;
  156. return my;
  157. };
  158. /**
  159. * Transparency Getter / Setter
  160. *
  161. * @param {Number} _v - Transparency level 0 - 1.
  162. * @returns {*}
  163. */
  164. my.transparency = function(_v) {
  165. if (!arguments.length) return transparency;
  166. transparency = _v;
  167. return my;
  168. };
  169. /**
  170. * Dispatch On Getter
  171. *
  172. * @returns {*}
  173. */
  174. my.on = function() {
  175. let value = dispatch.on.apply(dispatch, arguments);
  176. return value === dispatch ? my : value;
  177. };
  178. return my;
  179. }