/** * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */ import CLASS from "../../config/classes"; import {generateWait} from "../../module/generator"; import {diffDomain} from "../../module/util"; export default { /** * Generate flow * @param {object} args option object * @returns {function} * @private */ generateFlow(args): Function { const $$ = this; const {config, data, state, $el} = $$; return function() { // destroy() empties the instance while the redraw transition is waited on if (!$$.$el) { return; } const flowLength = args.flow.length; // set flag state.flowing = true; // remove head data after rendered data.targets.forEach(d => { d.values.splice(0, flowLength); }); // Increment dataGeneration to invalidate data-dependent caches state.dataGeneration++; // update elements related to x scale if ($$.updateXGrid) { $$.updateXGrid(true); } // target elements const elements = {}; [ "axis.x", "grid.x", "gridLines.x", "region.list", "text", "bar", "line", "area", "circle" ] .forEach(v => { const name = v.split("."); let node = $el[name[0]]; if (node && name.length > 1) { node = node[name[1]]; } if (node?.size()) { elements[v] = node; } }); // the focus slides with its data and the flow end moves it to the data under the pointer if (config.axis_rotated) { $$.hideGridFocus?.(); } else { const xFocus = $el.main.selectAll( `line.${CLASS.xgridFocus}:not(.${CLASS.xgridFocusContinuous})` ); xFocus.size() && (elements["gridFocus.x"] = xFocus); $el.main.selectAll(`line.${CLASS.ygridFocus}`).style("visibility", "hidden"); } $$.setFlowList(elements, args); }; }, /** * Set flow list * @param {object} elements Target elements * @param {object} args option object * @private */ setFlowList(elements, args): void { const $$ = this; const {flow, targets} = args; const { duration = args.duration, orgDataCount } = flow; const transform = $$.getFlowTransform(targets, orgDataCount); const wait = generateWait(); let n; wait.add(Object.keys(elements).map(v => { n = elements[v] .transition() .ease((t: number): number => +t) .duration(duration); if (v === "axis.x") { n = n.call(g => { $$.axis.x.setTransition(g).create(g); }); } else if (v === "region.list") { n = n.filter($$.isRegionOnX) .attr("transform", transform); } else { n = n.attr("transform", transform); } return n; })); n.call(wait, () => { $$.cleanUpFlow(elements, args); }); }, /** * Clean up flow * @param {object} elements Target elements * @param {object} args option object * @private */ cleanUpFlow(elements, args): void { const $$ = this; // destroy() interrupts the flow transition, which the wait counts as ended, // and empties the instance before this runs if (!$$.$el) { return; } const {config, state, $el: {svg}} = $$; const isRotated = config.axis_rotated; const {flow, shape, xv} = args; const {cx, cy, xForText, yForText} = shape.pos; const { done = () => {}, length: flowLength } = flow; // Remove flowed elements if (flowLength) { // point.focus.only draws one circle per data series, which the focus moves to any index const isFocusCircle = $$.isPointFocusOnly() ? (node: Element) => node.parentElement?.classList.contains(CLASS.circles) : () => false; ["circle", "text", "shape", "eventRect"].forEach(v => { const target: string[] = []; for (let i = 0; i < flowLength; i++) { target.push(`.${CLASS[v]}-${i}`); } svg.selectAll(`.${CLASS[`${v}s`]}`) // circles, shapes, texts, eventRects .selectAll(target) .filter(function() { return !isFocusCircle(this); }) .remove(); }); svg.select(`.${CLASS.xgrid}`) .remove(); } // draw again for removing flowed elements and reverting attr Object.keys(elements).forEach(v => { const n = elements[v]; if (v !== "axis.x") { n.attr("transform", null); } if (v === "grid.x") { n.attr(state.xgridAttr); } else if (v === "gridLines.x") { n.attr("x1", isRotated ? 0 : xv) .attr("x2", isRotated ? state.width : xv); n.select("text") .attr("x", isRotated ? state.width : 0) .attr("y", xv); } else if (/^(area|bar|line)$/.test(v)) { n.attr("d", shape.type[v]); } else if (v === "text") { n.attr("x", xForText) .attr("y", yForText) .style("fill-opacity", $$.opacityForText.bind($$)); } else if (v === "circle") { if ($$.isCirclePoint()) { n.attr("cx", cx).attr("cy", cy); } else { const xFunc = d => cx(d) - config.point_r; const yFunc = d => cy(d) - config.point_r; n.attr("x", xFunc).attr("y", yFunc); } } else if (v === "region.list") { n.select("rect").filter($$.isRegionOnX) .attr("x", $$.regionX.bind($$)) .attr("width", $$.regionWidth.bind($$)); } }); config.interaction_enabled && $$.redrawEventRect(); state.flowing = false; // other data is under the pointer now: show its focus and tooltip if ($$.$el.tooltip?.style("display") === "block") { $$.updateTooltipOnRedraw(); } else { $$.hideGridFocus?.(); } // callback for end of flow done.call($$.api); }, /** * Get flow transform value * @param {object} targets target * @param {number} orgDataCount original data count * @returns {string} * @private */ getFlowTransform(targets, orgDataCount): string { const $$ = this; const {data, scale: {x}} = $$; const dataValues = data.targets[0].values; let translateX; // update x domain to generate axis elements for flow const orgDomain = x.domain(); const domain = $$.updateXDomain(targets, true, true); // generate transform to flow if (!orgDataCount) { // if empty if (dataValues.length !== 1) { translateX = x(orgDomain[0]) - x(domain[0]); } else { if ($$.axis.isTimeSeries()) { const flowStart = $$.getValueOnIndex(dataValues, 0); const flowEnd = $$.getValueOnIndex(dataValues, dataValues.length - 1); translateX = x(flowStart.x) - x(flowEnd.x); } else { translateX = diffDomain(domain) / 2; } } } else { // The flowed-out head is already spliced from the data, so the slide is // measured by how far the domain start moved. translateX = x(orgDomain[0]) - x(domain[0]); } const scaleX = diffDomain(orgDomain) / diffDomain(domain); return `translate(${translateX},0) scale(${scaleX},1)`; } };