score:0
data: [...dataPie.datasets[0].data, this.state.first, this.state.second] }]change this to
data: [ this.state.first, this.state.second] }]
score: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>
)
}
}
score: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>
)
}
}
Source: stackoverflow.com
Related Query
- user input to create pie chart using reactjs from form
- How to debounce user input in reactjs using rxjs
- Using an input field with onBlur and a value from state blocks input in Reactjs JSX?
- reactjs how to input multiple file from form
- create a mobile app from my web site using reactjs (not native)
- ReactJS: using alert() to take input from user
- Create Chart using Reactjs Chartjs and axios
- How to prevent user from entering spaces in an input form field in ReactJS?
- How can I create a button which Fetches image from API and download to the local with using ReactJs
- how can i setState inside of array of objects taking user input from form
- Unable to limit or change user Input when using setFieldsValue in ant Design Form
- How to make aspecific API call from within a Line Chart class in ReactJS using react-chartjs-2?
- Create Pie Chart Using JSON Data React
- ReactJS - Custom hook to create a single object with the data collected from multiple form components
- Change state object variable from form input - ReactJS
- How to ignore "req.file.path" from form data if user do not choose a file using multer
- How can i restrict user from not selecting other file types using input type file with react and typescript?
- Create user authentication using reactjs and firebase
- plotting pie chart using reactJs with chartjs
- How to create a Dynamic html form in reactjs using json data?
- Reactjs Form Validations-for input type number the text field in constantly showing single value even after giving backspace from the keyboard
- take a csv file as input from user using React and send it as a post request using axios
- What's the best way to create a hamburger menu from scratch in Reactjs without using Hooks?
- Typescript error: Create Alpha from RGBA as an Input with type number, not a string while using React JS?
- How to store, submit and copy input from form to another component in ReactJS
- How to dynamically show data in a pie chart using Reactjs and D3js
- React form using data from previous input
- How to send data from a form data to a database using ReactJS , Node.js and MySQL queries?
- How can I fetch data from API url into my reactgoogle pie chart in reactjs
- Changing the theme color value from user input on button click using ThemeProvider
More Query from same tag
- With React Router : How to render a component multiple times with differents props each time in a Route?
- React useState setter in promise not updating
- How can I make a chart through JSONfile from highchats? please help me
- I am trying to install new component using npm. But it gives error
- How to render an Image in react which is coming from Nodejs backend as res.json()?
- Cannot deploy to with gh-pages with specific project due to error: cannot spawn sh: No such file or directory
- Why I am getting this error message when I run react js project first time in my system?
- I can't find the matching TypeScript type for `displayName` as a prop of `type`
- Saga watcher not receiving or processing dispatched action
- Sulu CMF - Filtered Single autocomplete selection for associated entity
- React-Bootstrap Form.Label opens file explorer when clicked on
- React Spring useSpring hook switching between from state and to state without animation
- Authenticated login for google enterprise account users
- What is meant by "one level deep"?
- Same component used in multiple routes is being remounted react-router
- Make React Component available as a Widget
- How to add authentication using JWT on a React app?
- Webpack import bootstrap .js and .css
- Unable to install from forked github repo using npm
- e.preventDefault() does not prevent page reload in React
- syntax error in Typescript when adding onTouchTap to div
- How to assign a function to ref using useRef with typescript?
- Collection on client not changing on Meteor.subscribe with React
- Next.js Fetch and Maintain global data for all components
- Open "choose file" dialog box on onclick of Raised Button Material ui
- React component does not react to mobx observable data
- How can I animate when rendering and unmount a component in React?
- Trying to redirect to component in react
- How to migrate api call wrapper from a redux thunk to redux saga
- How do I add HubSpot chat to react js app?