2

how to create chart taking input from the user from a form in reactjs

I made a form and pie chart not getting an idea of how to access data from a form

chartData:{
        datasets:[{
              data:[10,20,30,40,50]
                  }]
           }    

i want the chart to be created by the inputs form the form

3
  • which liberary you are using for chart Commented Jul 16, 2019 at 11:10
  • react-chartjs-2 Commented Jul 16, 2019 at 11:12
  • I had added the code use it will work for you Commented Jul 16, 2019 at 11:58

3 Answers 3

1
export default class App extends Component {
    state = {
        dataPie: {
            datasets: [{
                data: [10, 20, 30]
            }],

        },
        first: "",
        second: "",
    }
    handleSubmit = () => {
        const { dataPie } = this.state;
        this.setState({
            dataPie: {
                ...dataPie,
                datasets: [{
                    ...dataPie.datasets,
                    data: [...dataPie.datasets[0].data,
                    this.state.first, this.state.second]
                }]
            }
        })
    }
    handleChange = (evt) => {
        let a = parseInt(evt.target.value)
        this.setState({
            [evt.target.name]: a,
        })
    }
    render() {


        const { data, dataPie } = this.state;
        console.log(dataPie);
        return (
            <React.Fragment>
                <input type="number"
                    value={this.state.first}
                    name="first"
                    onChange={(evt) => this.handleChange(evt)} />
                <input type="number"
                    value={this.state.second}
                    name="second" onChange={(evt) => this.handleChange(evt)} />
                <button onClick={() => this.handleSubmit()}>add data to chart</button>
                <h1>pie chart</h1>
                <Pie data={dataPie} />
            </React.Fragment>

        )
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried to do polar chart using this codes it is working properly with few changes but now I want the circles in polar chart should be 10 each containing value 10. the input entered for one slice should be in one color the remaining should be of other color.eg: if my input value of 1 slice is 100 out of 10 the remaining 90 of that single slice should be of another color this should be done for my 5 inputs
I want something similar to this given link miro.medium.com/max/982/1*7q-Pe76n2lvvIfqWmlyiNg.jpeg
how to add two data field to a single label but one should remain default 10 and other should change according to my input given through input field
for a polar chart using reactjs
1

Use the following code will generate a dynamic chart

import React, { Component } from "react";
import {  Pie } from "react-chartjs-2";


export default class App extends Component {
    state = {
        dataPie: {
            datasets: [{
                data: [10, 20, 30]
            }],

        },
        first: "",
        second: "",
    }
    handleSubmit = () => {
        const { dataPie } = this.state;
        this.setState({
            dataPie: {
                ...dataPie,
                datasets: [{
                    ...dataPie.datasets,
                    data: [
                    this.state.first,
                     this.state.second
                       ]
                }]
            }
        })
    }
    handleChange = (evt) => {
        let a = parseInt(evt.target.value)
        this.setState({
            [evt.target.name]: a,
        })
    }
    render() {


        const { data, dataPie } = this.state;
        console.log(dataPie);
        return (
            <React.Fragment>
                <input type="number"
                    value={this.state.first}
                    name="first"
                    onChange={(evt) => this.handleChange(evt)} />
                <input type="number"
                    value={this.state.second}
                    name="second" onChange={(evt) => this.handleChange(evt)} />
                <button onClick={() => this.handleSubmit()}>add data to chart</button>
                <h1>pie chart</h1>
                <Pie data={dataPie} />
            </React.Fragment>

        )
    }
}

13 Comments

I don't want a static value. In this (data:[10,20,30,40,50],) i want the value which i enter through input fields
@Divya I have added the code that dynamically update the chart using inputs
Thank you so much for your help. Please keep on guiding us
if this code help you then please accept it as answer
Thank u so much @Sanaullah.U gave me a very big solution to my problem.Thank u once again
|
0

data: [...dataPie.datasets[0].data, this.state.first, this.state.second] }]change this to data: [ this.state.first, this.state.second] }]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.