sortTransform.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 { SOURCE_FORMAT_ARRAY_ROWS, SOURCE_FORMAT_OBJECT_ROWS } from '../../util/types.js';
  41. import { makePrintable, throwError } from '../../util/log.js';
  42. import { each } from 'zrender/lib/core/util.js';
  43. import { normalizeToArray } from '../../util/model.js';
  44. import { getRawValueParser, SortOrderComparator } from '../../data/helper/dataValueHelper.js';
  45. var sampleLog = '';
  46. if (process.env.NODE_ENV !== 'production') {
  47. sampleLog = ['Valid config is like:', '{ dimension: "age", order: "asc" }', 'or [{ dimension: "age", order: "asc"], { dimension: "date", order: "desc" }]'].join(' ');
  48. }
  49. export var sortTransform = {
  50. type: 'echarts:sort',
  51. transform: function (params) {
  52. var upstream = params.upstream;
  53. var config = params.config;
  54. var errMsg = ''; // Normalize
  55. // const orderExprList: OrderExpression[] = isArray(config[0])
  56. // ? config as OrderExpression[]
  57. // : [config as OrderExpression];
  58. var orderExprList = normalizeToArray(config);
  59. if (!orderExprList.length) {
  60. if (process.env.NODE_ENV !== 'production') {
  61. errMsg = 'Empty `config` in sort transform.';
  62. }
  63. throwError(errMsg);
  64. }
  65. var orderDefList = [];
  66. each(orderExprList, function (orderExpr) {
  67. var dimLoose = orderExpr.dimension;
  68. var order = orderExpr.order;
  69. var parserName = orderExpr.parser;
  70. var incomparable = orderExpr.incomparable;
  71. if (dimLoose == null) {
  72. if (process.env.NODE_ENV !== 'production') {
  73. errMsg = 'Sort transform config must has "dimension" specified.' + sampleLog;
  74. }
  75. throwError(errMsg);
  76. }
  77. if (order !== 'asc' && order !== 'desc') {
  78. if (process.env.NODE_ENV !== 'production') {
  79. errMsg = 'Sort transform config must has "order" specified.' + sampleLog;
  80. }
  81. throwError(errMsg);
  82. }
  83. if (incomparable && incomparable !== 'min' && incomparable !== 'max') {
  84. var errMsg_1 = '';
  85. if (process.env.NODE_ENV !== 'production') {
  86. errMsg_1 = 'incomparable must be "min" or "max" rather than "' + incomparable + '".';
  87. }
  88. throwError(errMsg_1);
  89. }
  90. if (order !== 'asc' && order !== 'desc') {
  91. var errMsg_2 = '';
  92. if (process.env.NODE_ENV !== 'production') {
  93. errMsg_2 = 'order must be "asc" or "desc" rather than "' + order + '".';
  94. }
  95. throwError(errMsg_2);
  96. }
  97. var dimInfo = upstream.getDimensionInfo(dimLoose);
  98. if (!dimInfo) {
  99. if (process.env.NODE_ENV !== 'production') {
  100. errMsg = makePrintable('Can not find dimension info via: ' + dimLoose + '.\n', 'Existing dimensions: ', upstream.cloneAllDimensionInfo(), '.\n', 'Illegal config:', orderExpr, '.\n');
  101. }
  102. throwError(errMsg);
  103. }
  104. var parser = parserName ? getRawValueParser(parserName) : null;
  105. if (parserName && !parser) {
  106. if (process.env.NODE_ENV !== 'production') {
  107. errMsg = makePrintable('Invalid parser name ' + parserName + '.\n', 'Illegal config:', orderExpr, '.\n');
  108. }
  109. throwError(errMsg);
  110. }
  111. orderDefList.push({
  112. dimIdx: dimInfo.index,
  113. parser: parser,
  114. comparator: new SortOrderComparator(order, incomparable)
  115. });
  116. }); // TODO: support it?
  117. var sourceFormat = upstream.sourceFormat;
  118. if (sourceFormat !== SOURCE_FORMAT_ARRAY_ROWS && sourceFormat !== SOURCE_FORMAT_OBJECT_ROWS) {
  119. if (process.env.NODE_ENV !== 'production') {
  120. errMsg = 'sourceFormat "' + sourceFormat + '" is not supported yet';
  121. }
  122. throwError(errMsg);
  123. } // Other upstream format are all array.
  124. var resultData = [];
  125. for (var i = 0, len = upstream.count(); i < len; i++) {
  126. resultData.push(upstream.getRawDataItem(i));
  127. }
  128. resultData.sort(function (item0, item1) {
  129. for (var i = 0; i < orderDefList.length; i++) {
  130. var orderDef = orderDefList[i];
  131. var val0 = upstream.retrieveValueFromItem(item0, orderDef.dimIdx);
  132. var val1 = upstream.retrieveValueFromItem(item1, orderDef.dimIdx);
  133. if (orderDef.parser) {
  134. val0 = orderDef.parser(val0);
  135. val1 = orderDef.parser(val1);
  136. }
  137. var result = orderDef.comparator.evaluate(val0, val1);
  138. if (result !== 0) {
  139. return result;
  140. }
  141. }
  142. return 0;
  143. });
  144. return {
  145. data: resultData
  146. };
  147. }
  148. };