benpickles / peity

Progressive <svg> pie, donut, bar and line charts

Home Page:http://benpickles.github.io/peity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React.js ?

vko-online opened this issue · comments

Any react port?

I do actually have an unpushed branch building support for a React-based version. I can let you know when there's something more to look at.

Cool
Please
Thanks

@benpickles maybe just push the WIP and let me do some polishing? Created a little proprietary component that approximates peity but would rather just contribute upstream.

Is there still no way of using this with react?

There is an attempt to port to react here https://www.npmjs.com/package/react-peity

react-peity is limited to Line and Bar charts. For our new React project we needed Pie Peity, so created a simple Component. Hope it will be helpful for someone.

p.s. Including jQuery globally is required.

/**
 * External Dependencies
 */
import 'peity';
import React, { Component } from 'react';

const $ = window.jQuery;

/**
 * Component
 */
class Peity extends Component {
    constructor( props ) {
        super( props );

        this.$chartRef = React.createRef();
        this.$chart = false;
    }

    componentDidMount() {
        const {
            type = 'line',
            options = {},
        } = this.props;

        this.$chart = $( this.$chartRef.current ).peity( type, options );
    }

    componentDidUpdate( prevProps ) {
        // nothing changed
        if ( JSON.stringify( prevProps.data ) === JSON.stringify( this.props.data ) ) {
            return false;
        }

        if ( this.$chart ) {
            this.$chart
                .change();
        }
    }

    render() {
        const {
            data = '',
        } = this.props;

        return (
            <span
                ref={ this.$chartRef }
                className="peity"
            >
                { data.join( ',' ) }
            </span>
        );
    }
}

export default Peity;

Usage example:

<Peity
    type="line"
    data={ [ 5, 3, 9, 6, 5, 9, 7, 3, 5, 2 ] }
    options={ {
        height: 16,
        strokeWidth: 0,
        fill: 'rgba(114, 94, 195, 0.8)',
    } }
/>

<Peity
    type="pie"
    data={ [ 5, 3, 4 ] }
    options={ {
        height: 16,
        strokeWidth: 0,
        fill: [
            'rgba(114, 94, 195, 0.8)',
            'rgba(114, 94, 195, 0.6)',
            'rgba(114, 94, 195, 0.4)',
        ],
    } }
/>

I ended up creating port for react-native https://github.com/vko-online/react-native-peity