barGrid.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { each, defaults, keys } from 'zrender/lib/core/util.js';
  41. import { parsePercent } from '../util/number.js';
  42. import { isDimensionStacked } from '../data/helper/dataStackHelper.js';
  43. import createRenderPlanner from '../chart/helper/createRenderPlanner.js';
  44. import { createFloat32Array } from '../util/vendor.js';
  45. var STACK_PREFIX = '__ec_stack_';
  46. function getSeriesStackId(seriesModel) {
  47. return seriesModel.get('stack') || STACK_PREFIX + seriesModel.seriesIndex;
  48. }
  49. function getAxisKey(axis) {
  50. return axis.dim + axis.index;
  51. }
  52. /**
  53. * @return {Object} {width, offset, offsetCenter} If axis.type is not 'category', return undefined.
  54. */
  55. export function getLayoutOnAxis(opt) {
  56. var params = [];
  57. var baseAxis = opt.axis;
  58. var axisKey = 'axis0';
  59. if (baseAxis.type !== 'category') {
  60. return;
  61. }
  62. var bandWidth = baseAxis.getBandWidth();
  63. for (var i = 0; i < opt.count || 0; i++) {
  64. params.push(defaults({
  65. bandWidth: bandWidth,
  66. axisKey: axisKey,
  67. stackId: STACK_PREFIX + i
  68. }, opt));
  69. }
  70. var widthAndOffsets = doCalBarWidthAndOffset(params);
  71. var result = [];
  72. for (var i = 0; i < opt.count; i++) {
  73. var item = widthAndOffsets[axisKey][STACK_PREFIX + i];
  74. item.offsetCenter = item.offset + item.width / 2;
  75. result.push(item);
  76. }
  77. return result;
  78. }
  79. export function prepareLayoutBarSeries(seriesType, ecModel) {
  80. var seriesModels = [];
  81. ecModel.eachSeriesByType(seriesType, function (seriesModel) {
  82. // Check series coordinate, do layout for cartesian2d only
  83. if (isOnCartesian(seriesModel)) {
  84. seriesModels.push(seriesModel);
  85. }
  86. });
  87. return seriesModels;
  88. }
  89. /**
  90. * Map from (baseAxis.dim + '_' + baseAxis.index) to min gap of two adjacent
  91. * values.
  92. * This works for time axes, value axes, and log axes.
  93. * For a single time axis, return value is in the form like
  94. * {'x_0': [1000000]}.
  95. * The value of 1000000 is in milliseconds.
  96. */
  97. function getValueAxesMinGaps(barSeries) {
  98. /**
  99. * Map from axis.index to values.
  100. * For a single time axis, axisValues is in the form like
  101. * {'x_0': [1495555200000, 1495641600000, 1495728000000]}.
  102. * Items in axisValues[x], e.g. 1495555200000, are time values of all
  103. * series.
  104. */
  105. var axisValues = {};
  106. each(barSeries, function (seriesModel) {
  107. var cartesian = seriesModel.coordinateSystem;
  108. var baseAxis = cartesian.getBaseAxis();
  109. if (baseAxis.type !== 'time' && baseAxis.type !== 'value') {
  110. return;
  111. }
  112. var data = seriesModel.getData();
  113. var key = baseAxis.dim + '_' + baseAxis.index;
  114. var dimIdx = data.getDimensionIndex(data.mapDimension(baseAxis.dim));
  115. var store = data.getStore();
  116. for (var i = 0, cnt = store.count(); i < cnt; ++i) {
  117. var value = store.get(dimIdx, i);
  118. if (!axisValues[key]) {
  119. // No previous data for the axis
  120. axisValues[key] = [value];
  121. } else {
  122. // No value in previous series
  123. axisValues[key].push(value);
  124. }
  125. // Ignore duplicated time values in the same axis
  126. }
  127. });
  128. var axisMinGaps = {};
  129. for (var key in axisValues) {
  130. if (axisValues.hasOwnProperty(key)) {
  131. var valuesInAxis = axisValues[key];
  132. if (valuesInAxis) {
  133. // Sort axis values into ascending order to calculate gaps
  134. valuesInAxis.sort(function (a, b) {
  135. return a - b;
  136. });
  137. var min = null;
  138. for (var j = 1; j < valuesInAxis.length; ++j) {
  139. var delta = valuesInAxis[j] - valuesInAxis[j - 1];
  140. if (delta > 0) {
  141. // Ignore 0 delta because they are of the same axis value
  142. min = min === null ? delta : Math.min(min, delta);
  143. }
  144. }
  145. // Set to null if only have one data
  146. axisMinGaps[key] = min;
  147. }
  148. }
  149. }
  150. return axisMinGaps;
  151. }
  152. export function makeColumnLayout(barSeries) {
  153. var axisMinGaps = getValueAxesMinGaps(barSeries);
  154. var seriesInfoList = [];
  155. each(barSeries, function (seriesModel) {
  156. var cartesian = seriesModel.coordinateSystem;
  157. var baseAxis = cartesian.getBaseAxis();
  158. var axisExtent = baseAxis.getExtent();
  159. var bandWidth;
  160. if (baseAxis.type === 'category') {
  161. bandWidth = baseAxis.getBandWidth();
  162. } else if (baseAxis.type === 'value' || baseAxis.type === 'time') {
  163. var key = baseAxis.dim + '_' + baseAxis.index;
  164. var minGap = axisMinGaps[key];
  165. var extentSpan = Math.abs(axisExtent[1] - axisExtent[0]);
  166. var scale = baseAxis.scale.getExtent();
  167. var scaleSpan = Math.abs(scale[1] - scale[0]);
  168. bandWidth = minGap ? extentSpan / scaleSpan * minGap : extentSpan; // When there is only one data value
  169. } else {
  170. var data = seriesModel.getData();
  171. bandWidth = Math.abs(axisExtent[1] - axisExtent[0]) / data.count();
  172. }
  173. var barWidth = parsePercent(seriesModel.get('barWidth'), bandWidth);
  174. var barMaxWidth = parsePercent(seriesModel.get('barMaxWidth'), bandWidth);
  175. var barMinWidth = parsePercent(
  176. // barMinWidth by default is 0.5 / 1 in cartesian. Because in value axis,
  177. // the auto-calculated bar width might be less than 0.5 / 1.
  178. seriesModel.get('barMinWidth') || (isInLargeMode(seriesModel) ? 0.5 : 1), bandWidth);
  179. var barGap = seriesModel.get('barGap');
  180. var barCategoryGap = seriesModel.get('barCategoryGap');
  181. seriesInfoList.push({
  182. bandWidth: bandWidth,
  183. barWidth: barWidth,
  184. barMaxWidth: barMaxWidth,
  185. barMinWidth: barMinWidth,
  186. barGap: barGap,
  187. barCategoryGap: barCategoryGap,
  188. axisKey: getAxisKey(baseAxis),
  189. stackId: getSeriesStackId(seriesModel)
  190. });
  191. });
  192. return doCalBarWidthAndOffset(seriesInfoList);
  193. }
  194. function doCalBarWidthAndOffset(seriesInfoList) {
  195. // Columns info on each category axis. Key is cartesian name
  196. var columnsMap = {};
  197. each(seriesInfoList, function (seriesInfo, idx) {
  198. var axisKey = seriesInfo.axisKey;
  199. var bandWidth = seriesInfo.bandWidth;
  200. var columnsOnAxis = columnsMap[axisKey] || {
  201. bandWidth: bandWidth,
  202. remainedWidth: bandWidth,
  203. autoWidthCount: 0,
  204. categoryGap: null,
  205. gap: '20%',
  206. stacks: {}
  207. };
  208. var stacks = columnsOnAxis.stacks;
  209. columnsMap[axisKey] = columnsOnAxis;
  210. var stackId = seriesInfo.stackId;
  211. if (!stacks[stackId]) {
  212. columnsOnAxis.autoWidthCount++;
  213. }
  214. stacks[stackId] = stacks[stackId] || {
  215. width: 0,
  216. maxWidth: 0
  217. };
  218. // Caution: In a single coordinate system, these barGrid attributes
  219. // will be shared by series. Consider that they have default values,
  220. // only the attributes set on the last series will work.
  221. // Do not change this fact unless there will be a break change.
  222. var barWidth = seriesInfo.barWidth;
  223. if (barWidth && !stacks[stackId].width) {
  224. // See #6312, do not restrict width.
  225. stacks[stackId].width = barWidth;
  226. barWidth = Math.min(columnsOnAxis.remainedWidth, barWidth);
  227. columnsOnAxis.remainedWidth -= barWidth;
  228. }
  229. var barMaxWidth = seriesInfo.barMaxWidth;
  230. barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
  231. var barMinWidth = seriesInfo.barMinWidth;
  232. barMinWidth && (stacks[stackId].minWidth = barMinWidth);
  233. var barGap = seriesInfo.barGap;
  234. barGap != null && (columnsOnAxis.gap = barGap);
  235. var barCategoryGap = seriesInfo.barCategoryGap;
  236. barCategoryGap != null && (columnsOnAxis.categoryGap = barCategoryGap);
  237. });
  238. var result = {};
  239. each(columnsMap, function (columnsOnAxis, coordSysName) {
  240. result[coordSysName] = {};
  241. var stacks = columnsOnAxis.stacks;
  242. var bandWidth = columnsOnAxis.bandWidth;
  243. var categoryGapPercent = columnsOnAxis.categoryGap;
  244. if (categoryGapPercent == null) {
  245. var columnCount = keys(stacks).length;
  246. // More columns in one group
  247. // the spaces between group is smaller. Or the column will be too thin.
  248. categoryGapPercent = Math.max(35 - columnCount * 4, 15) + '%';
  249. }
  250. var categoryGap = parsePercent(categoryGapPercent, bandWidth);
  251. var barGapPercent = parsePercent(columnsOnAxis.gap, 1);
  252. var remainedWidth = columnsOnAxis.remainedWidth;
  253. var autoWidthCount = columnsOnAxis.autoWidthCount;
  254. var autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  255. autoWidth = Math.max(autoWidth, 0);
  256. // Find if any auto calculated bar exceeded maxBarWidth
  257. each(stacks, function (column) {
  258. var maxWidth = column.maxWidth;
  259. var minWidth = column.minWidth;
  260. if (!column.width) {
  261. var finalWidth = autoWidth;
  262. if (maxWidth && maxWidth < finalWidth) {
  263. finalWidth = Math.min(maxWidth, remainedWidth);
  264. }
  265. // `minWidth` has higher priority. `minWidth` decide that whether the
  266. // bar is able to be visible. So `minWidth` should not be restricted
  267. // by `maxWidth` or `remainedWidth` (which is from `bandWidth`). In
  268. // the extreme cases for `value` axis, bars are allowed to overlap
  269. // with each other if `minWidth` specified.
  270. if (minWidth && minWidth > finalWidth) {
  271. finalWidth = minWidth;
  272. }
  273. if (finalWidth !== autoWidth) {
  274. column.width = finalWidth;
  275. remainedWidth -= finalWidth + barGapPercent * finalWidth;
  276. autoWidthCount--;
  277. }
  278. } else {
  279. // `barMinWidth/barMaxWidth` has higher priority than `barWidth`, as
  280. // CSS does. Because barWidth can be a percent value, where
  281. // `barMaxWidth` can be used to restrict the final width.
  282. var finalWidth = column.width;
  283. if (maxWidth) {
  284. finalWidth = Math.min(finalWidth, maxWidth);
  285. }
  286. // `minWidth` has higher priority, as described above
  287. if (minWidth) {
  288. finalWidth = Math.max(finalWidth, minWidth);
  289. }
  290. column.width = finalWidth;
  291. remainedWidth -= finalWidth + barGapPercent * finalWidth;
  292. autoWidthCount--;
  293. }
  294. });
  295. // Recalculate width again
  296. autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  297. autoWidth = Math.max(autoWidth, 0);
  298. var widthSum = 0;
  299. var lastColumn;
  300. each(stacks, function (column, idx) {
  301. if (!column.width) {
  302. column.width = autoWidth;
  303. }
  304. lastColumn = column;
  305. widthSum += column.width * (1 + barGapPercent);
  306. });
  307. if (lastColumn) {
  308. widthSum -= lastColumn.width * barGapPercent;
  309. }
  310. var offset = -widthSum / 2;
  311. each(stacks, function (column, stackId) {
  312. result[coordSysName][stackId] = result[coordSysName][stackId] || {
  313. bandWidth: bandWidth,
  314. offset: offset,
  315. width: column.width
  316. };
  317. offset += column.width * (1 + barGapPercent);
  318. });
  319. });
  320. return result;
  321. }
  322. function retrieveColumnLayout(barWidthAndOffset, axis, seriesModel) {
  323. if (barWidthAndOffset && axis) {
  324. var result = barWidthAndOffset[getAxisKey(axis)];
  325. if (result != null && seriesModel != null) {
  326. return result[getSeriesStackId(seriesModel)];
  327. }
  328. return result;
  329. }
  330. }
  331. export { retrieveColumnLayout };
  332. export function layout(seriesType, ecModel) {
  333. var seriesModels = prepareLayoutBarSeries(seriesType, ecModel);
  334. var barWidthAndOffset = makeColumnLayout(seriesModels);
  335. each(seriesModels, function (seriesModel) {
  336. var data = seriesModel.getData();
  337. var cartesian = seriesModel.coordinateSystem;
  338. var baseAxis = cartesian.getBaseAxis();
  339. var stackId = getSeriesStackId(seriesModel);
  340. var columnLayoutInfo = barWidthAndOffset[getAxisKey(baseAxis)][stackId];
  341. var columnOffset = columnLayoutInfo.offset;
  342. var columnWidth = columnLayoutInfo.width;
  343. data.setLayout({
  344. bandWidth: columnLayoutInfo.bandWidth,
  345. offset: columnOffset,
  346. size: columnWidth
  347. });
  348. });
  349. }
  350. // TODO: Do not support stack in large mode yet.
  351. export function createProgressiveLayout(seriesType) {
  352. return {
  353. seriesType: seriesType,
  354. plan: createRenderPlanner(),
  355. reset: function (seriesModel) {
  356. if (!isOnCartesian(seriesModel)) {
  357. return;
  358. }
  359. var data = seriesModel.getData();
  360. var cartesian = seriesModel.coordinateSystem;
  361. var baseAxis = cartesian.getBaseAxis();
  362. var valueAxis = cartesian.getOtherAxis(baseAxis);
  363. var valueDimIdx = data.getDimensionIndex(data.mapDimension(valueAxis.dim));
  364. var baseDimIdx = data.getDimensionIndex(data.mapDimension(baseAxis.dim));
  365. var drawBackground = seriesModel.get('showBackground', true);
  366. var valueDim = data.mapDimension(valueAxis.dim);
  367. var stackResultDim = data.getCalculationInfo('stackResultDimension');
  368. var stacked = isDimensionStacked(data, valueDim) && !!data.getCalculationInfo('stackedOnSeries');
  369. var isValueAxisH = valueAxis.isHorizontal();
  370. var valueAxisStart = getValueAxisStart(baseAxis, valueAxis);
  371. var isLarge = isInLargeMode(seriesModel);
  372. var barMinHeight = seriesModel.get('barMinHeight') || 0;
  373. var stackedDimIdx = stackResultDim && data.getDimensionIndex(stackResultDim);
  374. // Layout info.
  375. var columnWidth = data.getLayout('size');
  376. var columnOffset = data.getLayout('offset');
  377. return {
  378. progress: function (params, data) {
  379. var count = params.count;
  380. var largePoints = isLarge && createFloat32Array(count * 3);
  381. var largeBackgroundPoints = isLarge && drawBackground && createFloat32Array(count * 3);
  382. var largeDataIndices = isLarge && createFloat32Array(count);
  383. var coordLayout = cartesian.master.getRect();
  384. var bgSize = isValueAxisH ? coordLayout.width : coordLayout.height;
  385. var dataIndex;
  386. var store = data.getStore();
  387. var idxOffset = 0;
  388. while ((dataIndex = params.next()) != null) {
  389. var value = store.get(stacked ? stackedDimIdx : valueDimIdx, dataIndex);
  390. var baseValue = store.get(baseDimIdx, dataIndex);
  391. var baseCoord = valueAxisStart;
  392. var stackStartValue = void 0;
  393. // Because of the barMinHeight, we can not use the value in
  394. // stackResultDimension directly.
  395. if (stacked) {
  396. stackStartValue = +value - store.get(valueDimIdx, dataIndex);
  397. }
  398. var x = void 0;
  399. var y = void 0;
  400. var width = void 0;
  401. var height = void 0;
  402. if (isValueAxisH) {
  403. var coord = cartesian.dataToPoint([value, baseValue]);
  404. if (stacked) {
  405. var startCoord = cartesian.dataToPoint([stackStartValue, baseValue]);
  406. baseCoord = startCoord[0];
  407. }
  408. x = baseCoord;
  409. y = coord[1] + columnOffset;
  410. width = coord[0] - baseCoord;
  411. height = columnWidth;
  412. if (Math.abs(width) < barMinHeight) {
  413. width = (width < 0 ? -1 : 1) * barMinHeight;
  414. }
  415. } else {
  416. var coord = cartesian.dataToPoint([baseValue, value]);
  417. if (stacked) {
  418. var startCoord = cartesian.dataToPoint([baseValue, stackStartValue]);
  419. baseCoord = startCoord[1];
  420. }
  421. x = coord[0] + columnOffset;
  422. y = baseCoord;
  423. width = columnWidth;
  424. height = coord[1] - baseCoord;
  425. if (Math.abs(height) < barMinHeight) {
  426. // Include zero to has a positive bar
  427. height = (height <= 0 ? -1 : 1) * barMinHeight;
  428. }
  429. }
  430. if (!isLarge) {
  431. data.setItemLayout(dataIndex, {
  432. x: x,
  433. y: y,
  434. width: width,
  435. height: height
  436. });
  437. } else {
  438. largePoints[idxOffset] = x;
  439. largePoints[idxOffset + 1] = y;
  440. largePoints[idxOffset + 2] = isValueAxisH ? width : height;
  441. if (largeBackgroundPoints) {
  442. largeBackgroundPoints[idxOffset] = isValueAxisH ? coordLayout.x : x;
  443. largeBackgroundPoints[idxOffset + 1] = isValueAxisH ? y : coordLayout.y;
  444. largeBackgroundPoints[idxOffset + 2] = bgSize;
  445. }
  446. largeDataIndices[dataIndex] = dataIndex;
  447. }
  448. idxOffset += 3;
  449. }
  450. if (isLarge) {
  451. data.setLayout({
  452. largePoints: largePoints,
  453. largeDataIndices: largeDataIndices,
  454. largeBackgroundPoints: largeBackgroundPoints,
  455. valueAxisHorizontal: isValueAxisH
  456. });
  457. }
  458. }
  459. };
  460. }
  461. };
  462. }
  463. function isOnCartesian(seriesModel) {
  464. return seriesModel.coordinateSystem && seriesModel.coordinateSystem.type === 'cartesian2d';
  465. }
  466. function isInLargeMode(seriesModel) {
  467. return seriesModel.pipelineContext && seriesModel.pipelineContext.large;
  468. }
  469. // See cases in `test/bar-start.html` and `#7412`, `#8747`.
  470. function getValueAxisStart(baseAxis, valueAxis) {
  471. var startValue = valueAxis.model.get('startValue');
  472. if (!startValue) {
  473. startValue = 0;
  474. }
  475. return valueAxis.toGlobalCoord(valueAxis.dataToCoord(valueAxis.type === 'log' ? startValue > 0 ? startValue : 1 : startValue));
  476. }