dataFormat.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import { retrieveRawValue } from '../../data/helper/dataProvider.js';
  42. import { formatTpl } from '../../util/format.js';
  43. import { error, makePrintable } from '../../util/log.js';
  44. var DIMENSION_LABEL_REG = /\{@(.+?)\}/g;
  45. var DataFormatMixin = /** @class */function () {
  46. function DataFormatMixin() {}
  47. /**
  48. * Get params for formatter
  49. */
  50. DataFormatMixin.prototype.getDataParams = function (dataIndex, dataType) {
  51. var data = this.getData(dataType);
  52. var rawValue = this.getRawValue(dataIndex, dataType);
  53. var rawDataIndex = data.getRawIndex(dataIndex);
  54. var name = data.getName(dataIndex);
  55. var itemOpt = data.getRawDataItem(dataIndex);
  56. var style = data.getItemVisual(dataIndex, 'style');
  57. var color = style && style[data.getItemVisual(dataIndex, 'drawType') || 'fill'];
  58. var borderColor = style && style.stroke;
  59. var mainType = this.mainType;
  60. var isSeries = mainType === 'series';
  61. var userOutput = data.userOutput && data.userOutput.get();
  62. return {
  63. componentType: mainType,
  64. componentSubType: this.subType,
  65. componentIndex: this.componentIndex,
  66. seriesType: isSeries ? this.subType : null,
  67. seriesIndex: this.seriesIndex,
  68. seriesId: isSeries ? this.id : null,
  69. seriesName: isSeries ? this.name : null,
  70. name: name,
  71. dataIndex: rawDataIndex,
  72. data: itemOpt,
  73. dataType: dataType,
  74. value: rawValue,
  75. color: color,
  76. borderColor: borderColor,
  77. dimensionNames: userOutput ? userOutput.fullDimensions : null,
  78. encode: userOutput ? userOutput.encode : null,
  79. // Param name list for mapping `a`, `b`, `c`, `d`, `e`
  80. $vars: ['seriesName', 'name', 'value']
  81. };
  82. };
  83. /**
  84. * Format label
  85. * @param dataIndex
  86. * @param status 'normal' by default
  87. * @param dataType
  88. * @param labelDimIndex Only used in some chart that
  89. * use formatter in different dimensions, like radar.
  90. * @param formatter Formatter given outside.
  91. * @return return null/undefined if no formatter
  92. */
  93. DataFormatMixin.prototype.getFormattedLabel = function (dataIndex, status, dataType, labelDimIndex, formatter, extendParams) {
  94. status = status || 'normal';
  95. var data = this.getData(dataType);
  96. var params = this.getDataParams(dataIndex, dataType);
  97. if (extendParams) {
  98. params.value = extendParams.interpolatedValue;
  99. }
  100. if (labelDimIndex != null && zrUtil.isArray(params.value)) {
  101. params.value = params.value[labelDimIndex];
  102. }
  103. if (!formatter) {
  104. var itemModel = data.getItemModel(dataIndex);
  105. // @ts-ignore
  106. formatter = itemModel.get(status === 'normal' ? ['label', 'formatter'] : [status, 'label', 'formatter']);
  107. }
  108. if (zrUtil.isFunction(formatter)) {
  109. params.status = status;
  110. params.dimensionIndex = labelDimIndex;
  111. return formatter(params);
  112. } else if (zrUtil.isString(formatter)) {
  113. var str = formatTpl(formatter, params);
  114. // Support 'aaa{@[3]}bbb{@product}ccc'.
  115. // Do not support '}' in dim name util have to.
  116. return str.replace(DIMENSION_LABEL_REG, function (origin, dimStr) {
  117. var len = dimStr.length;
  118. var dimLoose = dimStr;
  119. if (dimLoose.charAt(0) === '[' && dimLoose.charAt(len - 1) === ']') {
  120. dimLoose = +dimLoose.slice(1, len - 1); // Also support: '[]' => 0
  121. if (process.env.NODE_ENV !== 'production') {
  122. if (isNaN(dimLoose)) {
  123. error("Invalide label formatter: @" + dimStr + ", only support @[0], @[1], @[2], ...");
  124. }
  125. }
  126. }
  127. var val = retrieveRawValue(data, dataIndex, dimLoose);
  128. if (extendParams && zrUtil.isArray(extendParams.interpolatedValue)) {
  129. var dimIndex = data.getDimensionIndex(dimLoose);
  130. if (dimIndex >= 0) {
  131. val = extendParams.interpolatedValue[dimIndex];
  132. }
  133. }
  134. return val != null ? val + '' : '';
  135. });
  136. }
  137. };
  138. /**
  139. * Get raw value in option
  140. */
  141. DataFormatMixin.prototype.getRawValue = function (idx, dataType) {
  142. return retrieveRawValue(this.getData(dataType), idx);
  143. };
  144. /**
  145. * Should be implemented.
  146. * @param {number} dataIndex
  147. * @param {boolean} [multipleSeries=false]
  148. * @param {string} [dataType]
  149. */
  150. DataFormatMixin.prototype.formatTooltip = function (dataIndex, multipleSeries, dataType) {
  151. // Empty function
  152. return;
  153. };
  154. return DataFormatMixin;
  155. }();
  156. export { DataFormatMixin };
  157. ;
  158. // PENDING: previously we accept this type when calling `formatTooltip`,
  159. // but guess little chance has been used outside. Do we need to backward
  160. // compat it?
  161. // type TooltipFormatResultLegacyObject = {
  162. // // `html` means the markup language text, either in 'html' or 'richText'.
  163. // // The name `html` is not appropriate because in 'richText' it is not a HTML
  164. // // string. But still support it for backward compatibility.
  165. // html: string;
  166. // markers: Dictionary<ColorString>;
  167. // };
  168. /**
  169. * For backward compat, normalize the return from `formatTooltip`.
  170. */
  171. export function normalizeTooltipFormatResult(result) {
  172. var markupText;
  173. // let markers: Dictionary<ColorString>;
  174. var markupFragment;
  175. if (zrUtil.isObject(result)) {
  176. if (result.type) {
  177. markupFragment = result;
  178. } else {
  179. if (process.env.NODE_ENV !== 'production') {
  180. console.warn('The return type of `formatTooltip` is not supported: ' + makePrintable(result));
  181. }
  182. }
  183. // else {
  184. // markupText = (result as TooltipFormatResultLegacyObject).html;
  185. // markers = (result as TooltipFormatResultLegacyObject).markers;
  186. // if (markersExisting) {
  187. // markers = zrUtil.merge(markersExisting, markers);
  188. // }
  189. // }
  190. } else {
  191. markupText = result;
  192. }
  193. return {
  194. text: markupText,
  195. // markers: markers || markersExisting,
  196. frag: markupFragment
  197. };
  198. }