UNPKG

1.94 kBPlain TextView Raw
1/**
2 * Copyright (c) 2017 ~ present NAVER Corp.
3 * billboard.js project is licensed under the MIT license
4 */
5import {isValue, isFunction, isObjectType} from "../../module/util";
6import {AxisType} from "../../../types/types";
7
8/**
9 * Get formatted
10 * @param {object} $$ Context
11 * @param {string} typeValue Axis type
12 * @param {number} v Value to be formatted
13 * @returns {number | string}
14 * @private
15 */
16function getFormat($$, typeValue: AxisType, v: number): number | string {
17 const {config} = $$;
18 const type = `axis_${typeValue}_tick_format`;
19 const format = config[type] ?
20 config[type] : $$.defaultValueFormat;
21
22 return format(v);
23}
24
25export default {
26 getYFormat(forArc: boolean): Function {
27 const $$ = this;
28 let {yFormat, y2Format} = $$;
29
30 if (forArc && !$$.hasType("gauge")) {
31 yFormat = $$.defaultArcValueFormat;
32 y2Format = $$.defaultArcValueFormat;
33 }
34
35 return function(v, ratio, id) {
36 const format = $$.axis && $$.axis.getId(id) === "y2" ?
37 y2Format : yFormat;
38
39 return format.call($$, v, ratio);
40 };
41 },
42
43 yFormat(v: number): number | string {
44 return getFormat(this, "y", v);
45 },
46
47 y2Format(v: number): number | string {
48 return getFormat(this, "y2", v);
49 },
50
51 defaultValueFormat(v): number | string {
52 return isValue(v) ? +v : "";
53 },
54
55 defaultArcValueFormat(v, ratio): string {
56 return `${(ratio * 100).toFixed(1)}%`;
57 },
58
59 dataLabelFormat(targetId: string): Function {
60 const $$ = this;
61 const dataLabels = $$.config.data_labels;
62 const defaultFormat = v => (isValue(v) ? +v : "");
63 let format = defaultFormat;
64
65 // find format according to axis id
66 if (isFunction(dataLabels.format)) {
67 format = dataLabels.format;
68 } else if (isObjectType(dataLabels.format)) {
69 if (dataLabels.format[targetId]) {
70 format = dataLabels.format[targetId] === true ?
71 defaultFormat : dataLabels.format[targetId];
72 } else {
73 format = () => "";
74 }
75 }
76
77 return format.bind($$.api);
78 }
79};