UNPKG

formula-treemap

Version:

한약재를 구성하는 재료(성분)들을 보기 좋게 시각화 한 리액트 컴포넌트 패키지입니다.

104 lines (76 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateModelColors = void 0; var _lodash = _interopRequireDefault(require("lodash")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } var updateModelColors = function updateModelColors(groups, dataGroups) { // Assign colors to each item. We'll need to handle the discrete // and continuous cases separately. var colors; var legendEntries = []; // legendAlignment; // var min = _.min(this.dataset, function (company) { // return company[coloringProp]; var min = _lodash.default.minBy(groups, function (group) { return group.weight; }).weight; var max = _lodash.default.maxBy(groups, function (group) { return group.weight; }).weight; var range = Math.sqrt(max - min) / 19; colors = ['f9f6fb', 'f2eaf6', 'ebdef2', 'e5d3ed', 'dec7e9', 'd7bbe4', 'd0afdf', 'c9a4db', 'c298d6', 'bb8cd2', 'b481cd', 'ad75c8', 'a669c4', '9f5dbf', '9852bb', '9147b5', '8842a9', '7e3e9e', '6c3586', '542969']; var undefinedColor = '#cccccc'; var hasUndefined = false; var newDataGroups = _toConsumableArray(dataGroups); // console.log(range); newDataGroups.forEach(function (group) { // Use grey for undefined values var value = group.weight; // console.log(Math.round(Math.sqrt(value - min) / range)); hasUndefined |= _lodash.default.isUndefined(value); group.color = '#' + (_lodash.default.isUndefined(value) ? undefinedColor : colors[Math.round(Math.sqrt(value - min) / range)]); }); // Prepare legend entries. We need to compute some reasonable steps in which to present // the legend for our continuous scale. var maxLegendEntries = 10, stepsPerMagnitude = 20; var rawIncrement = (max - min) / maxLegendEntries; // console.log(rawIncrement); var magnitude = Math.pow(10, Math.ceil(log10(rawIncrement))); var increment = magnitude * Math.ceil(stepsPerMagnitude * rawIncrement / magnitude) / stepsPerMagnitude; // Always put min and max on the scale, fill values in-between with the computed increment. legendEntries.push({ color: '#' + colors[19], value: max }); var value = increment * Math.ceil((max - increment) / increment); while (value > min) { // console.log(value); legendEntries.push({ color: '#' + colors[Math.round(Math.sqrt(value - min) / range)], value: value }); value -= increment; } legendEntries.push({ color: '#' + colors[0], value: min }); if (hasUndefined) { legendEntries.push({ color: undefinedColor, value: 'n/a' }); } // legendAlignment = 'right'; // this.legend.display(legendEntries, legendAlignment); function log10(n) { return Math.log(n) / Math.log(10); } // console.log(legendEntries); return { newDataGroups: newDataGroups, legendEntries: legendEntries }; }; exports.updateModelColors = updateModelColors;