dataFormat.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 _dataProvider = require("../../data/helper/dataProvider");
  20. var retrieveRawValue = _dataProvider.retrieveRawValue;
  21. var _format = require("../../util/format");
  22. var getTooltipMarker = _format.getTooltipMarker;
  23. var formatTpl = _format.formatTpl;
  24. var _model = require("../../util/model");
  25. var getTooltipRenderMode = _model.getTooltipRenderMode;
  26. /*
  27. * Licensed to the Apache Software Foundation (ASF) under one
  28. * or more contributor license agreements. See the NOTICE file
  29. * distributed with this work for additional information
  30. * regarding copyright ownership. The ASF licenses this file
  31. * to you under the Apache License, Version 2.0 (the
  32. * "License"); you may not use this file except in compliance
  33. * with the License. You may obtain a copy of the License at
  34. *
  35. * http://www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing,
  38. * software distributed under the License is distributed on an
  39. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  40. * KIND, either express or implied. See the License for the
  41. * specific language governing permissions and limitations
  42. * under the License.
  43. */
  44. var DIMENSION_LABEL_REG = /\{@(.+?)\}/g; // PENDING A little ugly
  45. var _default = {
  46. /**
  47. * Get params for formatter
  48. * @param {number} dataIndex
  49. * @param {string} [dataType]
  50. * @return {Object}
  51. */
  52. 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 color = data.getItemVisual(dataIndex, 'color');
  59. var borderColor = data.getItemVisual(dataIndex, 'borderColor');
  60. var tooltipModel = this.ecModel.getComponent('tooltip');
  61. var renderModeOption = tooltipModel && tooltipModel.get('renderMode');
  62. var renderMode = getTooltipRenderMode(renderModeOption);
  63. var mainType = this.mainType;
  64. var isSeries = mainType === 'series';
  65. var userOutput = data.userOutput;
  66. return {
  67. componentType: mainType,
  68. componentSubType: this.subType,
  69. componentIndex: this.componentIndex,
  70. seriesType: isSeries ? this.subType : null,
  71. seriesIndex: this.seriesIndex,
  72. seriesId: isSeries ? this.id : null,
  73. seriesName: isSeries ? this.name : null,
  74. name: name,
  75. dataIndex: rawDataIndex,
  76. data: itemOpt,
  77. dataType: dataType,
  78. value: rawValue,
  79. color: color,
  80. borderColor: borderColor,
  81. dimensionNames: userOutput ? userOutput.dimensionNames : null,
  82. encode: userOutput ? userOutput.encode : null,
  83. marker: getTooltipMarker({
  84. color: color,
  85. renderMode: renderMode
  86. }),
  87. // Param name list for mapping `a`, `b`, `c`, `d`, `e`
  88. $vars: ['seriesName', 'name', 'value']
  89. };
  90. },
  91. /**
  92. * Format label
  93. * @param {number} dataIndex
  94. * @param {string} [status='normal'] 'normal' or 'emphasis'
  95. * @param {string} [dataType]
  96. * @param {number} [dimIndex] Only used in some chart that
  97. * use formatter in different dimensions, like radar.
  98. * @param {string} [labelProp='label']
  99. * @return {string} If not formatter, return null/undefined
  100. */
  101. getFormattedLabel: function (dataIndex, status, dataType, dimIndex, labelProp) {
  102. status = status || 'normal';
  103. var data = this.getData(dataType);
  104. var itemModel = data.getItemModel(dataIndex);
  105. var params = this.getDataParams(dataIndex, dataType);
  106. if (dimIndex != null && params.value instanceof Array) {
  107. params.value = params.value[dimIndex];
  108. }
  109. var formatter = itemModel.get(status === 'normal' ? [labelProp || 'label', 'formatter'] : [status, labelProp || 'label', 'formatter']);
  110. if (typeof formatter === 'function') {
  111. params.status = status;
  112. params.dimensionIndex = dimIndex;
  113. return formatter(params);
  114. } else if (typeof formatter === 'string') {
  115. var str = formatTpl(formatter, params); // Support 'aaa{@[3]}bbb{@product}ccc'.
  116. // Do not support '}' in dim name util have to.
  117. return str.replace(DIMENSION_LABEL_REG, function (origin, dim) {
  118. var len = dim.length;
  119. if (dim.charAt(0) === '[' && dim.charAt(len - 1) === ']') {
  120. dim = +dim.slice(1, len - 1); // Also: '[]' => 0
  121. }
  122. return retrieveRawValue(data, dataIndex, dim);
  123. });
  124. }
  125. },
  126. /**
  127. * Get raw value in option
  128. * @param {number} idx
  129. * @param {string} [dataType]
  130. * @return {Array|number|string}
  131. */
  132. getRawValue: function (idx, dataType) {
  133. return retrieveRawValue(this.getData(dataType), idx);
  134. },
  135. /**
  136. * Should be implemented.
  137. * @param {number} dataIndex
  138. * @param {boolean} [multipleSeries=false]
  139. * @param {number} [dataType]
  140. * @return {string} tooltip string
  141. */
  142. formatTooltip: function () {// Empty function
  143. }
  144. };
  145. module.exports = _default;