【100分】求一段JS改法解决思路
【100分】求一段JS改法
Highcharts的highstock,在1.2.2版里关于提示气泡的格式化,我给它做了一下扩充,能显示百分比。
代码如下:
Highcharts的highstock,在1.2.2版里关于提示气泡的格式化,我给它做了一下扩充,能显示百分比。
代码如下:
tooltipFormatter: function (pointFormat) {
var point = this,
series = point.series,
seriesTooltipOptions = series.tooltipOptions,
match = pointFormat.match(/\{(series|point)\.[a-zA-Z]+\}/g),
dataSum = pointFormat.match(/\{(dataSum)\.[0-9]+\}/g),//dataSum 我自己添加的
splitter = /[{\.}]/,
obj,
key,
replacement,
repOptionKey,
parts,
prop,
ratio,//ratio我自己添加的
i,
cfg = { // docs: percentageDecimals, percentagePrefix, percentageSuffix, totalDecimals, totalPrefix, totalSuffix
y: 0, // 0: use 'value' for repOptionKey
open: 0,
high: 0,
low: 0,
close: 0,
percentage: 1, // 1: use the self name for repOptionKey
total: 1
};
// Backwards compatibility to y naming in early Highstock
seriesTooltipOptions.valuePrefix = seriesTooltipOptions.valuePrefix || seriesTooltipOptions.yPrefix;
seriesTooltipOptions.valueDecimals = seriesTooltipOptions.valueDecimals || seriesTooltipOptions.yDecimals;
seriesTooltipOptions.valueSuffix = seriesTooltipOptions.valueSuffix || seriesTooltipOptions.ySuffix;
// loop over the variables defined on the form {series.name}, {point.y} etc
for (i in match) {
key = match[i];
if (isString(key) && key !== pointFormat) { // IE matches more than just the variables
// Split it further into parts
parts = (' ' + key).split(splitter); // add empty string because IE and the rest handles it differently
obj = { 'point': point, 'series': series}[parts[1]];
/*原为prop = parts[2];下面是我改的*/
if (parts[2] === "ratio") {
ratio = parseFloat((" " + dataSum[0]).split(splitter)[2]);