UNPKG

@carbon/charts

Version:
95 lines 4.03 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); // Internal Imports import { Pie } from './pie'; import { DOMUtils } from '../../services'; import { Tools } from '../../tools'; // D3 Imports import { select } from 'd3-selection'; import { interpolateNumber, interpolateRound } from 'd3-interpolate'; var Donut = /** @class */ (function (_super) { __extends(Donut, _super); function Donut() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = 'donut'; return _this; } Donut.prototype.render = function (animate) { if (animate === void 0) { animate = true; } // Call render() from Pie _super.prototype.render.call(this, animate); var self = this; // if there are no data, remove the center content // that is the old one and do nothing if (this.model.isDataEmpty()) { this.getContainerSVG().select('g.center').remove(); return; } var svg = DOMUtils.appendOrSelect(this.getContainerSVG(), 'g.center'); var options = this.getOptions(); // Compute the outer radius needed var radius = this.computeRadius(); // Add the number shown in the center of the donut DOMUtils.appendOrSelect(svg, 'text.donut-figure') .attr('text-anchor', 'middle') .style('font-size', function () { return options.donut.center.numberFontSize(radius); }) .transition(this.services.transitions.getTransition('donut-figure-enter-update', animate)) .tween('text', function () { return self.centerNumberTween(select(this)); }); // Add the label below the number in the center of the donut DOMUtils.appendOrSelect(svg, 'text.donut-title') .attr('text-anchor', 'middle') .style('font-size', function () { return options.donut.center.titleFontSize(radius); }) .attr('y', options.donut.center.titleYPosition(radius)) .text(Tools.getProperty(options, 'donut', 'center', 'label')); }; Donut.prototype.getInnerRadius = function () { // Compute the outer radius needed var radius = this.computeRadius(); return radius * (3 / 4); }; Donut.prototype.centerNumberTween = function (d3Ref) { var options = this.getOptions(); var donutCenterFigure = Tools.getProperty(options, 'donut', 'center', 'number'); if (donutCenterFigure === null) { donutCenterFigure = this.model .getDisplayData() .reduce(function (accumulator, d) { return accumulator + d.value; }, 0); } // Remove commas from the current value string, and convert to an int var currentValue = parseInt(d3Ref.text().replace(/[, ]+/g, ''), 10) || 0; var interpolateFunction; if (currentValue % 1 === 0 && donutCenterFigure % 1 === 0) { interpolateFunction = interpolateRound; } else { interpolateFunction = interpolateNumber; } var i = interpolateFunction(currentValue, donutCenterFigure); return function (t) { var numberFormatter = options.donut.center.numberFormatter; d3Ref.text(numberFormatter(i(t))); }; }; return Donut; }(Pie)); export { Donut }; //# sourceMappingURL=../../../src/components/graphs/donut.js.map