| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- "use strict";
- var __rest = (this && this.__rest) || function (s, e) {
- var t = {};
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
- t[p] = s[p];
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
- t[p[i]] = s[p[i]];
- }
- return t;
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Gauge = void 0;
- const util_1 = require("@antv/util");
- const g_1 = require("@antv/g");
- const helper_1 = require("../utils/helper");
- const coordinate_1 = require("../utils/coordinate");
- const coordinate_2 = require("../coordinate");
- const utils_1 = require("../shape/utils");
- const selection_1 = require("../utils/selection");
- function dataTransform(data, scale) {
- const { name = 'score', target, total, percent, thresholds = [] } = data;
- const _target = percent || target;
- const _total = percent ? 1 : total;
- const newScale = Object.assign({ y: {
- domain: [0, _total],
- } }, scale);
- if (!thresholds.length) {
- return {
- targetData: [{ x: name, y: _target, color: 'target' }],
- totalData: [
- { x: name, y: _target, color: 'target' },
- { x: name, y: _total - _target, color: 'total' },
- ],
- target: _target,
- total: _total,
- scale: newScale,
- };
- }
- return {
- targetData: [{ x: name, y: _target, color: 'target' }],
- totalData: thresholds.map((d, i) => ({
- x: name,
- y: i >= 1 ? d - thresholds[i - 1] : d,
- color: i,
- })),
- target: _target,
- total: _total,
- scale: newScale,
- };
- }
- function getTextContent(textStyle, { target, total }) {
- const { content } = textStyle;
- return content ? content(target, total) : target.toString();
- }
- const indicatorShape = (options) => {
- const { shape, radius } = options, style = __rest(options, ["shape", "radius"]);
- const pointerStyle = (0, helper_1.subObject)(style, 'pointer');
- const pinStyle = (0, helper_1.subObject)(style, 'pin');
- const { shape: pointerShape } = pointerStyle, resPointerStyle = __rest(pointerStyle, ["shape"]);
- const { shape: pinShape } = pinStyle, resPinStyle = __rest(pinStyle, ["shape"]);
- return (points, value, coordinate, theme) => {
- // Invert points.
- const invertedPoints = points.map((p) => coordinate.invert(p));
- // Get new coordinate.
- const [startAngle, endAngle, innerRadius] = (0, coordinate_1.getTransformOptions)(coordinate, 'polar');
- const newCoordinate = coordinate.clone();
- const { color: stroke } = value;
- const newTransformations = (0, coordinate_2.Radial)({
- startAngle,
- endAngle,
- innerRadius,
- outerRadius: radius,
- });
- newTransformations.push(['cartesian']);
- newCoordinate.update({
- transformations: newTransformations,
- });
- const newPoints = invertedPoints.map((p) => newCoordinate.map(p));
- const [x, y] = (0, utils_1.getOrigin)(newPoints);
- const [cx, cy] = coordinate.getCenter();
- const pointerAttrs = Object.assign(Object.assign({ x1: x, y1: y, x2: cx, y2: cy, stroke }, resPointerStyle), style);
- const pinAttrs = Object.assign(Object.assign({ cx,
- cy,
- stroke }, resPinStyle), style);
- const indicatorGroup = (0, selection_1.select)(new g_1.Group());
- if (!(0, helper_1.isUnset)(pointerShape)) {
- typeof pointerShape === 'function'
- ? indicatorGroup.append(() => pointerShape(newPoints, value, newCoordinate, theme))
- : indicatorGroup.append('line').call(utils_1.applyStyle, pointerAttrs).node();
- }
- if (!(0, helper_1.isUnset)(pinShape)) {
- typeof pinShape === 'function'
- ? indicatorGroup.append(() => pinShape(newPoints, value, newCoordinate, theme))
- : indicatorGroup.append('circle').call(utils_1.applyStyle, pinAttrs).node();
- }
- return indicatorGroup.node();
- };
- };
- const Gauge = (options) => {
- const DEFAULT_OPTIONS = {
- coordinate: {
- type: 'radial',
- innerRadius: 0.9,
- outerRadius: 1,
- startAngle: (-11 / 10) * Math.PI,
- endAngle: (1 / 10) * Math.PI,
- },
- axis: {
- x: false,
- },
- legend: false,
- tooltip: false,
- encode: {
- x: 'x',
- y: 'y',
- color: 'color',
- },
- scale: {
- color: {
- range: ['#30BF78', '#D0D0D0'],
- },
- },
- };
- const DEFAULT_INDICATOR_OPTIONS = {
- style: {
- shape: indicatorShape,
- lineWidth: 4,
- pointerLineCap: 'round',
- pinR: 10,
- pinFill: '#fff',
- radius: 0.6,
- },
- };
- const DEFAULT_TEXT_OPTIONS = {
- type: 'text',
- style: {
- x: '50%',
- y: '60%',
- textAlign: 'center',
- textBaseline: 'middle',
- fontSize: 20,
- fontWeight: 800,
- fill: '#888',
- },
- };
- return () => {
- const { data = {}, scale = {}, style = {}, animate = {}, transform = [] } = options, resOptions = __rest(options, ["data", "scale", "style", "animate", "transform"]);
- const { targetData, totalData, target, total, scale: newScale, } = dataTransform(data, scale);
- const textStyle = (0, helper_1.subObject)(style, 'text');
- // pointer + pin
- const indicatorStyle = (0, helper_1.filterPrefixObject)(style, ['pointer', 'pin']);
- return [
- (0, util_1.deepMix)({}, DEFAULT_OPTIONS, Object.assign({ type: 'interval', transform: [{ type: 'stackY' }], data: totalData, scale: newScale, style: (0, helper_1.subObject)(style, 'arc'), animate: typeof animate === 'object' ? (0, helper_1.subObject)(animate, 'arc') : animate }, resOptions)),
- (0, util_1.deepMix)({}, DEFAULT_OPTIONS, DEFAULT_INDICATOR_OPTIONS, Object.assign({ type: 'point', data: targetData, scale: newScale, style: indicatorStyle, animate: typeof animate === 'object'
- ? (0, helper_1.subObject)(animate, 'indicator')
- : animate }, resOptions)),
- (0, util_1.deepMix)({}, DEFAULT_TEXT_OPTIONS, {
- style: Object.assign({ text: getTextContent(textStyle, { target, total }) }, textStyle),
- animate: typeof animate === 'object' ? (0, helper_1.subObject)(animate, 'text') : animate,
- }),
- ];
- };
- };
- exports.Gauge = Gauge;
- exports.Gauge.props = {};
- //# sourceMappingURL=gauge.js.map
|