dataFormat.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 =
  46. /** @class */
  47. function () {
  48. function DataFormatMixin() {}
  49. /**
  50. * Get params for formatter
  51. */
  52. DataFormatMixin.prototype.getDataParams = function (dataIndex, dataType) {
  53. var data = this.getData(dataType);
  54. var rawValue = this.getRawValue(dataIndex, dataType);
  55. var rawDataIndex = data.getRawIndex(dataIndex);
  56. var name = data.getName(dataIndex);
  57. var itemOpt = data.getRawDataItem(dataIndex);
  58. var style = data.getItemVisual(dataIndex, 'style');
  59. var color = style && style[data.getItemVisual(dataIndex, 'drawType') || 'fill'];
  60. var borderColor = style && style.stroke;
  61. var mainType = this.mainType;
  62. var isSeries = mainType === 'series';
  63. var userOutput = data.userOutput && data.userOutput.get();
  64. return {
  65. componentType: mainType,
  66. componentSubType: this.subType,
  67. componentIndex: this.componentIndex,
  68. seriesType: isSeries ? this.subType : null,
  69. seriesIndex: this.seriesIndex,
  70. seriesId: isSeries ? this.id : null,
  71. seriesName: isSeries ? this.name : null,
  72. name: name,
  73. dataIndex: rawDataIndex,
  74. data: itemOpt,
  75. dataType: dataType,
  76. value: rawValue,
  77. color: color,
  78. borderColor: borderColor,
  79. dimensionNames: userOutput ? userOutput.fullDimensions : null,
  80. encode: userOutput ? userOutput.encode : null,
  81. // Param name list for mapping `a`, `b`, `c`, `d`, `e`
  82. $vars: ['seriesName', 'name', 'value']
  83. };
  84. };
  85. /**
  86. * Format label
  87. * @param dataIndex
  88. * @param status 'normal' by default
  89. * @param dataType
  90. * @param labelDimIndex Only used in some chart that
  91. * use formatter in different dimensions, like radar.
  92. * @param formatter Formatter given outside.
  93. * @return return null/undefined if no formatter
  94. */
  95. DataFormatMixin.prototype.getFormattedLabel = function (dataIndex, status, dataType, labelDimIndex, formatter, extendParams) {
  96. status = status || 'normal';
  97. var data = this.getData(dataType);
  98. var params = this.getDataParams(dataIndex, dataType);
  99. if (extendParams) {
  100. params.value = extendParams.interpolatedValue;
  101. }
  102. if (labelDimIndex != null && zrUtil.isArray(params.value)) {
  103. params.value = params.value[labelDimIndex];
  104. }
  105. if (!formatter) {
  106. var itemModel = data.getItemModel(dataIndex); // @ts-ignore
  107. formatter = itemModel.get(status === 'normal' ? ['label', 'formatter'] : [status, 'label', 'formatter']);
  108. }
  109. if (zrUtil.isFunction(formatter)) {
  110. params.status = status;
  111. params.dimensionIndex = labelDimIndex;
  112. return formatter(params);
  113. } else if (zrUtil.isString(formatter)) {
  114. var str = formatTpl(formatter, params); // 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. ; // PENDING: previously we accept this type when calling `formatTooltip`,
  158. // but guess little chance has been used outside. Do we need to backward
  159. // compat it?
  160. // type TooltipFormatResultLegacyObject = {
  161. // // `html` means the markup language text, either in 'html' or 'richText'.
  162. // // The name `html` is not appropriate because in 'richText' it is not a HTML
  163. // // string. But still support it for backward compatibility.
  164. // html: string;
  165. // markers: Dictionary<ColorString>;
  166. // };
  167. /**
  168. * For backward compat, normalize the return from `formatTooltip`.
  169. */
  170. export function normalizeTooltipFormatResult(result) {
  171. var markupText; // let markers: Dictionary<ColorString>;
  172. var markupFragment;
  173. if (zrUtil.isObject(result)) {
  174. if (result.type) {
  175. markupFragment = result;
  176. } else {
  177. if (process.env.NODE_ENV !== 'production') {
  178. console.warn('The return type of `formatTooltip` is not supported: ' + makePrintable(result));
  179. }
  180. } // else {
  181. // markupText = (result as TooltipFormatResultLegacyObject).html;
  182. // markers = (result as TooltipFormatResultLegacyObject).markers;
  183. // if (markersExisting) {
  184. // markers = zrUtil.merge(markersExisting, markers);
  185. // }
  186. // }
  187. } else {
  188. markupText = result;
  189. }
  190. return {
  191. text: markupText,
  192. // markers: markers || markersExisting,
  193. frag: markupFragment
  194. };
  195. }