format.ts 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

S
sushuang 已提交
20 21
import * as zrUtil from 'zrender/src/core/util';
import * as textContain from 'zrender/src/contain/text';
S
sushuang 已提交
22
import * as numberUtil from './number';
23
import {TooltipRenderMode} from './types';
P
pissang 已提交
24
import { Dictionary } from 'zrender/src/core/types';
25
import { StyleProps } from 'zrender/src/graphic/Style';
S
sushuang 已提交
26
// import Text from 'zrender/src/graphic/Text';
S
sushuang 已提交
27 28

/**
29
 * Add a comma each three digit.
S
sushuang 已提交
30
 */
P
pissang 已提交
31 32
export function addCommas(x: string | number): string {
    if (isNaN(x as number)) {
S
sushuang 已提交
33 34
        return '-';
    }
P
pissang 已提交
35 36 37
    const parts = (x + '').split('.');
    return parts[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g, '$1,')
            + (parts.length > 1 ? ('.' + parts[1]) : '');
S
sushuang 已提交
38
}
S
sushuang 已提交
39

40
export function toCamelCase(str: string, upperCaseFirst?: boolean): string {
S
sushuang 已提交
41
    str = (str || '').toLowerCase().replace(/-(.)/g, function (match, group1) {
S
sushuang 已提交
42 43 44 45 46 47 48 49
        return group1.toUpperCase();
    });

    if (upperCaseFirst && str) {
        str = str.charAt(0).toUpperCase() + str.slice(1);
    }

    return str;
S
sushuang 已提交
50
}
S
sushuang 已提交
51

S
sushuang 已提交
52
export var normalizeCssArray = zrUtil.normalizeCssArray;
S
sushuang 已提交
53

S
tweak.  
sushuang 已提交
54 55

