highcharts 处理API以不接收小于0的值(负值)

km0tfn4u  于 2022-11-10  发布在  Highcharts
关注(0)|答案(1)|浏览(163)

我需要处理API结果,这样我就不会收到小于0的值,我一直在尝试这种方法,但到目前为止,我还不能:

private handleChart(data: Object): void {
    const series = [];

    for (const [key, value] of Object.entries(data)) {
      series.push({
        name: key,
        data: value,
        type: "line",
      });
    }

    if (series[0].data.length < 0) {
      delete series[0].data;
    }
    console.log(data );  

    this.plotChart(series);
  }

原始代码:

private handleChart(data: Object): void {
    const series = [];

    for (const [key, value] of Object.entries(data)) {
      series.push({
        name: key,
        data: value,
        type: "line",
      });
    }

    this.plotChart(series);
  }
snz8szmq

snz8szmq1#

假设您的数据对象为:

const obj = {'jan':100,'feb':200, 'mar':-100,'apr':250};

您可以筛选和Map对象。条目的结果如下:

Object.entries(obj)
  .filter(arr=> +arr[1] >= 0)
  .map(arr=>{return{name:arr[0],data:+arr[1],type:'line'}});

结果将是:

[
 {"name": "jan","data": 100,"type": "line"},
 {"name": "feb","data": 200,"type": "line"},
 {"name": "apr","data": 250,"type": "line"}
]

或者,如果要将负值设为下限:

Object.entries(obj)
   .map(arr=>{return{name:arr[0],data:+arr[1] >= 0 ? +arr[1] : 0,type:'line'}});

结果:

[
 {"name": "jan","data": 100,"type": "line"},
 {"name": "feb","data": 200,"type": "line"},
 {"name": "mar","data": 0,"type": "line"},
 {"name": "apr","data": 250,"type": "line"}
]

相关问题