question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ng-nvd3 chart is not updating with data change

See original GitHub issue

I am using a ng2-nvd3 in project and unable to update my chart new data is changed on runtime. only legends are updated. Any help would be appreciated.

I am creating a method and asssigning it to a button: getStats(): any{ this.data.push({“values”:[{“x”:1000,“y”:1000}], “key”: “runtime added value”});
this.nvD3.chart.update(); }

Code in my component: `import { Component, OnInit, AfterViewInit, ViewChild } from ‘@angular/core’;

import {nvD3} from ‘ng2-nvd3’; import * as d3 from ‘d3’; //declare let d3: any;

@Component({ selector: ‘d3graph’, templateUrl:‘./d3graph.component.html’ }) export class D3graphComponent implements OnInit { chart: any; config:any; api: any; options; data; chartType; @ViewChild(nvD3) nvD3: nvD3; constructor() { }

ngOnInit() { this.options = { chart: { type: ‘lineChart’, height: 450, margin : { top: 20, right: 20, bottom: 60, left: 65 }, x: function(d){ return d.x; }, y: function(d){ return d.y; }, callback: function(chart){ console.log(“!!! lineChart callback !!!”);},

      color: d3.scale.category10().range(),
      //useInteractiveGuideline: true,
      //clipVoronoi: false,
      xAxis: {
          axisLabel: 'X Axis'
          //ticks: 10
          // tickFormat: function(d) {
          //     return d3.time.format('%m/%d/%y')(new Date(d))
          // },
          // ticks: 10
      },

      yAxis: {
          axisLabel: 'Y Axis',
          tickFormat: function(d){
              return d3.format(",d")(d);
          },
          axisLabelDistance: 0
      },

      interactiveLayer:{
        tooltip:{
          headerEnabled: true,
          headerFormatter: function (key) {
            return "Project name";
          }
          
        }
      },
      dispatch: {
        stateChange: function(e){ console.log('stateChange') },
        changeState: function(e){ console.log('changeState') },
        
        renderEnd: function(e){ console.log('renderEnd');  }
    }
      
    }
};

this.data = [
  {
    "values": [{
        "x": 0,
        "y": 0
    }, {
        "x": 173,
        "y": 100
    }, {
        "x": 347,
        "y": 600
    }, {
        "x": 526,
        "y": 9
    }, {
        "x": 700,
        "y": 40
    }, {
        "x": 931,
        "y": 60
    }, {
        "x": 1058,
        "y": 100
    }],
    "key": "Project 656"
  },  
{
    "values": [{
        "x": 0,
        "y": 341
    }, {
        "x": 173,
        "y": 341
    }, {
        "x": 347,
        "y": 341
    }, {
        "x": 526,
        "y": 341
    }, {
        "x": 700,
        "y": 341
    }, {
        "x": 931,
        "y": 341
    }, {
        "x": 1058,
        "y": 341
    }],
    "key": "Project Hello"
}, {
    "values": [{
        "x": 0,
        "y": 560
    }, {
        "x": 173,
        "y": 100
    }, {
        "x": 347,
        "y": 341
    }, {
        "x": 526,
        "y": 170
    }, {
        "x": 700,
        "y": 370
    }, {
        "x": 931,
        "y": 330
    }, {
        "x": 1058,
        "y": 250
    }],
    "key": "Project 333"
}, {
    "values": [{
        "x": 0,
        "y": 500
    }, {
        "x": 173,
        "y": 230
    }, {
        "x": 347,
        "y": 400
    }, {
        "x": 526,
        "y": 120
    }, {
        "x": 700,
        "y": 190
    }, {
        "x": 931,
        "y": 146
    }],
    "key": "Project sds"
}];

}

getStats(): any{ this.data.push({“values”:[{“x”:1000,“y”:1000}], “key”: “runtime added value”});
this.nvD3.chart.update(); }

} `

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:12

github_iconTop GitHub Comments

7reactions
Swatantramishra1commented, Aug 7, 2017

Hi @ritsWeb, here are solutions. in html file ---- <nvd3 #nvd3 [options]=“options2” [data]=“data2”></nvd3>

in component.ts — @ViewChild(‘nvd3’) nvd3; And wherever you want to update this chart just right . this.nvd3.chart.update(); it will work for sure

Thanks

2reactions
Prashanth-Thomascommented, Oct 24, 2017

@Swatantramishra1 thanks for your quick reply 😃 . This would work fine if it was AngularJS. but i am using Angular4, we do not use $scope in Angular2/4.

we can only gain access to the element using // in the html component <nvd3 #nvd3 [options]=“options” [data]=“data” api=“api”>

// in the TS file within the class @ViewChild(‘nvd3’) nvd3;

// to update new data array, also within TS file this.nvd3.chart.update();

So i am assuming my way to update both options and data is this.nvd3.api.updateWithOptions(); tried it, but it threw an error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NVD3 tooltip pointer not updating position after updating chart ...
When I update the data I do this: chartData.datum(data).call(chart);. Everything updates fine except the position of the pointers of the tooltip ...
Read more >
How to work with D3.js's general update pattern
Observe how the graph responds to user interactions, and changes in data or state. This article aims to equip you with the fundamental...
Read more >
Chart.update does not update the chart-d3.js - appsloveworld
[Solved]-Nvd3js bullet chart : Chart.update does not update the chart-d3.js ... score:0. I faced the same problem today and it is a bug...
Read more >
Simple Line Chart - NVD3
Tooltip which shows all visible series data. Line transitions that happen when turning on/off series, ... //Update the chart when window resizes. nv.utils....
Read more >
Angular-nvD3 Quick start - GitHub Pages
Directive watches for data changing and automatically updates the chart. Note that in this case the angular $watch procedure is used for tracking...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found