var replaceReg = /([&<>"'])/g;
P
pissang 已提交
56
var replaceMap: Dictionary<string> = {
S
tweak.  
sushuang 已提交
57 58 59 60
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
S
sushuang 已提交
61
    '\'': '&#39;'
S
tweak.  
sushuang 已提交
62 63
};

P
pissang 已提交
64
export function encodeHTML(source: string): string {
S
tweak.  
sushuang 已提交
65 66
    return source == null
        ? ''
S
sushuang 已提交
67 68
        : (source + '').replace(replaceReg, function (str, c) {
            return replaceMap[c];
S
tweak.  
sushuang 已提交
69
        });
S
sushuang 已提交
70
}
S
sushuang 已提交
71 72 73

var TPL_VAR_ALIAS = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

74
var wrapVar = function (varName: string, seriesIdx?: number): string {
S
sushuang 已提交
75 76 77
    return '{' + varName + (seriesIdx == null ? '' : seriesIdx) + '}';
};

P
pissang 已提交
78
export interface TplFormatterParam extends Dictionary<any> {
79 80 81
    // Param name list for mapping `a`, `b`, `c`, `d`, `e`
    $vars: string[];
}
S
sushuang 已提交
82 83 84 85
/**
 * Template formatter
 * @param {Array.<Object>|Object} paramsList
 */
86 87 88 89 90
export function formatTpl(
    tpl: string,
    paramsList: TplFormatterParam | TplFormatterParam[],
    encode?: boolean
): string {
S
sushuang 已提交
91 92 93
    if (!zrUtil.isArray(paramsList)) {
        paramsList = [paramsList];
    }
P
pissang 已提交
94
    var seriesLen = paramsList.length;
S
sushuang 已提交
95 96 97 98 99 100 101
    if (!seriesLen) {
        return '';
    }

    var $vars = paramsList[0].$vars || [];
    for (var i = 0; i < $vars.length; i++) {
        var alias = TPL_VAR_ALIAS[i];
S
sushuang 已提交
102
        tpl = tpl.replace(wrapVar(alias), wrapVar(alias, 0));
S
sushuang 已提交
103 104 105 106
    }
    for (var seriesIdx = 0; seriesIdx < seriesLen; seriesIdx++) {
        for (var k = 0; k < $vars.length; k++) {
            var val = paramsList[seriesIdx][$vars[k]];
D
dengxiaohong01 已提交
107
            tpl = tpl.replace(
S
sushuang 已提交
108 109
                wrapVar(TPL_VAR_ALIAS[k], seriesIdx),
                encode ? encodeHTML(val) : val
D
dengxiaohong01 已提交
110
            );
D
deqingli 已提交
111
        }
S
sushuang 已提交
112 113 114
    }

    return tpl;
S
sushuang 已提交
115
}
S
sushuang 已提交
116 117 118 119

/**
 * simple Template formatter
 */
P
pissang 已提交
120
export function formatTplSimple(tpl: string, param: Dictionary<any>, encode?: boolean) {
S
sushuang 已提交
121 122 123 124 125 126 127
    zrUtil.each(param, function (value, key) {
        tpl = tpl.replace(
            '{' + key + '}',
            encode ? encodeHTML(value) : value
        );
    });
    return tpl;
S
sushuang 已提交
128
}
S
sushuang 已提交
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
interface RichTextTooltipMarker {
    renderMode: TooltipRenderMode;
    content: string;
    style: {
        color: string
        [key: string]: any
    };
}
export type TooltipMarker = string | RichTextTooltipMarker;
interface GetTooltipMarkerOpt {
    color?: string;
    extraCssText?: string;
    // By default: 'item'
    type?: 'item' | 'subItem';
    renderMode?: TooltipRenderMode;
    // id name for marker. If only one marker is in a rich text, this can be omitted.
    // By default: 'X'
    markerId?: string;
}
export function getTooltipMarker(color: string, extraCssText?: string): TooltipMarker;
export function getTooltipMarker(opt: GetTooltipMarkerOpt): TooltipMarker;
export function getTooltipMarker(opt: string | GetTooltipMarkerOpt, extraCssText?: string): TooltipMarker {
S
tweak  
sushuang 已提交
152 153 154 155
    opt = zrUtil.isString(opt) ? {color: opt, extraCssText: extraCssText} : (opt || {});
    var color = opt.color;
    var type = opt.type;
    var extraCssText = opt.extraCssText;
156 157
    var renderMode = opt.renderMode || 'html';
    var markerId = opt.markerId || 'X';
S
tweak  
sushuang 已提交
158 159 160 161 162

    if (!color) {
        return '';
    }

163
    if (renderMode === 'html') {
164
        return type === 'subItem'
S
tweak  
sushuang 已提交
165 166
        ? '<span style="display:inline-block;vertical-align:middle;margin-right:8px;margin-left:3px;'
            + 'border-radius:4px;width:4px;height:4px;background-color:'
S
sushuang 已提交
167
            + encodeHTML(color) + ';' + (extraCssText || '') + '"></span>'
S
tweak  
sushuang 已提交
168 169 170
        : '<span style="display:inline-block;margin-right:5px;'
            + 'border-radius:10px;width:10px;height:10px;background-color:'
            + encodeHTML(color) + ';' + (extraCssText || '') + '"></span>';
171
    }
172 173 174 175 176 177 178 179 180 181
    else {
        // Space for rich element marker
        return {
            renderMode: renderMode,
            content: '{marker' + markerId + '|}  ',
            style: {
                color: color
            }
        };
    }
S
sushuang 已提交
182
}
S
sushuang 已提交
183

P
pissang 已提交
184
function pad(str: string, len: number): string {
S
sushuang 已提交
185 186 187 188
    str += '';
    return '0000'.substr(0, len - str.length) + str;
}

S
sushuang 已提交
189 190 191 192 193 194 195 196 197 198

/**
 * ISO Date format
 * @param {string} tpl
 * @param {number} value
 * @param {boolean} [isUTC=false] Default in local time.
 *           see `module:echarts/scale/Time`
 *           and `module:echarts/util/number#parseDate`.
 * @inner
 */
1
100pah 已提交
199
export function formatTime(tpl: string, value: number | string | Date, isUTC?: boolean) {
S
sushuang 已提交
200 201 202 203 204 205 206 207 208 209 210
    if (tpl === 'week'
        || tpl === 'month'
        || tpl === 'quarter'
        || tpl === 'half-year'
        || tpl === 'year'
    ) {
        tpl = 'MM-dd\nyyyy';
    }

    var date = numberUtil.parseDate(value);
    var utc = isUTC ? 'UTC' : '';
P
pissang 已提交
211 212 213 214 215 216 217
    var y = (date as any)['get' + utc + 'FullYear']();
    var M = (date as any)['get' + utc + 'Month']() + 1;
    var d = (date as any)['get' + utc + 'Date']();
    var h = (date as any)['get' + utc + 'Hours']();
    var m = (date as any)['get' + utc + 'Minutes']();
    var s = (date as any)['get' + utc + 'Seconds']();
    var S = (date as any)['get' + utc + 'Milliseconds']();
S
sushuang 已提交
218

S
sushuang 已提交
219
    tpl = tpl.replace('MM', pad(M, 2))
S
sushuang 已提交
220 221
        .replace('M', M)
        .replace('yyyy', y)
P
pissang 已提交
222
        .replace('yy', y % 100 + '')
S
sushuang 已提交
223
        .replace('dd', pad(d, 2))
S
sushuang 已提交
224
        .replace('d', d)
S
sushuang 已提交
225
        .replace('hh', pad(h, 2))
S
sushuang 已提交
226
        .replace('h', h)
S
sushuang 已提交
227
        .replace('mm', pad(m, 2))
S
sushuang 已提交
228
        .replace('m', m)
S
sushuang 已提交
229 230 231
        .replace('ss', pad(s, 2))
        .replace('s', s)
        .replace('SSS', pad(S, 3));
S
sushuang 已提交
232 233

    return tpl;
S
sushuang 已提交
234
}
S
sushuang 已提交
235 236 237 238 239 240

/**
 * Capital first
 * @param {string} str
 * @return {string}
 */
P
pissang 已提交
241
export function capitalFirst(str: string): string {
S
sushuang 已提交
242
    return str ? str.charAt(0).toUpperCase() + str.substr(1) : str;
S
sushuang 已提交
243
}
S
sushuang 已提交
244

S
sushuang 已提交
245
export var truncateText = textContain.truncateText;
S
sushuang 已提交
246

P
pissang 已提交
247
export function getTextBoundingRect(opt: StyleProps): ReturnType<typeof textContain.getBoundingRect> {
248 249 250 251 252
    return textContain.getBoundingRect(
        opt.text,
        opt.font,
        opt.textAlign,
        opt.textVerticalAlign,
P
pissang 已提交
253
        opt.textPadding as number[],
254 255 256 257 258 259 260 261 262 263 264 265 266
        opt.textLineHeight,
        opt.rich,
        opt.truncate
    );
}

/**
 * @deprecated
 * the `textLineHeight` was added later.
 * For backward compatiblility, put it as the last parameter.
 * But deprecated this interface. Please use `getTextBoundingRect` instead.
 */
export function getTextRect(
P
pissang 已提交
267 268 269 270 271 272 273 274 275
    text: StyleProps['text'],
    font: StyleProps['font'],
    textAlign: StyleProps['textAlign'],
    textVerticalAlign: StyleProps['textVerticalAlign'],
    textPadding: StyleProps['textPadding'],
    rich: StyleProps['rich'],
    truncate: StyleProps['truncate'],
    textLineHeight: number
): ReturnType<typeof textContain.getBoundingRect> {
276
    return textContain.getBoundingRect(
P
pissang 已提交
277
        text, font, textAlign, textVerticalAlign, textPadding as number[], textLineHeight, rich, truncate
278 279
    );
}