ngx-charts-line-chart failing with Cannot read property 'getColor' of undefined
Asked Answered
M

1

7

I'm setting up a simple example of the line chart and it fails with the getColor on undefined error. If I remove the [results] data, the chart displays but then fails on missing data. Not sure how to track this problem down?

Using ngx-charts 12.0.1 I copied the example code from https://github.com/marjan-georgiev/Angular-CLI-ngx-charts-Demo and then added a second chart to the home page shown in the example below.

I've tried removing as many variables as possible to get results.

Tag:

<div style="height: 100px">
  <ngx-charts-line-chart
    [view]="view"
    [results]="single"
    class="chart-container"
    [scheme]="colorScheme"
    [legend]="showLegend"
    [gradient]="gradient"
    [xAxis]="showXAxis"
    [yAxis]="showYAxis"
    [showXAxisLabel]="showXAxisLabel"
    [showYAxisLabel]="showYAxisLabel"
    [xAxisLabel]="xAxisLabel"
    [yAxisLabel]="yAxisLabel"
    (select)="onSelect($event)"
    (activate)="onActivate($event)"
    (deactivate)="onDeactivate($event)"
  >
  </ngx-charts-line-chart>
</div>

component data:
single: any[] = [
    {
      name: "Germany",
      value: 8940000
    },
    {
      name: "USA",
      value: 5000000
    },
    {
      name: "France",
      value: 7200000
    },
    {
      name: "Mars",
      value: 720000
    }
  ];


  colorScheme = {
    domain: ["#5AA454", "#A10A28", "#C7B42C", "#AAAAAA"]
  };

Console Error with [results]: 
    ERROR TypeError: Cannot read property 'getColor' of undefined
    at Object.eval [as updateDirectives] (LineSeriesComponent.html:14)


Console Error without [results]:
The chart is build on the page and, of course, empty. The following
error shows up when I click on any portion of the empty chart.

Error: <rect> attribute x: Expected length, "NaN".  
    platform-browser:1973
Minim answered 15/7, 2019 at 23:22 Comment(1)
any luck on this?Bascomb
A
0

Despite it complaining about the colors, I found the issue was with the way the series data was formatted when I came across it. I copied and pasted the data from this line chart demo and it seemed to sort itself out. If anyone in the future comes across a similar issue, validate the format of your data.

The correct format of the OP's data should be:

single: any[] = [
    {
      name: "Germany",
      series: [
         { 
           name: "1990",
           value: 8940000
         }
      ]
    },
    {
      name: "USA",
      series: [
         { 
           name: "1990",
           value: 5000000
         }
      ]
    },
    {
      name: "France",
      series: [
         { 
           name: "1990",
           value: 7200000
         }
      ]
    },
    {
      name: "Mars",
      series: [
         { 
           name: "1990",
           value: 720000
         }
      ]
    }
  ];
Associate answered 5/7 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.