我有这个代码来获取图表的数据:
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;
}
问题是当利润有负值时,它会打破图表。现在我需要检查是"利润" ("利润":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))
}
有谁知道我怎么检查这个?
答案 0 :(得分:1)
我认为你可以在javascript中使用过滤功能来实现这一点。
data=data.data.filter((d)=>{// as your data array is inside data object
return d.profit>=0;
})
现在你将只有正数或零利润的数据。