Code Monkey home page Code Monkey logo

Comments (8)

EmirGluhbegovic avatar EmirGluhbegovic commented on May 5, 2024 1

Child Component:
Base64 string works

class Child extends React.Component {
  componentDidMount() {
    var pieChart = this.refs.chart.chart_instance.toBase64Image();
    console.log(pieChart);
  }

  render() {
    return(
      <Pie ref='chart' data={data} options={options} />
    )
  }
}

Parent Component
Cannot access ._chart_instance

class Parent extends React.Component {
  componentDidMount() {
    var pieChart = this.refs.chart.chart_instance.toBase64Image();
    console.log(pieChart);
  }
  render() {
    return(
    <Child ref='chart' data={data} options={options} />
    )
  }
}

from react-chartjs-2.

gor181 avatar gor181 commented on May 5, 2024

Hey @EmirGluhbegovic ,

Can you make a fiddle or similar, would really help to narrow it πŸ‘

thanks

from react-chartjs-2.

EmirGluhbegovic avatar EmirGluhbegovic commented on May 5, 2024

I have a feeling there is a way to refer to the chart_instance in the parent that I am not aware off possibly?

from react-chartjs-2.

gor181 avatar gor181 commented on May 5, 2024

So you have a deep structure. What I can see is that you get the reference in the Child componentDidMount.

Try to make the Child as:

class Child extends React.Component {
  componentDidMount() {
    this.chart_instance = this.refs.chart.chart_instance;
  }

  render() {
    return(
      <Pie ref='chart' data={data} options={options} />
    )
  }
}

And the Parent as:

class Parent extends React.Component {
  componentDidMount() {
    var pieChart = this.refs.chart.chart_instance.toBase64Image();
    console.log(pieChart);
  }
  render() {
    return(
    <Child ref='chart' data={data} options={options} />
    )
  }
}

Let me know how it goes.

from react-chartjs-2.

EmirGluhbegovic avatar EmirGluhbegovic commented on May 5, 2024

@gor181 Tried exactly as you have said, getting an error
this.refs.lineChart.toBase64Image is not a function.
I made sure I have
import ChartsJS from 'react-chartjs-2';
in the parent also. Unless this.chart is still not providing access to the chart_instance ?

from react-chartjs-2.

gor181 avatar gor181 commented on May 5, 2024

@EmirGluhbegovic Yes I did this blindly, please check corrected example.

from react-chartjs-2.

EmirGluhbegovic avatar EmirGluhbegovic commented on May 5, 2024

@gor181 Thanks Bro, that works perfectly now. Guess its my lack of React there so not a react-chartjs-2 issue. Thanks for the help again.

from react-chartjs-2.

gor181 avatar gor181 commented on May 5, 2024

No problem, glad it's working. It's not your react, we people tend to overcomplicate sometimes πŸ‘ 🍺

from react-chartjs-2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.