474680126d18c604ce9b07720a3a322e6de275d9eedbd4fd3559c00d51140563753e3f033f071dc483dcf97b847a50fdfd2181c0654169dc8243ebe7e8cbbe 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.bar = bar;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  9. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  10. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  11. var _updater = require("../class/updater.class");
  12. var _config = require("../config");
  13. var _util = require("@jiaminghi/c-render/lib/plugin/util");
  14. var _util2 = require("../util");
  15. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  16. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  17. function bar(chart) {
  18. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  19. var xAxis = option.xAxis,
  20. yAxis = option.yAxis,
  21. series = option.series;
  22. var bars = [];
  23. if (xAxis && yAxis && series) {
  24. bars = (0, _util2.initNeedSeries)(series, _config.barConfig, 'bar');
  25. bars = setBarAxis(bars, chart);
  26. bars = setBarPositionData(bars, chart);
  27. bars = calcBarsPosition(bars, chart);
  28. }
  29. (0, _updater.doUpdate)({
  30. chart: chart,
  31. series: bars.slice(-1),
  32. key: 'backgroundBar',
  33. getGraphConfig: getBackgroundBarConfig
  34. });
  35. bars.reverse();
  36. (0, _updater.doUpdate)({
  37. chart: chart,
  38. series: bars,
  39. key: 'bar',
  40. getGraphConfig: getBarConfig,
  41. getStartGraphConfig: getStartBarConfig,
  42. beforeUpdate: beforeUpdateBar
  43. });
  44. (0, _updater.doUpdate)({
  45. chart: chart,
  46. series: bars,
  47. key: 'barLabel',
  48. getGraphConfig: getLabelConfig
  49. });
  50. }
  51. function setBarAxis(bars, chart) {
  52. var axisData = chart.axisData;
  53. bars.forEach(function (bar) {
  54. var xAxisIndex = bar.xAxisIndex,
  55. yAxisIndex = bar.yAxisIndex;
  56. if (typeof xAxisIndex !== 'number') xAxisIndex = 0;
  57. if (typeof yAxisIndex !== 'number') yAxisIndex = 0;
  58. var xAxis = axisData.find(function (_ref) {
  59. var axis = _ref.axis,
  60. index = _ref.index;
  61. return "".concat(axis).concat(index) === "x".concat(xAxisIndex);
  62. });
  63. var yAxis = axisData.find(function (_ref2) {
  64. var axis = _ref2.axis,
  65. index = _ref2.index;
  66. return "".concat(axis).concat(index) === "y".concat(yAxisIndex);
  67. });
  68. var axis = [xAxis, yAxis];
  69. var valueAxisIndex = axis.findIndex(function (_ref3) {
  70. var data = _ref3.data;
  71. return data === 'value';
  72. });
  73. bar.valueAxis = axis[valueAxisIndex];
  74. bar.labelAxis = axis[1 - valueAxisIndex];
  75. });
  76. return bars;
  77. }
  78. function setBarPositionData(bars, chart) {
  79. var labelBarGroup = groupBarByLabelAxis(bars);
  80. labelBarGroup.forEach(function (group) {
  81. setBarIndex(group);
  82. setBarNum(group);
  83. setBarCategoryWidth(group, chart);
  84. setBarWidthAndGap(group);
  85. setBarAllWidthAndGap(group);
  86. });
  87. return bars;
  88. }
  89. function setBarIndex(bars) {
  90. var stacks = getBarStack(bars);
  91. stacks = stacks.map(function (stack) {
  92. return {
  93. stack: stack,
  94. index: -1
  95. };
  96. });
  97. var currentIndex = 0;
  98. bars.forEach(function (bar) {
  99. var stack = bar.stack;
  100. if (!stack) {
  101. bar.barIndex = currentIndex;
  102. currentIndex++;
  103. } else {
  104. var stackData = stacks.find(function (_ref4) {
  105. var s = _ref4.stack;
  106. return s === stack;
  107. });
  108. if (stackData.index === -1) {
  109. stackData.index = currentIndex;
  110. currentIndex++;
  111. }
  112. bar.barIndex = stackData.index;
  113. }
  114. });
  115. }
  116. function groupBarByLabelAxis(bars) {
  117. var labelAxis = bars.map(function (_ref5) {
  118. var _ref5$labelAxis = _ref5.labelAxis,
  119. axis = _ref5$labelAxis.axis,
  120. index = _ref5$labelAxis.index;
  121. return axis + index;
  122. });
  123. labelAxis = (0, _toConsumableArray2["default"])(new Set(labelAxis));
  124. return labelAxis.map(function (axisIndex) {
  125. return bars.filter(function (_ref6) {
  126. var _ref6$labelAxis = _ref6.labelAxis,
  127. axis = _ref6$labelAxis.axis,
  128. index = _ref6$labelAxis.index;
  129. return axis + index === axisIndex;
  130. });
  131. });
  132. }
  133. function getBarStack(bars) {
  134. var stacks = [];
  135. bars.forEach(function (_ref7) {
  136. var stack = _ref7.stack;
  137. if (stack) stacks.push(stack);
  138. });
  139. return (0, _toConsumableArray2["default"])(new Set(stacks));
  140. }
  141. function setBarNum(bars) {
  142. var barNum = (0, _toConsumableArray2["default"])(new Set(bars.map(function (_ref8) {
  143. var barIndex = _ref8.barIndex;
  144. return barIndex;
  145. }))).length;
  146. bars.forEach(function (bar) {
  147. return bar.barNum = barNum;
  148. });
  149. }
  150. function setBarCategoryWidth(bars) {
  151. var lastBar = bars.slice(-1)[0];
  152. var barCategoryGap = lastBar.barCategoryGap,
  153. tickGap = lastBar.labelAxis.tickGap;
  154. var barCategoryWidth = 0;
  155. if (typeof barCategoryGap === 'number') {
  156. barCategoryWidth = barCategoryGap;
  157. } else {
  158. barCategoryWidth = (1 - parseInt(barCategoryGap) / 100) * tickGap;
  159. }
  160. bars.forEach(function (bar) {
  161. return bar.barCategoryWidth = barCategoryWidth;
  162. });
  163. }
  164. function setBarWidthAndGap(bars) {
  165. var _bars$slice$ = bars.slice(-1)[0],
  166. barCategoryWidth = _bars$slice$.barCategoryWidth,
  167. barWidth = _bars$slice$.barWidth,
  168. barGap = _bars$slice$.barGap,
  169. barNum = _bars$slice$.barNum;
  170. var widthAndGap = [];
  171. if (typeof barWidth === 'number' || barWidth !== 'auto') {
  172. widthAndGap = getBarWidthAndGapWithPercentOrNumber(barCategoryWidth, barWidth, barGap, barNum);
  173. } else if (barWidth === 'auto') {
  174. widthAndGap = getBarWidthAndGapWidthAuto(barCategoryWidth, barWidth, barGap, barNum);
  175. }
  176. var _widthAndGap = widthAndGap,
  177. _widthAndGap2 = (0, _slicedToArray2["default"])(_widthAndGap, 2),
  178. width = _widthAndGap2[0],
  179. gap = _widthAndGap2[1];
  180. bars.forEach(function (bar) {
  181. bar.barWidth = width;
  182. bar.barGap = gap;
  183. });
  184. }
  185. function getBarWidthAndGapWithPercentOrNumber(barCategoryWidth, barWidth, barGap) {
  186. var width = 0,
  187. gap = 0;
  188. if (typeof barWidth === 'number') {
  189. width = barWidth;
  190. } else {
  191. width = parseInt(barWidth) / 100 * barCategoryWidth;
  192. }
  193. if (typeof barGap === 'number') {
  194. gap = barGap;
  195. } else {
  196. gap = parseInt(barGap) / 100 * width;
  197. }
  198. return [width, gap];
  199. }
  200. function getBarWidthAndGapWidthAuto(barCategoryWidth, barWidth, barGap, barNum) {
  201. var width = 0,
  202. gap = 0;
  203. var barItemWidth = barCategoryWidth / barNum;
  204. if (typeof barGap === 'number') {
  205. gap = barGap;
  206. width = barItemWidth - gap;
  207. } else {
  208. var percent = 10 + parseInt(barGap) / 10;
  209. if (percent === 0) {
  210. width = barItemWidth * 2;
  211. gap = -width;
  212. } else {
  213. width = barItemWidth / percent * 10;
  214. gap = barItemWidth - width;
  215. }
  216. }
  217. return [width, gap];
  218. }
  219. function setBarAllWidthAndGap(bars) {
  220. var _bars$slice$2 = bars.slice(-1)[0],
  221. barGap = _bars$slice$2.barGap,
  222. barWidth = _bars$slice$2.barWidth,
  223. barNum = _bars$slice$2.barNum;
  224. var barAllWidthAndGap = (barGap + barWidth) * barNum - barGap;
  225. bars.forEach(function (bar) {
  226. return bar.barAllWidthAndGap = barAllWidthAndGap;
  227. });
  228. }
  229. function calcBarsPosition(bars, chart) {
  230. bars = calcBarValueAxisCoordinate(bars);
  231. bars = calcBarLabelAxisCoordinate(bars);
  232. bars = eliminateNullBarLabelAxis(bars);
  233. bars = keepSameNumBetweenBarAndData(bars);
  234. return bars;
  235. }
  236. function calcBarLabelAxisCoordinate(bars) {
  237. return bars.map(function (bar) {
  238. var labelAxis = bar.labelAxis,
  239. barAllWidthAndGap = bar.barAllWidthAndGap,
  240. barGap = bar.barGap,
  241. barWidth = bar.barWidth,
  242. barIndex = bar.barIndex;
  243. var tickGap = labelAxis.tickGap,
  244. tickPosition = labelAxis.tickPosition,
  245. axis = labelAxis.axis;
  246. var coordinateIndex = axis === 'x' ? 0 : 1;
  247. var barLabelAxisPos = tickPosition.map(function (tick, i) {
  248. var barCategoryStartPos = tickPosition[i][coordinateIndex] - tickGap / 2;
  249. var barItemsStartPos = barCategoryStartPos + (tickGap - barAllWidthAndGap) / 2;
  250. return barItemsStartPos + (barIndex + 0.5) * barWidth + barIndex * barGap;
  251. });
  252. return _objectSpread(_objectSpread({}, bar), {}, {
  253. barLabelAxisPos: barLabelAxisPos
  254. });
  255. });
  256. }
  257. function calcBarValueAxisCoordinate(bars) {
  258. return bars.map(function (bar) {
  259. var data = (0, _util2.mergeSameStackData)(bar, bars);
  260. data = eliminateNonNumberData(bar, data);
  261. var _bar$valueAxis = bar.valueAxis,
  262. axis = _bar$valueAxis.axis,
  263. minValue = _bar$valueAxis.minValue,
  264. maxValue = _bar$valueAxis.maxValue,
  265. linePosition = _bar$valueAxis.linePosition;
  266. var startPos = getValuePos(minValue, maxValue, minValue < 0 ? 0 : minValue, linePosition, axis);
  267. var endPos = data.map(function (v) {
  268. return getValuePos(minValue, maxValue, v, linePosition, axis);
  269. });
  270. var barValueAxisPos = endPos.map(function (p) {
  271. return [startPos, p];
  272. });
  273. return _objectSpread(_objectSpread({}, bar), {}, {
  274. barValueAxisPos: barValueAxisPos
  275. });
  276. });
  277. }
  278. function eliminateNonNumberData(barItem, barData) {
  279. var data = barItem.data;
  280. return barData.map(function (v, i) {
  281. return typeof data[i] === 'number' ? v : null;
  282. }).filter(function (d) {
  283. return d !== null;
  284. });
  285. }
  286. function eliminateNullBarLabelAxis(bars) {
  287. return bars.map(function (bar) {
  288. var barLabelAxisPos = bar.barLabelAxisPos,
  289. data = bar.data;
  290. data.forEach(function (d, i) {
  291. if (typeof d === 'number') return;
  292. barLabelAxisPos[i] = null;
  293. });
  294. return _objectSpread(_objectSpread({}, bar), {}, {
  295. barLabelAxisPos: barLabelAxisPos.filter(function (p) {
  296. return p !== null;
  297. })
  298. });
  299. });
  300. }
  301. function keepSameNumBetweenBarAndData(bars) {
  302. bars.forEach(function (bar) {
  303. var data = bar.data,
  304. barLabelAxisPos = bar.barLabelAxisPos,
  305. barValueAxisPos = bar.barValueAxisPos;
  306. var dataNum = data.filter(function (d) {
  307. return typeof d === 'number';
  308. }).length;
  309. var axisPosNum = barLabelAxisPos.length;
  310. if (axisPosNum > dataNum) {
  311. barLabelAxisPos.splice(dataNum);
  312. barValueAxisPos.splice(dataNum);
  313. }
  314. });
  315. return bars;
  316. }
  317. function getValuePos(min, max, value, linePosition, axis) {
  318. if (typeof value !== 'number') return null;
  319. var valueMinus = max - min;
  320. var coordinateIndex = axis === 'x' ? 0 : 1;
  321. var posMinus = linePosition[1][coordinateIndex] - linePosition[0][coordinateIndex];
  322. var percent = (value - min) / valueMinus;
  323. if (valueMinus === 0) percent = 0;
  324. var pos = percent * posMinus;
  325. return pos + linePosition[0][coordinateIndex];
  326. }
  327. function getBackgroundBarConfig(barItem) {
  328. var animationCurve = barItem.animationCurve,
  329. animationFrame = barItem.animationFrame,
  330. rLevel = barItem.rLevel;
  331. var shapes = getBackgroundBarShapes(barItem);
  332. var style = getBackgroundBarStyle(barItem);
  333. return shapes.map(function (shape) {
  334. return {
  335. name: 'rect',
  336. index: rLevel,
  337. visible: barItem.backgroundBar.show,
  338. animationCurve: animationCurve,
  339. animationFrame: animationFrame,
  340. shape: shape,
  341. style: style
  342. };
  343. });
  344. }
  345. function getBackgroundBarShapes(barItem) {
  346. var labelAxis = barItem.labelAxis,
  347. valueAxis = barItem.valueAxis;
  348. var tickPosition = labelAxis.tickPosition;
  349. var axis = valueAxis.axis,
  350. linePosition = valueAxis.linePosition;
  351. var width = getBackgroundBarWidth(barItem);
  352. var haltWidth = width / 2;
  353. var posIndex = axis === 'x' ? 0 : 1;
  354. var centerPos = tickPosition.map(function (p) {
  355. return p[1 - posIndex];
  356. });
  357. var _ref9 = [linePosition[0][posIndex], linePosition[1][posIndex]],
  358. start = _ref9[0],
  359. end = _ref9[1];
  360. return centerPos.map(function (center) {
  361. if (axis === 'x') {
  362. return {
  363. x: start,
  364. y: center - haltWidth,
  365. w: end - start,
  366. h: width
  367. };
  368. } else {
  369. return {
  370. x: center - haltWidth,
  371. y: end,
  372. w: width,
  373. h: start - end
  374. };
  375. }
  376. });
  377. }
  378. function getBackgroundBarWidth(barItem) {
  379. var barAllWidthAndGap = barItem.barAllWidthAndGap,
  380. barCategoryWidth = barItem.barCategoryWidth,
  381. backgroundBar = barItem.backgroundBar;
  382. var width = backgroundBar.width;
  383. if (typeof width === 'number') return width;
  384. if (width === 'auto') return barAllWidthAndGap;
  385. return parseInt(width) / 100 * barCategoryWidth;
  386. }
  387. function getBackgroundBarStyle(barItem) {
  388. return barItem.backgroundBar.style;
  389. }
  390. function getBarConfig(barItem) {
  391. var barLabelAxisPos = barItem.barLabelAxisPos,
  392. animationCurve = barItem.animationCurve,
  393. animationFrame = barItem.animationFrame,
  394. rLevel = barItem.rLevel;
  395. var name = getBarName(barItem);
  396. return barLabelAxisPos.map(function (foo, i) {
  397. return {
  398. name: name,
  399. index: rLevel,
  400. animationCurve: animationCurve,
  401. animationFrame: animationFrame,
  402. shape: getBarShape(barItem, i),
  403. style: getBarStyle(barItem, i)
  404. };
  405. });
  406. }
  407. function getBarName(barItem) {
  408. var shapeType = barItem.shapeType;
  409. if (shapeType === 'leftEchelon' || shapeType === 'rightEchelon') return 'polyline';
  410. return 'rect';
  411. }
  412. function getBarShape(barItem, i) {
  413. var shapeType = barItem.shapeType;
  414. if (shapeType === 'leftEchelon') {
  415. return getLeftEchelonShape(barItem, i);
  416. } else if (shapeType === 'rightEchelon') {
  417. return getRightEchelonShape(barItem, i);
  418. } else {
  419. return getNormalBarShape(barItem, i);
  420. }
  421. }
  422. function getLeftEchelonShape(barItem, i) {
  423. var barValueAxisPos = barItem.barValueAxisPos,
  424. barLabelAxisPos = barItem.barLabelAxisPos,
  425. barWidth = barItem.barWidth,
  426. echelonOffset = barItem.echelonOffset;
  427. var _barValueAxisPos$i = (0, _slicedToArray2["default"])(barValueAxisPos[i], 2),
  428. start = _barValueAxisPos$i[0],
  429. end = _barValueAxisPos$i[1];
  430. var labelAxisPos = barLabelAxisPos[i];
  431. var halfWidth = barWidth / 2;
  432. var valueAxis = barItem.valueAxis.axis;
  433. var points = [];
  434. if (valueAxis === 'x') {
  435. points[0] = [end, labelAxisPos - halfWidth];
  436. points[1] = [end, labelAxisPos + halfWidth];
  437. points[2] = [start, labelAxisPos + halfWidth];
  438. points[3] = [start + echelonOffset, labelAxisPos - halfWidth];
  439. if (end - start < echelonOffset) points.splice(3, 1);
  440. } else {
  441. points[0] = [labelAxisPos - halfWidth, end];
  442. points[1] = [labelAxisPos + halfWidth, end];
  443. points[2] = [labelAxisPos + halfWidth, start];
  444. points[3] = [labelAxisPos - halfWidth, start - echelonOffset];
  445. if (start - end < echelonOffset) points.splice(3, 1);
  446. }
  447. return {
  448. points: points,
  449. close: true
  450. };
  451. }
  452. function getRightEchelonShape(barItem, i) {
  453. var barValueAxisPos = barItem.barValueAxisPos,
  454. barLabelAxisPos = barItem.barLabelAxisPos,
  455. barWidth = barItem.barWidth,
  456. echelonOffset = barItem.echelonOffset;
  457. var _barValueAxisPos$i2 = (0, _slicedToArray2["default"])(barValueAxisPos[i], 2),
  458. start = _barValueAxisPos$i2[0],
  459. end = _barValueAxisPos$i2[1];
  460. var labelAxisPos = barLabelAxisPos[i];
  461. var halfWidth = barWidth / 2;
  462. var valueAxis = barItem.valueAxis.axis;
  463. var points = [];
  464. if (valueAxis === 'x') {
  465. points[0] = [end, labelAxisPos + halfWidth];
  466. points[1] = [end, labelAxisPos - halfWidth];
  467. points[2] = [start, labelAxisPos - halfWidth];
  468. points[3] = [start + echelonOffset, labelAxisPos + halfWidth];
  469. if (end - start < echelonOffset) points.splice(2, 1);
  470. } else {
  471. points[0] = [labelAxisPos + halfWidth, end];
  472. points[1] = [labelAxisPos - halfWidth, end];
  473. points[2] = [labelAxisPos - halfWidth, start];
  474. points[3] = [labelAxisPos + halfWidth, start - echelonOffset];
  475. if (start - end < echelonOffset) points.splice(2, 1);
  476. }
  477. return {
  478. points: points,
  479. close: true
  480. };
  481. }
  482. function getNormalBarShape(barItem, i) {
  483. var barValueAxisPos = barItem.barValueAxisPos,
  484. barLabelAxisPos = barItem.barLabelAxisPos,
  485. barWidth = barItem.barWidth;
  486. var _barValueAxisPos$i3 = (0, _slicedToArray2["default"])(barValueAxisPos[i], 2),
  487. start = _barValueAxisPos$i3[0],
  488. end = _barValueAxisPos$i3[1];
  489. var labelAxisPos = barLabelAxisPos[i];
  490. var valueAxis = barItem.valueAxis.axis;
  491. var shape = {};
  492. if (valueAxis === 'x') {
  493. shape.x = start;
  494. shape.y = labelAxisPos - barWidth / 2;
  495. shape.w = end - start;
  496. shape.h = barWidth;
  497. } else {
  498. shape.x = labelAxisPos - barWidth / 2;
  499. shape.y = end;
  500. shape.w = barWidth;
  501. shape.h = start - end;
  502. }
  503. return shape;
  504. }
  505. function getBarStyle(barItem, i) {
  506. var barStyle = barItem.barStyle,
  507. gradient = barItem.gradient,
  508. color = barItem.color,
  509. independentColor = barItem.independentColor,
  510. independentColors = barItem.independentColors;
  511. var fillColor = [barStyle.fill || color];
  512. var gradientColor = (0, _util2.deepMerge)(fillColor, gradient.color);
  513. if (independentColor) {
  514. var idtColor = independentColors[i % independentColors.length];
  515. gradientColor = idtColor instanceof Array ? idtColor : [idtColor];
  516. }
  517. if (gradientColor.length === 1) gradientColor.push(gradientColor[0]);
  518. var gradientParams = getGradientParams(barItem, i);
  519. return (0, _util2.deepMerge)({
  520. gradientColor: gradientColor,
  521. gradientParams: gradientParams,
  522. gradientType: 'linear',
  523. gradientWith: 'fill'
  524. }, barStyle);
  525. }
  526. function getGradientParams(barItem, i) {
  527. var barValueAxisPos = barItem.barValueAxisPos,
  528. barLabelAxisPos = barItem.barLabelAxisPos,
  529. data = barItem.data;
  530. var _barItem$valueAxis = barItem.valueAxis,
  531. linePosition = _barItem$valueAxis.linePosition,
  532. axis = _barItem$valueAxis.axis;
  533. var _barValueAxisPos$i4 = (0, _slicedToArray2["default"])(barValueAxisPos[i], 2),
  534. start = _barValueAxisPos$i4[0],
  535. end = _barValueAxisPos$i4[1];
  536. var labelAxisPos = barLabelAxisPos[i];
  537. var value = data[i];
  538. var _linePosition = (0, _slicedToArray2["default"])(linePosition, 2),
  539. lineStart = _linePosition[0],
  540. lineEnd = _linePosition[1];
  541. var valueAxisIndex = axis === 'x' ? 0 : 1;
  542. var endPos = end;
  543. if (!barItem.gradient.local) {
  544. endPos = value < 0 ? lineStart[valueAxisIndex] : lineEnd[valueAxisIndex];
  545. }
  546. if (axis === 'y') {
  547. return [labelAxisPos, endPos, labelAxisPos, start];
  548. } else {
  549. return [endPos, labelAxisPos, start, labelAxisPos];
  550. }
  551. }
  552. function getStartBarConfig(barItem) {
  553. var configs = getBarConfig(barItem);
  554. var shapeType = barItem.shapeType;
  555. configs.forEach(function (config) {
  556. var shape = config.shape;
  557. if (shapeType === 'leftEchelon') {
  558. shape = getStartLeftEchelonShape(shape, barItem);
  559. } else if (shapeType === 'rightEchelon') {
  560. shape = getStartRightEchelonShape(shape, barItem);
  561. } else {
  562. shape = getStartNormalBarShape(shape, barItem);
  563. }
  564. config.shape = shape;
  565. });
  566. return configs;
  567. }
  568. function getStartLeftEchelonShape(shape, barItem) {
  569. var axis = barItem.valueAxis.axis;
  570. shape = (0, _util.deepClone)(shape);
  571. var _shape = shape,
  572. points = _shape.points;
  573. var index = axis === 'x' ? 0 : 1;
  574. var start = points[2][index];
  575. points.forEach(function (point) {
  576. return point[index] = start;
  577. });
  578. return shape;
  579. }
  580. function getStartRightEchelonShape(shape, barItem) {
  581. var axis = barItem.valueAxis.axis;
  582. shape = (0, _util.deepClone)(shape);
  583. var _shape2 = shape,
  584. points = _shape2.points;
  585. var index = axis === 'x' ? 0 : 1;
  586. var start = points[2][index];
  587. points.forEach(function (point) {
  588. return point[index] = start;
  589. });
  590. return shape;
  591. }
  592. function getStartNormalBarShape(shape, barItem) {
  593. var axis = barItem.valueAxis.axis;
  594. var x = shape.x,
  595. y = shape.y,
  596. w = shape.w,
  597. h = shape.h;
  598. if (axis === 'x') {
  599. w = 0;
  600. } else {
  601. y = y + h;
  602. h = 0;
  603. }
  604. return {
  605. x: x,
  606. y: y,
  607. w: w,
  608. h: h
  609. };
  610. }
  611. function beforeUpdateBar(graphs, barItem, i, updater) {
  612. var render = updater.chart.render;
  613. var name = getBarName(barItem);
  614. if (graphs[i] && graphs[i][0].name !== name) {
  615. graphs[i].forEach(function (g) {
  616. return render.delGraph(g);
  617. });
  618. graphs[i] = null;
  619. }
  620. }
  621. function getLabelConfig(barItem) {
  622. var animationCurve = barItem.animationCurve,
  623. animationFrame = barItem.animationFrame,
  624. rLevel = barItem.rLevel;
  625. var shapes = getLabelShapes(barItem);
  626. var style = getLabelStyle(barItem);
  627. return shapes.map(function (shape) {
  628. return {
  629. name: 'text',
  630. index: rLevel,
  631. visible: barItem.label.show,
  632. animationCurve: animationCurve,
  633. animationFrame: animationFrame,
  634. shape: shape,
  635. style: style
  636. };
  637. });
  638. }
  639. function getLabelShapes(barItem) {
  640. var contents = getFormatterLabels(barItem);
  641. var position = getLabelsPosition(barItem);
  642. return position.map(function (pos, i) {
  643. return {
  644. position: pos,
  645. content: contents[i]
  646. };
  647. });
  648. }
  649. function getFormatterLabels(barItem) {
  650. var data = barItem.data,
  651. label = barItem.label;
  652. var formatter = label.formatter;
  653. data = data.filter(function (d) {
  654. return typeof d === 'number';
  655. }).map(function (d) {
  656. return d.toString();
  657. });
  658. if (!formatter) return data;
  659. var type = (0, _typeof2["default"])(formatter);
  660. if (type === 'string') return data.map(function (d) {
  661. return formatter.replace('{value}', d);
  662. });
  663. if (type === 'function') return data.map(function (d, i) {
  664. return formatter({
  665. value: d,
  666. index: i
  667. });
  668. });
  669. return data;
  670. }
  671. function getLabelsPosition(barItem) {
  672. var label = barItem.label,
  673. barValueAxisPos = barItem.barValueAxisPos,
  674. barLabelAxisPos = barItem.barLabelAxisPos;
  675. var position = label.position,
  676. offset = label.offset;
  677. var axis = barItem.valueAxis.axis;
  678. return barValueAxisPos.map(function (_ref10, i) {
  679. var _ref11 = (0, _slicedToArray2["default"])(_ref10, 2),
  680. start = _ref11[0],
  681. end = _ref11[1];
  682. var labelAxisPos = barLabelAxisPos[i];
  683. var pos = [end, labelAxisPos];
  684. if (position === 'bottom') {
  685. pos = [start, labelAxisPos];
  686. }
  687. if (position === 'center') {
  688. pos = [(start + end) / 2, labelAxisPos];
  689. }
  690. if (axis === 'y') pos.reverse();
  691. return getOffsetedPoint(pos, offset);
  692. });
  693. }
  694. function getOffsetedPoint(_ref12, _ref13) {
  695. var _ref14 = (0, _slicedToArray2["default"])(_ref12, 2),
  696. x = _ref14[0],
  697. y = _ref14[1];
  698. var _ref15 = (0, _slicedToArray2["default"])(_ref13, 2),
  699. ox = _ref15[0],
  700. oy = _ref15[1];
  701. return [x + ox, y + oy];
  702. }
  703. function getLabelStyle(barItem) {
  704. var color = barItem.color,
  705. style = barItem.label.style,
  706. gc = barItem.gradient.color;
  707. if (gc.length) color = gc[0];
  708. style = (0, _util2.deepMerge)({
  709. fill: color
  710. }, style);
  711. return style;
  712. }