在图表的创建中,使用format
属性将百分比数值格式化为需要的形式。例如,将负数部分格式化为括号括起来的形式,代码示例如下:
new TradingView.widget({
//...
priceScale: {
type: 'percentage',
format: {
pattern: '###.##%',
// 使用 formatter 函数来将负数部分格式化为括号括起来的形式
formatter: function (price) {
if (price < 0) {
return '(' + (-price).toFixed(2) + '%)';
} else {
return price.toFixed(2) + '%';
}
}
},
},
//...
})