import * as d3 from "d3";
import componentBubbles from "./bubbles.js";
import { arrayUnique } from "../arrayUnique.js";
import { dispatch } from "../events.js";
import { attachAccessors } from "../accessors.js";
import { assignCoordinateScales, coordinateSeriesData } from "./seriesGroup.js";
/**
* Reusable 3D Multi Series Bubble Chart Component
*
* @module
*/
export default function() {
/* Default Properties */
const state = {
dimensions: { x: 40, y: 40, z: 40 },
colors: ["green", "red", "yellow", "steelblue", "orange"],
xScale: undefined,
yScale: undefined,
zScale: undefined,
colorScale: undefined,
sizeScale: undefined,
sizeRange: [0.5, 3.0]
};
let classed = "d3X3dBubblesMultiSeries";
/* Scales */
let colorDomain = [];
/* Components */
const bubbles = componentBubbles()
.mappings({ x: 'x', y: 'y', z: 'z', size: 'size', color: 'color' })
.colors(d3.schemeRdYlGn[8])
.sizeRange([2, 2]);
/**
* Initialise Data and Scales
*
* @private
* @param {Array} data - Chart data.
*/
const init = function(data) {
const rowKeys = assignCoordinateScales(state, data);
colorDomain = arrayUnique(colorDomain, rowKeys);
state.colorScale = d3.scaleOrdinal()
.domain(colorDomain)
.range(state.colors);
};
/**
* Constructor
*
* @constructor
* @alias bubblesMultiSeries
* @param {d3.selection} selection - The chart holder D3 selection.
*/
const my = function(selection) {
selection.each(function(data) {
init(data);
const element = d3.select(this)
.classed(classed, true);
bubbles.xScale(state.xScale)
.yScale(state.yScale)
.zScale(state.zScale)
.sizeScale(state.sizeScale);
const addBubbles = function(d) {
const color = state.colorScale(d.key);
bubbles.color(color);
d3.select(this).call(bubbles);
};
const bubbleGroup = element.selectAll(".bubbleGroup")
.data(coordinateSeriesData, (d) => d.key);
bubbleGroup.enter()
.append("Group")
.classed("bubbleGroup", true)
.merge(bubbleGroup)
.transition()
.each(addBubbles);
bubbleGroup.exit()
.remove();
});
};
/**
* Dispatch On Getter
*
* @returns {*}
*/
my.on = function() {
let value = dispatch.on.apply(dispatch, arguments);
return value === dispatch ? my : value;
};
/**
* Getter / setters. No argument returns the value. One argument sets it and returns the component.
*
* @param {{x: number, y: number, z: number}} dimensions - 3D object dimensions.
* @param {d3.scale} xScale - D3 Scale.
* @param {d3.scale} yScale - D3 scale.
* @param {d3.scale} zScale - D3 Scale.
* @param {d3.scale} colorScale - D3 color scale.
* @param {d3.scale} sizeScale - D3 size scale.
* @param {number[]} sizeRange - Size min and max (e.g. [0.5, 3.0]).
* @param {Array} colors - Array of colours used by color scale.
*/
attachAccessors(my, state);
return my;
}