Olical / react-faux-dom

DOM like structure that renders to React (unmaintained, archived)
http://oli.me.uk/2015/09/09/d3-within-react-the-right-way/
The Unlicense
1.21k stars 87 forks source link

getContext("2d") with reactFauxDom #86

Closed ZakariaHili closed 7 years ago

ZakariaHili commented 7 years ago

Hi, I want to use ReadtFauxDom with chartJS, but the chartJS doesn't render anything(FauxDom is empty), I think this pb is due to the fact that ReactFauxDOM.element('canvas').getContext('2d') doesn't existe (I am not sure)

What do you think ?

here the code:


var Chart = require('chart.js');
class BarReact extends React.Component {
    constructor() {
        super()
    }
    componentWillMount() {
        var ctx = new ReactFauxDOM.Element('canvas');

        var myChart = new Chart(ctx, {

            type: 'bar',
            data: {
                labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
                datasets: [{
                    label: '# of Votes',
                    data: [12, 19, 3, 5, 2, 3],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    ],
                    borderColor: [
                        'rgba(255,99,132,1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
                        }
                    }]
                }
            }
        });
        this.setState({ chart: ctx })
    }

    render() {

        return (

                        {this.state.chart.toReact()}

        )
    }
}

NB: I'm using
"chart.js": "^2.5.0", "react-faux-dom": "^3.0.1"

Olical commented 7 years ago

If what you're working with is using a canvas, I'm afraid it won't work. There is no support for that in this library, it's just a lightweight DOM shim really. Sorry about that!

ZakariaHili commented 7 years ago

Thank you for your reply, but I found another solution without react faux dom, Here's the solution for person who faced the same issue (To avoid asking the same question)


import React from 'react';
import Chart from 'chart.js';
import ReactDOM from 'react-dom';

class ChartComponent extends React.Component {
    componentDidMount() {
        this.initializeChart(this.props.config);
    }
    initializeChart(options) {
        let el = ReactDOM.findDOMNode(this.refs.chart);
        let ctx = el.getContext("2d");
        let chart = new Chart(ctx, options);
    }
    render() {
        return (<canvas ref="chart" height="400px" />);
    }
}

```export default ChartComponent;