无法将数据转换为ng2-chart [条形图]

时间:2019-02-20 09:01:16

标签: javascript angular typescript angular7 ng2-charts

我是angular的新手,正在使用Angular 7,需要将api数据转换为ng2-charts格式。

以下是我的api数据:

{
    "status": 200,
    "message": "Fetched Successfully",
    "data": [
      {
        "1": [
          {
            "productid": "5c595f1c736429312f1ee15b",
            "totalQty": 5
          },
          {
            "productid": "5c595f1c736429312f1ee158",
            "totalQty": 54
          },
          {
            "productid": "5c595f1c736429312f1ee157",
            "totalQty": 156
          }
        ],
        "2": [
          {
            "productid": "5c595e3b736429312f1ee155",
            "totalQty": 15
          },
          {
            "productid": "5c595f1c736429312f1ee157",
            "totalQty": 42
          }
        ],
        "5": [
          {
            "productid": "5c595e3b736429312f1ee155",
            "totalQty": 50
          },
          {
            "productid": "5c595f1c736429312f1ee157",
            "totalQty": 70
          }
        ],
        "12": [
          {
            "productid": "5c595e3b736429312f1ee145",
            "totalQty": 17
          },
          {
            "productid": "5c595f1c736429312f1ee157",
            "totalQty": 5
          }
        ]
      }
    ]
  }

以下是我期望的输出格式:

  public barChartLabels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']; // all months 
  public barChartType = 'bar';
  public barChartLegend = true;

  public barChartData = [ 
    { data: [5], label: '5c595f1c736429312f1ee15b' },
    { data: [54], label: '5c595f1c736429312f1ee158' },
    { data: [156, 42, , , 70, , , , , , , 5], label: '5c595f1c736429312f1ee157' },
    { data: [, 15, , , 50], label: '5c595e3b736429312f1ee155' },
    { data: [, , , , , , , , , , , 17], label: '5c595e3b736429312f1ee145' },
    { data: [, 15], label: '5c595e3b736429312f1ee145' }
  ];

我想使图表具有动态性,因为api中会有动态数据。

我已经尝试过使用循环,但是它增加了复杂性,并且花费时间,因此寻找有效的解决方案。

预先感谢

1 个答案:

答案 0 :(得分:0)

您好希望此帮助:“ https://github.com/valor-software/ng2-charts/issues/662” 此问题已在上面的链接中关闭。