Source.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. var _util = require("zrender/lib/core/util");
  20. var createHashMap = _util.createHashMap;
  21. var isTypedArray = _util.isTypedArray;
  22. var _clazz = require("../util/clazz");
  23. var enableClassCheck = _clazz.enableClassCheck;
  24. var _sourceType = require("./helper/sourceType");
  25. var SOURCE_FORMAT_ORIGINAL = _sourceType.SOURCE_FORMAT_ORIGINAL;
  26. var SERIES_LAYOUT_BY_COLUMN = _sourceType.SERIES_LAYOUT_BY_COLUMN;
  27. var SOURCE_FORMAT_UNKNOWN = _sourceType.SOURCE_FORMAT_UNKNOWN;
  28. var SOURCE_FORMAT_TYPED_ARRAY = _sourceType.SOURCE_FORMAT_TYPED_ARRAY;
  29. var SOURCE_FORMAT_KEYED_COLUMNS = _sourceType.SOURCE_FORMAT_KEYED_COLUMNS;
  30. /*
  31. * Licensed to the Apache Software Foundation (ASF) under one
  32. * or more contributor license agreements. See the NOTICE file
  33. * distributed with this work for additional information
  34. * regarding copyright ownership. The ASF licenses this file
  35. * to you under the Apache License, Version 2.0 (the
  36. * "License"); you may not use this file except in compliance
  37. * with the License. You may obtain a copy of the License at
  38. *
  39. * http://www.apache.org/licenses/LICENSE-2.0
  40. *
  41. * Unless required by applicable law or agreed to in writing,
  42. * software distributed under the License is distributed on an
  43. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  44. * KIND, either express or implied. See the License for the
  45. * specific language governing permissions and limitations
  46. * under the License.
  47. */
  48. /**
  49. * [sourceFormat]
  50. *
  51. * + "original":
  52. * This format is only used in series.data, where
  53. * itemStyle can be specified in data item.
  54. *
  55. * + "arrayRows":
  56. * [
  57. * ['product', 'score', 'amount'],
  58. * ['Matcha Latte', 89.3, 95.8],
  59. * ['Milk Tea', 92.1, 89.4],
  60. * ['Cheese Cocoa', 94.4, 91.2],
  61. * ['Walnut Brownie', 85.4, 76.9]
  62. * ]
  63. *
  64. * + "objectRows":
  65. * [
  66. * {product: 'Matcha Latte', score: 89.3, amount: 95.8},
  67. * {product: 'Milk Tea', score: 92.1, amount: 89.4},
  68. * {product: 'Cheese Cocoa', score: 94.4, amount: 91.2},
  69. * {product: 'Walnut Brownie', score: 85.4, amount: 76.9}
  70. * ]
  71. *
  72. * + "keyedColumns":
  73. * {
  74. * 'product': ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie'],
  75. * 'count': [823, 235, 1042, 988],
  76. * 'score': [95.8, 81.4, 91.2, 76.9]
  77. * }
  78. *
  79. * + "typedArray"
  80. *
  81. * + "unknown"
  82. */
  83. /**
  84. * @constructor
  85. * @param {Object} fields
  86. * @param {string} fields.sourceFormat
  87. * @param {Array|Object} fields.fromDataset
  88. * @param {Array|Object} [fields.data]
  89. * @param {string} [seriesLayoutBy='column']
  90. * @param {Array.<Object|string>} [dimensionsDefine]
  91. * @param {Objet|HashMap} [encodeDefine]
  92. * @param {number} [startIndex=0]
  93. * @param {number} [dimensionsDetectCount]
  94. */
  95. function Source(fields) {
  96. /**
  97. * @type {boolean}
  98. */
  99. this.fromDataset = fields.fromDataset;
  100. /**
  101. * Not null/undefined.
  102. * @type {Array|Object}
  103. */
  104. this.data = fields.data || (fields.sourceFormat === SOURCE_FORMAT_KEYED_COLUMNS ? {} : []);
  105. /**
  106. * See also "detectSourceFormat".
  107. * Not null/undefined.
  108. * @type {string}
  109. */
  110. this.sourceFormat = fields.sourceFormat || SOURCE_FORMAT_UNKNOWN;
  111. /**
  112. * 'row' or 'column'
  113. * Not null/undefined.
  114. * @type {string} seriesLayoutBy
  115. */
  116. this.seriesLayoutBy = fields.seriesLayoutBy || SERIES_LAYOUT_BY_COLUMN;
  117. /**
  118. * dimensions definition in option.
  119. * can be null/undefined.
  120. * @type {Array.<Object|string>}
  121. */
  122. this.dimensionsDefine = fields.dimensionsDefine;
  123. /**
  124. * encode definition in option.
  125. * can be null/undefined.
  126. * @type {Objet|HashMap}
  127. */
  128. this.encodeDefine = fields.encodeDefine && createHashMap(fields.encodeDefine);
  129. /**
  130. * Not null/undefined, uint.
  131. * @type {number}
  132. */
  133. this.startIndex = fields.startIndex || 0;
  134. /**
  135. * Can be null/undefined (when unknown), uint.
  136. * @type {number}
  137. */
  138. this.dimensionsDetectCount = fields.dimensionsDetectCount;
  139. }
  140. /**
  141. * Wrap original series data for some compatibility cases.
  142. */
  143. Source.seriesDataToSource = function (data) {
  144. return new Source({
  145. data: data,
  146. sourceFormat: isTypedArray(data) ? SOURCE_FORMAT_TYPED_ARRAY : SOURCE_FORMAT_ORIGINAL,
  147. fromDataset: false
  148. });
  149. };
  150. enableClassCheck(Source);
  151. var _default = Source;
  152. module.exports = _default;