barGrid.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. } // Ignore duplicated time values in the same axis
  125. }
  126. });
  127. var axisMinGaps = {};
  128. for (var key in axisValues) {
  129. if (axisValues.hasOwnProperty(key)) {
  130. var valuesInAxis = axisValues[key];
  131. if (valuesInAxis) {
  132. // Sort axis values into ascending order to calculate gaps
  133. valuesInAxis.sort(function (a, b) {
  134. return a - b;
  135. });
  136. var min = null;
  137. for (var j = 1; j < valuesInAxis.length; ++j) {
  138. var delta = valuesInAxis[j] - valuesInAxis[j - 1];
  139. if (delta > 0) {
  140. // Ignore 0 delta because they are of the same axis value
  141. min = min === null ? delta : Math.min(min, delta);
  142. }
  143. } // Set to null if only have one data
  144. axisMinGaps[key] = min;
  145. }
  146. }
  147. }
  148. return axisMinGaps;
  149. }
  150. export function makeColumnLayout(barSeries) {
  151. var axisMinGaps = getValueAxesMinGaps(barSeries);
  152. var seriesInfoList = [];
  153. each(barSeries, function (seriesModel) {
  154. var cartesian = seriesModel.coordinateSystem;
  155. var baseAxis = cartesian.getBaseAxis();
  156. var axisExtent = baseAxis.getExtent();
  157. var bandWidth;
  158. if (baseAxis.type === 'category') {
  159. bandWidth = baseAxis.getBandWidth();
  160. } else if (baseAxis.type === 'value' || baseAxis.type === 'time') {
  161. var key = baseAxis.dim + '_' + baseAxis.index;
  162. var minGap = axisMinGaps[key];
  163. var extentSpan = Math.abs(axisExtent[1] - axisExtent[0]);
  164. var scale = baseAxis.scale.getExtent();
  165. var scaleSpan = Math.abs(scale[1] - scale[0]);
  166. bandWidth = minGap ? extentSpan / scaleSpan * minGap : extentSpan; // When there is only one data value
  167. } else {
  168. var data = seriesModel.getData();
  169. bandWidth = Math.abs(axisExtent[1] - axisExtent[0]) / data.count();
  170. }
  171. var barWidth = parsePercent(seriesModel.get('barWidth'), bandWidth);
  172. var barMaxWidth = parsePercent(seriesModel.get('barMaxWidth'), bandWidth);
  173. var barMinWidth = parsePercent( // barMinWidth by default is 0.5 / 1 in cartesian. Because in value axis,
  174. // the auto-calculated bar width might be less than 0.5 / 1.
  175. seriesModel.get('barMinWidth') || (isInLargeMode(seriesModel) ? 0.5 : 1), bandWidth);
  176. var barGap = seriesModel.get('barGap');
  177. var barCategoryGap = seriesModel.get('barCategoryGap');
  178. seriesInfoList.push({
  179. bandWidth: bandWidth,
  180. barWidth: barWidth,
  181. barMaxWidth: barMaxWidth,
  182. barMinWidth: barMinWidth,
  183. barGap: barGap,
  184. barCategoryGap: barCategoryGap,
  185. axisKey: getAxisKey(baseAxis),
  186. stackId: getSeriesStackId(seriesModel)
  187. });
  188. });
  189. return doCalBarWidthAndOffset(seriesInfoList);
  190. }
  191. function doCalBarWidthAndOffset(seriesInfoList) {
  192. // Columns info on each category axis. Key is cartesian name
  193. var columnsMap = {};
  194. each(seriesInfoList, function (seriesInfo, idx) {
  195. var axisKey = seriesInfo.axisKey;
  196. var bandWidth = seriesInfo.bandWidth;
  197. var columnsOnAxis = columnsMap[axisKey] || {
  198. bandWidth: bandWidth,
  199. remainedWidth: bandWidth,
  200. autoWidthCount: 0,
  201. categoryGap: null,
  202. gap: '20%',
  203. stacks: {}
  204. };
  205. var stacks = columnsOnAxis.stacks;
  206. columnsMap[axisKey] = columnsOnAxis;
  207. var stackId = seriesInfo.stackId;
  208. if (!stacks[stackId]) {
  209. columnsOnAxis.autoWidthCount++;
  210. }
  211. stacks[stackId] = stacks[stackId] || {
  212. width: 0,
  213. maxWidth: 0
  214. }; // Caution: In a single coordinate system, these barGrid attributes
  215. // will be shared by series. Consider that they have default values,
  216. // only the attributes set on the last series will work.
  217. // Do not change this fact unless there will be a break change.
  218. var barWidth = seriesInfo.barWidth;
  219. if (barWidth && !stacks[stackId].width) {
  220. // See #6312, do not restrict width.
  221. stacks[stackId].width = barWidth;
  222. barWidth = Math.min(columnsOnAxis.remainedWidth, barWidth);
  223. columnsOnAxis.remainedWidth -= barWidth;
  224. }
  225. var barMaxWidth = seriesInfo.barMaxWidth;
  226. barMaxWidth && (stacks[stackId].maxWidth = barMaxWidth);
  227. var barMinWidth = seriesInfo.barMinWidth;
  228. barMinWidth && (stacks[stackId].minWidth = barMinWidth);
  229. var barGap = seriesInfo.barGap;
  230. barGap != null && (columnsOnAxis.gap = barGap);
  231. var barCategoryGap = seriesInfo.barCategoryGap;
  232. barCategoryGap != null && (columnsOnAxis.categoryGap = barCategoryGap);
  233. });
  234. var result = {};
  235. each(columnsMap, function (columnsOnAxis, coordSysName) {
  236. result[coordSysName] = {};
  237. var stacks = columnsOnAxis.stacks;
  238. var bandWidth = columnsOnAxis.bandWidth;
  239. var categoryGapPercent = columnsOnAxis.categoryGap;
  240. if (categoryGapPercent == null) {
  241. var columnCount = keys(stacks).length; // More columns in one group
  242. // the spaces between group is smaller. Or the column will be too thin.
  243. categoryGapPercent = Math.max(35 - columnCount * 4, 15) + '%';
  244. }
  245. var categoryGap = parsePercent(categoryGapPercent, bandWidth);
  246. var barGapPercent = parsePercent(columnsOnAxis.gap, 1);
  247. var remainedWidth = columnsOnAxis.remainedWidth;
  248. var autoWidthCount = columnsOnAxis.autoWidthCount;
  249. var autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  250. autoWidth = Math.max(autoWidth, 0); // Find if any auto calculated bar exceeded maxBarWidth
  251. each(stacks, function (column) {
  252. var maxWidth = column.maxWidth;
  253. var minWidth = column.minWidth;
  254. if (!column.width) {
  255. var finalWidth = autoWidth;
  256. if (maxWidth && maxWidth < finalWidth) {
  257. finalWidth = Math.min(maxWidth, remainedWidth);
  258. } // `minWidth` has higher priority. `minWidth` decide that whether the
  259. // bar is able to be visible. So `minWidth` should not be restricted
  260. // by `maxWidth` or `remainedWidth` (which is from `bandWidth`). In
  261. // the extreme cases for `value` axis, bars are allowed to overlap
  262. // with each other if `minWidth` specified.
  263. if (minWidth && minWidth > finalWidth) {
  264. finalWidth = minWidth;
  265. }
  266. if (finalWidth !== autoWidth) {
  267. column.width = finalWidth;
  268. remainedWidth -= finalWidth + barGapPercent * finalWidth;
  269. autoWidthCount--;
  270. }
  271. } else {
  272. // `barMinWidth/barMaxWidth` has higher priority than `barWidth`, as
  273. // CSS does. Because barWidth can be a percent value, where
  274. // `barMaxWidth` can be used to restrict the final width.
  275. var finalWidth = column.width;
  276. if (maxWidth) {
  277. finalWidth = Math.min(finalWidth, maxWidth);
  278. } // `minWidth` has higher priority, as described above
  279. if (minWidth) {
  280. finalWidth = Math.max(finalWidth, minWidth);
  281. }
  282. column.width = finalWidth;
  283. remainedWidth -= finalWidth + barGapPercent * finalWidth;
  284. autoWidthCount--;
  285. }
  286. }); // Recalculate width again
  287. autoWidth = (remainedWidth - categoryGap) / (autoWidthCount + (autoWidthCount - 1) * barGapPercent);
  288. autoWidth = Math.max(autoWidth, 0);
  289. var widthSum = 0;
  290. var lastColumn;
  291. each(stacks, function (column, idx) {
  292. if (!column.width) {
  293. column.width = autoWidth;
  294. }
  295. lastColumn = column;
  296. widthSum += column.width * (1 + barGapPercent);
  297. });
  298. if (lastColumn) {
  299. widthSum -= lastColumn.width * barGapPercent;
  300. }
  301. var offset = -widthSum / 2;
  302. each(stacks, function (column, stackId) {
  303. result[coordSysName][stackId] = result[coordSysName][stackId] || {
  304. bandWidth: bandWidth,
  305. offset: offset,
  306. width: column.width
  307. };
  308. offset += column.width * (1 + barGapPercent);
  309. });
  310. });
  311. return result;
  312. }
  313. function retrieveColumnLayout(barWidthAndOffset, axis, seriesModel) {
  314. if (barWidthAndOffset && axis) {
  315. var result = barWidthAndOffset[getAxisKey(axis)];
  316. if (result != null && seriesModel != null) {
  317. return result[getSeriesStackId(seriesModel)];
  318. }
  319. return result;
  320. }
  321. }
  322. export { retrieveColumnLayout };
  323. export function layout(seriesType, ecModel) {
  324. var seriesModels = prepareLayoutBarSeries(seriesType, ecModel);
  325. var barWidthAndOffset = makeColumnLayout(seriesModels);
  326. each(seriesModels, function (seriesModel) {
  327. var data = seriesModel.getData();
  328. var cartesian = seriesModel.coordinateSystem;
  329. var baseAxis = cartesian.getBaseAxis();
  330. var stackId = getSeriesStackId(seriesModel);
  331. var columnLayoutInfo = barWidthAndOffset[getAxisKey(baseAxis)][stackId];
  332. var columnOffset = columnLayoutInfo.offset;
  333. var columnWidth = columnLayoutInfo.width;
  334. data.setLayout({
  335. bandWidth: columnLayoutInfo.bandWidth,
  336. offset: columnOffset,
  337. size: columnWidth
  338. });
  339. });
  340. } // TODO: Do not support stack in large mode yet.
  341. export function createProgressiveLayout(seriesType) {
  342. return {
  343. seriesType: seriesType,
  344. plan: createRenderPlanner(),
  345. reset: function (seriesModel) {
  346. if (!isOnCartesian(seriesModel)) {
  347. return;
  348. }
  349. var data = seriesModel.getData();
  350. var cartesian = seriesModel.coordinateSystem;
  351. var baseAxis = cartesian.getBaseAxis();
  352. var valueAxis = cartesian.getOtherAxis(baseAxis);
  353. var valueDimIdx = data.getDimensionIndex(data.mapDimension(valueAxis.dim));
  354. var baseDimIdx = data.getDimensionIndex(data.mapDimension(baseAxis.dim));
  355. var drawBackground = seriesModel.get('showBackground', true);
  356. var valueDim = data.mapDimension(valueAxis.dim);
  357. var stackResultDim = data.getCalculationInfo('stackResultDimension');
  358. var stacked = isDimensionStacked(data, valueDim) && !!data.getCalculationInfo('stackedOnSeries');
  359. var isValueAxisH = valueAxis.isHorizontal();
  360. var valueAxisStart = getValueAxisStart(baseAxis, valueAxis);
  361. var isLarge = isInLargeMode(seriesModel);
  362. var barMinHeight = seriesModel.get('barMinHeight') || 0;
  363. var stackedDimIdx = stackResultDim && data.getDimensionIndex(stackResultDim); // Layout info.
  364. var columnWidth = data.getLayout('size');
  365. var columnOffset = data.getLayout('offset');
  366. return {
  367. progress: function (params, data) {
  368. var count = params.count;
  369. var largePoints = isLarge && createFloat32Array(count * 3);
  370. var largeBackgroundPoints = isLarge && drawBackground && createFloat32Array(count * 3);
  371. var largeDataIndices = isLarge && createFloat32Array(count);
  372. var coordLayout = cartesian.master.getRect();
  373. var bgSize = isValueAxisH ? coordLayout.width : coordLayout.height;
  374. var dataIndex;
  375. var store = data.getStore();
  376. var idxOffset = 0;
  377. while ((dataIndex = params.next()) != null) {
  378. var value = store.get(stacked ? stackedDimIdx : valueDimIdx, dataIndex);
  379. var baseValue = store.get(baseDimIdx, dataIndex);
  380. var baseCoord = valueAxisStart;
  381. var startValue = void 0; // Because of the barMinHeight, we can not use the value in
  382. // stackResultDimension directly.
  383. if (stacked) {
  384. startValue = +value - store.get(valueDimIdx, dataIndex);
  385. }
  386. var x = void 0;
  387. var y = void 0;
  388. var width = void 0;
  389. var height = void 0;
  390. if (isValueAxisH) {
  391. var coord = cartesian.dataToPoint([value, baseValue]);
  392. if (stacked) {
  393. var startCoord = cartesian.dataToPoint([startValue, baseValue]);
  394. baseCoord = startCoord[0];
  395. }
  396. x = baseCoord;
  397. y = coord[1] + columnOffset;
  398. width = coord[0] - baseCoord;
  399. height = columnWidth;
  400. if (Math.abs(width) < barMinHeight) {
  401. width = (width < 0 ? -1 : 1) * barMinHeight;
  402. }
  403. } else {
  404. var coord = cartesian.dataToPoint([baseValue, value]);
  405. if (stacked) {
  406. var startCoord = cartesian.dataToPoint([baseValue, startValue]);
  407. baseCoord = startCoord[1];
  408. }
  409. x = coord[0] + columnOffset;
  410. y = baseCoord;
  411. width = columnWidth;
  412. height = coord[1] - baseCoord;
  413. if (Math.abs(height) < barMinHeight) {
  414. // Include zero to has a positive bar
  415. height = (height <= 0 ? -1 : 1) * barMinHeight;
  416. }
  417. }
  418. if (!isLarge) {
  419. data.setItemLayout(dataIndex, {
  420. x: x,
  421. y: y,
  422. width: width,
  423. height: height
  424. });
  425. } else {
  426. largePoints[idxOffset] = x;
  427. largePoints[idxOffset + 1] = y;
  428. largePoints[idxOffset + 2] = isValueAxisH ? width : height;
  429. if (largeBackgroundPoints) {
  430. largeBackgroundPoints[idxOffset] = isValueAxisH ? coordLayout.x : x;
  431. largeBackgroundPoints[idxOffset + 1] = isValueAxisH ? y : coordLayout.y;
  432. largeBackgroundPoints[idxOffset + 2] = bgSize;
  433. }
  434. largeDataIndices[dataIndex] = dataIndex;
  435. }
  436. idxOffset += 3;
  437. }
  438. if (isLarge) {
  439. data.setLayout({
  440. largePoints: largePoints,
  441. largeDataIndices: largeDataIndices,
  442. largeBackgroundPoints: largeBackgroundPoints,
  443. valueAxisHorizontal: isValueAxisH
  444. });
  445. }
  446. }
  447. };
  448. }
  449. };
  450. }
  451. function isOnCartesian(seriesModel) {
  452. return seriesModel.coordinateSystem && seriesModel.coordinateSystem.type === 'cartesian2d';
  453. }
  454. function isInLargeMode(seriesModel) {
  455. return seriesModel.pipelineContext && seriesModel.pipelineContext.large;
  456. } // See cases in `test/bar-start.html` and `#7412`, `#8747`.
  457. function getValueAxisStart(baseAxis, valueAxis) {
  458. return valueAxis.toGlobalCoord(valueAxis.dataToCoord(valueAxis.type === 'log' ? 1 : 0));
  459. }