Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Changing chart type is broken #59

Open
Sivart-me opened this issue Jan 2, 2017 · 2 comments
Open

Changing chart type is broken #59

Sivart-me opened this issue Jan 2, 2017 · 2 comments

Comments

@Sivart-me
Copy link

I'm trying to implement the ability for the user to choose between alternative chart types, whilst using the same underlying data. However when the chart changes type it is rendered overlaid on the original chart as seen in this screenshot:

overlaidcharts

For the purpose of demonstrating the issue, I've modified the piechart example from the repository to switch between the discreteBarChart and the original pieChart type as follows (note that for whatever reason the button appears to require being clicked 3 times before the chart changes):

;(function(global){

var pieData = [
  {key: "One", y: 5, color: "#5F5"},
  {key: "Two", y: 2},
  {key: "Three", y: 9},
  {key: "Four", y: 7},
  {key: "Five", y: 4},
  {key: "Six", y: 3},
  {key: "Seven", y: 0.5}
];

var barData = [
  {key: "Cumulative Return", values: pieData}
]

var PieWrapper = React.createClass({
  getInitialState: function() {
    return {pie: false}
  },
  handleClick: function() {
    this.setState({pie: !this.state.pie})
  },
  render: function() {
    const data = (this.state.pie)? pieData: barData;
    const type = (this.state.pie)? "pieChart": "discreteBarChart";
    return (
      <div>
        <button onClick={this.handleClick}>Change Chart</button>
        <NVD3Chart
          id="chart"
          width="600"
          height="370"
          type={type}
          datum={data}
          x="key"
          y="y"
          renderEnd={function(chart, e){console.log( chart.id(), e)}}
          renderStart={function(chart, e){console.log( chart.id(), e)}}
          ready={function(chart, e){console.log( chart.id(), e)}}
        />
      </div>
    )
  }
})

ReactDOM.render(
  <PieWrapper name="wrapper" />,
  document.getElementById('pieChart')
);

})(window);
@starsinmypockets
Copy link

I'm guessing that if you are re-rendering the chart as a different type, then you need to unmount the old chart first:

http://stackoverflow.com/questions/21662153/unmounting-react-js-node

@Sivart-me
Copy link
Author

Ok I'll try this when I get a chance and post back my findings.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants