我有以下代码可获取图表数据:

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”(利润):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))
}

有人知道我该如何检查吗?

最佳答案

我认为您可以在javascript中使用过滤器功能来实现此目的。

data=data.data.filter((d)=>{// as your data array is inside data object
return d.profit>=0;
})

现在,您将只有利润为正或为零的数据。

08-25 13:44