dc.js Source: utils.js (original) (raw)

/**

/**

/**

};

/**

};

/**

/**

/**

}; dc.utils.printSingleValue.fformat = d3.format('.2f');

// convert 'day' to 'timeDay' and similar dc.utils.toTimeFunc = function (t) { return 'time' + t.charAt(0).toUpperCase() + t.slice(1); };

/**

};

/**

};

/**

/**

/**

/**

/**

/**

/**

/**

/**

/**

/**

};

// ******** Sunburst Chart ******** dc.utils.allChildren = function (node) { var paths = []; paths.push(node.path); console.log('currentNode', node); if (node.children) { for (var i = 0; i < node.children.length; i++) { paths = paths.concat(dc.utils.allChildren(node.children[i])); } } return paths; };

// builds a d3 Hierarchy from a collection // TODO: turn this monster method something better. dc.utils.toHierarchy = function (list, accessor) { var root = {'key': 'root', 'children': []}; for (var i = 0; i < list.length; i++) { var data = list[i]; var parts = data.key; var value = accessor(data); var currentNode = root; for (var j = 0; j < parts.length; j++) { var currentPath = parts.slice(0, j + 1); var children = currentNode.children; var nodeName = parts[j]; var childNode; if (j + 1 < parts.length) { // Not yet at the end of the sequence; move down the tree. childNode = findChild(children, nodeName);

            // If we don't already have a child node for this branch, create it.
            if (childNode === void 0) {
                childNode = {'key': nodeName, 'children': [], 'path': currentPath};
                children.push(childNode);
            }
            currentNode = childNode;
        } else {
            // Reached the end of the sequence; create a leaf node.
            childNode = {'key': nodeName, 'value': value, 'data': data, 'path': currentPath};
            children.push(childNode);
        }
    }
}
return root;

};

function findChild (children, nodeName) { for (var k = 0; k < children.length; k++) { if (children[k].key === nodeName) { return children[k]; } } }

dc.utils.getAncestors = function (node) { var path = []; var current = node; while (current.parent) { path.unshift(current.name); current = current.parent; } return path; };

dc.utils.arraysIdentical = function (a, b) { var i = a.length; if (i !== b.length) { return false; } while (i--) { if (a[i] !== b[i]) { return false; } } return true; };

if (typeof Object.assign !== 'function') { // Must be writable: true, enumerable: false, configurable: true Object.defineProperty(Object, 'assign', { value: function assign (target, varArgs) { // .length of function is 2 'use strict'; if (target === null) { // TypeError if undefined or null throw new TypeError('Cannot convert undefined or null to object'); }

        var to = Object(target);

        for (var index = 1; index < arguments.length; index++) {
            var nextSource = arguments[index];

            if (nextSource !== null) { // Skip over if undefined or null
                for (var nextKey in nextSource) {
                    // Avoid bugs when hasOwnProperty is shadowed
                    if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
                        to[nextKey] = nextSource[nextKey];
                    }
                }
            }
        }
        return to;
    },
    writable: true,
    configurable: true
});

}