4

Console logCannot read property 'toLocaleString' of undefinedon my component.ts i am using this method to take the data from json

getChartProducts() {

        this.TicketService.getAlladminPage(this.number, this.size, this.sort, this.orderByColumn)
          .subscribe(
            (chart: any[]) => {

              let item = 0;

              if (chart['content']) {
                while(item < chart['content'].length){

                  let chartItem = {
                    'name' : chart['content'][item].name,
                    'price': chart['content'][item].price
                  };

                  this.chart.push(chartItem);
                  item ++;
                }

                console.log( this.chart);
              }
            });

      }`

i also set

`
     chart: { name :string, price :number}[] = [];
view: any[] = [700, 400];

  // options
  showXAxis = true;
  showYAxis = true;
  gradient = false;
  showLegend = true;
  showXAxisLabel = true;
  showYAxisLabel = true;`

and my HTML

` <ngx-charts-bar-vertical
                                [view]="view"
                                [scheme]="colorScheme"
                                [results]="chart"
                                [gradient]="gradient"
                                [xAxis]="showXAxis"
                                [yAxis]="showYAxis"
                                [legend]="showLegend"
                                [showXAxisLabel]="showXAxisLabel"
                                [showYAxisLabel]="showYAxisLabel"
                                [xAxisLabel]="xAxisLabel"
                                [yAxisLabel]="yAxisLabel"
                                (select)="onSelect($event)">
                              </ngx-charts-bar-vertical>`

Any idea for this error? i tried chart: { "name":string, "price" :number}[] = []; but again the same error

I am using Angular CLI: 1.7.4 Node: 8.11.2 OS: win32 x64 Angular: 5.0.2 "@swimlane/ngx-charts": "7.4.0",

Tzimpo
  • 964
  • 1
  • 9
  • 24
  • Where is `toLocaleString` being invoked? – user184994 Jul 08 '18 at 09:29
  • @user184994 what do you mean? – Tzimpo Jul 08 '18 at 09:44
  • Your error says `Cannot read property 'toLocaleString' of undefined`, which means somewhere the code is trying to call `toLocaleString`. In that error, there is probably whats called a stack-trace, which will show where the error occured. Can you add the whole error to your question? – user184994 Jul 08 '18 at 09:47
  • @user184994 i uploaded an image with the error – Tzimpo Jul 08 '18 at 09:54
  • So it looks like, in your SeriesVerticalComponent, you have a function called `update`. Can you add that code (and any relevant code that it calls into) please? – user184994 Jul 08 '18 at 09:55
  • @user184994 this component is from the ngx-charts library. From series-vertical-component.js : SeriesVerticalComponent.prototype.ngOnChanges = function (changes) { this.update(); }; – Tzimpo Jul 08 '18 at 10:03
  • It's too big to post it here – Tzimpo Jul 08 '18 at 10:05
  • Can you `console.log` the value you're passing in for the data (i.e. your `chart` value) please? It looks like you may have an undefined value in there – user184994 Jul 08 '18 at 10:09
  • @user184994 i add an image on post console log – Tzimpo Jul 08 '18 at 10:12
  • It looks like `ngx-charts` is expecting each entry to have a property called `value`, but your data doesn't. It only has a property called `price` – user184994 Jul 08 '18 at 10:14
  • @user184994 That's it!!! Thank you mate!! – Tzimpo Jul 08 '18 at 10:17

3 Answers3

10

For a Grouped Vertical Bar Chart, you need to use:

ngx-charts-bar-vertical-2d

Rather than:

ngx-charts-bar-vertical
1

Your data needs to have a property called value, but your data only has a property called price.

Just update your data to include a value property, and it should work.

user184994
  • 17,791
  • 1
  • 46
  • 52
0
the above error still occurs for charts such as Grouped Vertical Bar Chart, Grouped Horizontal Bar Chart etc this looks like a bug from ngx-charts
the input for this is 

[
  {
    "name": "Germany",
    "series": [
      {
        "name": "2010",
        "value": 7300000
      },
      {
        "name": "2011",
        "value": 8940000
      }
    ]
  },
​
  {
    "name": "USA",
    "series": [
      {
        "name": "2010",
        "value": 7870000
      },
      {
        "name": "2011",
        "value": 8270000
      }
    ]
  }
]

they expect value in the outer array, but if we send value their, there is no meaning

i.e [
{
"name": "Germany",
"value": 234324,
"series": [
]
},
...
]
Bhimashankar Mantur
  • 189
  • 1
  • 3
  • 12