我有一个获取图表数据的代码:
function createData(data) {
if ($("#options").val() == 1) {
var totalTopProfit = eval(data.data[0].profit.replace(/[^\d\.]/g, '') + '+' + data.data[1].profit.replace(/[^\d\.]/g, '') + '+' + data.data[2].profit.replace(/[^\d\.]/g, '') + '+' + data.data[3].profit.replace(/[^\d\.]/g, '')).toFixed(2);
var totalProfit = eval(data.others.replace(/[^\d\.]/g, ''));
var othersProfit = parseFloat(totalProfit) - totalTopProfit;
createLegend(data, othersProfit, 1);
var data = [{
"company": data.data[0].company,
"profit": data.data[0].profit,
"percentage": parseFloat(eval((data.data[0].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[1].company,
"profit": data.data[1].profit,
"percentage": parseFloat(eval((data.data[1].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[2].company,
"profit": data.data[2].profit,
"percentage": parseFloat(eval((data.data[2].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[3].company,
"profit": data.data[3].profit,
"percentage": parseFloat(eval((data.data[3].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": "Others",
"profit": numberWithCommas(othersProfit.toFixed(2)),
"percentage": parseFloat(eval((othersProfit / totalProfit) * 100).toFixed(2))
}];
} else if ($("#options").val() == 2) {
var totalTopProfit = eval(data.data[0].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[1].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[2].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[3].revenue.replace(/[^\d\.]/g, '')).toFixed(2);
var totalProfit = eval(data.others.replace(/[^\d\.]/g, ''));
var othersProfit = parseFloat(totalProfit) - totalTopProfit;
createLegend(data, othersProfit, 2);
var data = [{
"company": data.data[0].company,
"profit": data.data[0].revenue,
"percentage": parseFloat(eval((data.data[0].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[1].company,
"profit": data.data[1].revenue,
"percentage": parseFloat(eval((data.data[1].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[2].company,
"profit": data.data[2].revenue,
"percentage": parseFloat(eval((data.data[2].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[3].company,
"profit": data.data[3].revenue,
"percentage": parseFloat(eval((data.data[3].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": "Others",
"profit": numberWithCommas(othersProfit.toFixed(2)),
"percentage": parseFloat(eval((othersProfit / totalProfit) * 100).toFixed(2))
}];
} else if ($("#options").val() == 3) {
var totalTopProfit = eval(data.data[0].profit.replace(/[^\d\.]/g, '') + '+' + data.data[1].profit.replace(/[^\d\.]/g, '') + '+' + data.data[2].profit.replace(/[^\d\.]/g, '') + '+' + data.data[3].profit.replace(/[^\d\.]/g, '')).toFixed(2);
var totalProfit = eval(data.others.replace(/[^\d\.]/g, ''));
var othersProfit = parseFloat(totalProfit) - totalTopProfit;
createLegend(data, othersProfit, 3);
var data = [{
"company": data.data[0].offer,
"profit": data.data[0].profit,
"percentage": parseFloat(eval((data.data[0].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[1].offer,
"profit": data.data[1].profit,
"percentage": parseFloat(eval((data.data[1].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[2].offer,
"profit": data.data[2].profit,
"percentage": parseFloat(eval((data.data[2].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[3].offer,
"profit": data.data[3].profit,
"percentage": parseFloat(eval((data.data[3].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": "Others",
"profit": numberWithCommas(othersProfit.toFixed(2)),
"percentage": parseFloat(eval((othersProfit / totalProfit) * 100).toFixed(2))
}];
} else if ($("#options").val() == 4) {
var totalTopProfit = eval(data.data[0].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[1].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[2].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[3].revenue.replace(/[^\d\.]/g, '')).toFixed(2);
var totalProfit = eval(data.others.replace(/[^\d\.]/g, ''));
var othersProfit = parseFloat(totalProfit) - totalTopProfit;
createLegend(data, othersProfit, 4);
var data = [{
"company": data.data[0].offer,
"profit": data.data[0].revenue,
"percentage": parseFloat(eval((data.data[0].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[1].offer,
"profit": data.data[1].revenue,
"percentage": parseFloat(eval((data.data[1].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[2].offer,
"profit": data.data[2].revenue,
"percentage": parseFloat(eval((data.data[2].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[3].offer,
"profit": data.data[3].revenue,
"percentage": parseFloat(eval((data.data[3].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": "Others",
"profit": numberWithCommas(othersProfit.toFixed(2)),
"percentage": parseFloat(eval((othersProfit / totalProfit) * 100).toFixed(2))
}];
} else if ($("#options").val() == 5) {
var totalTopProfit = eval(data.data[0].profit.replace(/[^\d\.]/g, '') + '+' + data.data[1].profit.replace(/[^\d\.]/g, '') + '+' + data.data[2].profit.replace(/[^\d\.]/g, '') + '+' + data.data[3].profit.replace(/[^\d\.]/g, '')).toFixed(2);
var totalProfit = eval(data.others.replace(/[^\d\.]/g, ''));
var othersProfit = parseFloat(totalProfit) - totalTopProfit;
createLegend(data, othersProfit, 5);
var data = [{
"company": data.data[0].advertiser,
"profit": data.data[0].profit,
"percentage": parseFloat(eval((data.data[0].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[1].advertiser,
"profit": data.data[1].profit,
"percentage": parseFloat(eval((data.data[1].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[2].advertiser,
"profit": data.data[2].profit,
"percentage": parseFloat(eval((data.data[2].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[3].advertiser,
"profit": data.data[3].profit,
"percentage": parseFloat(eval((data.data[3].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": "Others",
"profit": numberWithCommas(othersProfit.toFixed(2)),
"percentage": parseFloat(eval((othersProfit / totalProfit) * 100).toFixed(2))
}];
} else if ($("#options").val() == 6) {
var totalTopProfit = eval(data.data[0].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[1].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[2].revenue.replace(/[^\d\.]/g, '') + '+' + data.data[3].revenue.replace(/[^\d\.]/g, '')).toFixed(2);
var totalProfit = eval(data.others.replace(/[^\d\.]/g, ''));
var othersProfit = parseFloat(totalProfit) - totalTopProfit;
createLegend(data, othersProfit, 6);
var data = [{
"company": data.data[0].advertiser,
"profit": data.data[0].revenue,
"percentage": parseFloat(eval((data.data[0].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[1].advertiser,
"profit": data.data[1].revenue,
"percentage": parseFloat(eval((data.data[1].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[2].advertiser,
"profit": data.data[2].revenue,
"percentage": parseFloat(eval((data.data[2].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": data.data[3].advertiser,
"profit": data.data[3].revenue,
"percentage": parseFloat(eval((data.data[3].revenue.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}, {
"company": "Others",
"profit": numberWithCommas(othersProfit.toFixed(2)),
"percentage": parseFloat(eval((othersProfit / totalProfit) * 100).toFixed(2))
}];
}
return data;
}
问题在于当利润值为负数时,会破坏图表。现在我需要检查“profit”(“profit”:data.data [0] .profit)是否为负数,如果是,那么我需要删除所有这些具有负值利润的数据,并且只返回具有正利润的数据。
{
"company": data.data[0].company,
"profit": data.data[0].profit,
"percentage": parseFloat(eval((data.data[0].profit.replace(/[^\d\.]/g, '') / totalProfit) * 100).toFixed(2))
}
有谁知道我该如何检查这个问题吗?
data
被初始化之前调用了createLegend(data, ...)
。 - Andreas