index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Sparkline = void 0;
  4. var tslib_1 = require("tslib");
  5. var scale_1 = require("@antv/scale");
  6. var util_1 = require("@antv/util");
  7. var core_1 = require("../../core");
  8. var util_2 = require("../../util");
  9. var columns_1 = require("./columns");
  10. var lines_1 = require("./lines");
  11. var path_1 = require("./path");
  12. var utils_1 = require("./utils");
  13. var Sparkline = /** @class */ (function (_super) {
  14. tslib_1.__extends(Sparkline, _super);
  15. function Sparkline(options) {
  16. return _super.call(this, options, {
  17. type: 'line',
  18. width: 200,
  19. height: 20,
  20. isStack: false,
  21. color: ['#83daad', '#edbf45', '#d2cef9', '#e290b3', '#6f63f4'],
  22. smooth: true,
  23. lineLineWidth: 1,
  24. areaOpacity: 0,
  25. isGroup: false,
  26. columnLineWidth: 1,
  27. columnStroke: '#fff',
  28. }) || this;
  29. }
  30. Object.defineProperty(Sparkline.prototype, "rawData", {
  31. /**
  32. * 将data统一格式化为数组形式
  33. * 如果堆叠,则生成堆叠数据
  34. */
  35. get: function () {
  36. var rawData = this.attributes.data;
  37. if (!rawData || (rawData === null || rawData === void 0 ? void 0 : rawData.length) === 0)
  38. return [[]];
  39. var data = (0, util_1.clone)(rawData);
  40. // number[] -> number[][]
  41. return (0, util_1.isNumber)(data[0]) ? [data] : data;
  42. },
  43. enumerable: false,
  44. configurable: true
  45. });
  46. Object.defineProperty(Sparkline.prototype, "data", {
  47. get: function () {
  48. if (this.attributes.isStack)
  49. return (0, utils_1.getStackedData)(this.rawData);
  50. return this.rawData;
  51. },
  52. enumerable: false,
  53. configurable: true
  54. });
  55. Object.defineProperty(Sparkline.prototype, "scales", {
  56. get: function () {
  57. return this.createScales(this.data);
  58. },
  59. enumerable: false,
  60. configurable: true
  61. });
  62. Object.defineProperty(Sparkline.prototype, "baseline", {
  63. /**
  64. * 基准线,默认为 0
  65. */
  66. get: function () {
  67. var y = this.scales.y;
  68. var _a = tslib_1.__read(y.getOptions().domain || [0, 0], 2), y1 = _a[0], y2 = _a[1];
  69. if (y2 < 0) {
  70. return y.map(y2);
  71. }
  72. return y.map(y1 < 0 ? 0 : y1);
  73. },
  74. enumerable: false,
  75. configurable: true
  76. });
  77. Object.defineProperty(Sparkline.prototype, "containerShape", {
  78. get: function () {
  79. var _a = this.attributes, width = _a.width, height = _a.height;
  80. return { width: width, height: height };
  81. },
  82. enumerable: false,
  83. configurable: true
  84. });
  85. Object.defineProperty(Sparkline.prototype, "linesStyle", {
  86. get: function () {
  87. var _this = this;
  88. var _a = this.attributes, type = _a.type, isStack = _a.isStack, smooth = _a.smooth;
  89. if (type !== 'line')
  90. throw new Error('linesStyle can only be used in line type');
  91. var areaStyle = (0, util_2.subStyleProps)(this.attributes, 'area');
  92. var lineStyle = (0, util_2.subStyleProps)(this.attributes, 'line');
  93. var width = this.containerShape.width;
  94. var data = this.data;
  95. if (data[0].length === 0)
  96. return { lines: [], areas: [] };
  97. var _b = this.scales, x = _b.x, y = _b.y;
  98. // 线条Path
  99. var lines = (0, path_1.dataToLines)(data, { type: 'line', x: x, y: y });
  100. // 生成区域path
  101. var areas = [];
  102. if (areaStyle) {
  103. var baseline = this.baseline;
  104. if (isStack) {
  105. areas = smooth
  106. ? (0, path_1.linesToStackCurveAreaPaths)(lines, width, baseline)
  107. : (0, path_1.linesToStackAreaPaths)(lines, width, baseline);
  108. }
  109. else {
  110. areas = (0, path_1.linesToAreaPaths)(lines, smooth, width, baseline);
  111. }
  112. }
  113. return {
  114. lines: lines.map(function (line, idx) {
  115. return tslib_1.__assign({ stroke: _this.getColor(idx), path: smooth ? (0, path_1.lineToCurvePath)(line) : (0, path_1.lineToLinePath)(line) }, lineStyle);
  116. }),
  117. areas: areas.map(function (path, idx) {
  118. return tslib_1.__assign({ path: path, fill: _this.getColor(idx) }, areaStyle);
  119. }),
  120. };
  121. },
  122. enumerable: false,
  123. configurable: true
  124. });
  125. Object.defineProperty(Sparkline.prototype, "columnsStyle", {
  126. get: function () {
  127. var _this = this;
  128. var columnStyle = (0, util_2.subStyleProps)(this.attributes, 'column');
  129. var _a = this.attributes, isStack = _a.isStack, type = _a.type;
  130. if (type !== 'column')
  131. throw new Error('columnsStyle can only be used in column type');
  132. var height = this.containerShape.height;
  133. var data = this.rawData;
  134. if (!data)
  135. return { columns: [] };
  136. if (isStack)
  137. data = (0, utils_1.getStackedData)(data);
  138. var _b = this.createScales(data), x = _b.x, y = _b.y;
  139. var _c = tslib_1.__read((0, utils_1.getRange)(data), 2), minVal = _c[0], maxVal = _c[1];
  140. var heightScale = new scale_1.Linear({
  141. domain: [0, maxVal - (minVal > 0 ? 0 : minVal)],
  142. range: [0, height],
  143. });
  144. var bandWidth = x.getBandWidth();
  145. var rawData = this.rawData;
  146. return {
  147. columns: data.map(function (column, i) {
  148. return column.map(function (val, j) {
  149. var barWidth = bandWidth / data.length;
  150. return tslib_1.__assign(tslib_1.__assign({ fill: _this.getColor(i) }, columnStyle), (isStack
  151. ? {
  152. x: x.map(j),
  153. y: y.map(val),
  154. width: bandWidth,
  155. height: heightScale.map(rawData[i][j]),
  156. }
  157. : {
  158. x: x.map(j) + barWidth * i,
  159. y: val >= 0 ? y.map(val) : y.map(0),
  160. width: barWidth,
  161. height: heightScale.map(Math.abs(val)),
  162. }));
  163. });
  164. }),
  165. };
  166. },
  167. enumerable: false,
  168. configurable: true
  169. });
  170. Sparkline.prototype.render = function (attributes, container) {
  171. this.container = (0, util_2.maybeAppend)(container, '.container', 'rect').attr('className', 'container').node();
  172. var type = attributes.type;
  173. var className = "spark".concat(type);
  174. var style = type === 'line' ? this.linesStyle : this.columnsStyle;
  175. this.spark = (0, util_2.maybeAppend)(container, ".".concat(className), function () {
  176. if (type === 'line')
  177. return new lines_1.Lines({ className: className, style: style });
  178. return new columns_1.Columns({ className: className, style: style });
  179. })
  180. .styles(style)
  181. .node();
  182. };
  183. /**
  184. * 根据数据索引获取color
  185. */
  186. Sparkline.prototype.getColor = function (index) {
  187. var color = this.attributes.color;
  188. if ((0, util_1.isArray)(color)) {
  189. return color[index % color.length];
  190. }
  191. if ((0, util_1.isFunction)(color)) {
  192. return color.call(null, index);
  193. }
  194. return color;
  195. };
  196. /**
  197. * 根据数据生成scale
  198. */
  199. Sparkline.prototype.createScales = function (data) {
  200. var _a, _b;
  201. var _c = this.attributes, type = _c.type, _d = _c.range, range = _d === void 0 ? [] : _d, isGroup = _c.isGroup, spacing = _c.spacing;
  202. var _e = this.containerShape, width = _e.width, height = _e.height;
  203. var _f = tslib_1.__read((0, utils_1.getRange)(data), 2), minVal = _f[0], maxVal = _f[1];
  204. var yScale = new scale_1.Linear({
  205. domain: [(_a = range[0]) !== null && _a !== void 0 ? _a : minVal, (_b = range[1]) !== null && _b !== void 0 ? _b : maxVal],
  206. range: [height, 0],
  207. });
  208. if (type === 'line') {
  209. return {
  210. type: type,
  211. x: new scale_1.Linear({
  212. domain: [0, data[0].length - 1],
  213. range: [0, width],
  214. }),
  215. y: yScale,
  216. };
  217. }
  218. return {
  219. type: type,
  220. x: new scale_1.Band({
  221. domain: data[0].map(function (val, idx) { return idx; }),
  222. range: [0, width],
  223. paddingInner: isGroup ? spacing : 0,
  224. }),
  225. y: yScale,
  226. };
  227. };
  228. Sparkline.tag = 'sparkline';
  229. return Sparkline;
  230. }(core_1.GUI));
  231. exports.Sparkline = Sparkline;
  232. //# sourceMappingURL=index.js.map