mirror of
https://github.com/aleleba/chartjs-2-react.git
synced 2025-07-03 04:08:18 -06:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
60cae61ef1 | |||
fe3bf8702e | |||
77ed20aa45 |
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
> Libreria de chart.js para React.js
|
> Libreria de chart.js para React.js
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/chart-2-react) [](https://standardjs.com)
|
[](https://www.npmjs.com/package/chartjs-2-react) [](https://standardjs.com)
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
1007
example/package-lock.json
generated
1007
example/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -5,11 +5,11 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"chartjs-2-react": "file:..",
|
||||||
"prop-types": "^15.6.2",
|
"prop-types": "^15.6.2",
|
||||||
"react": "^16.4.1",
|
"react": "^16.4.1",
|
||||||
"react-dom": "^16.4.1",
|
"react-dom": "^16.4.1",
|
||||||
"react-scripts": "^1.1.4",
|
"react-scripts": "^1.1.4"
|
||||||
"chartjs-2-react": "file:.."
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start",
|
"start": "react-scripts start",
|
||||||
|
@ -1,18 +1,31 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react';
|
||||||
|
|
||||||
import { RadialGauge } from 'chartjs-2-react'
|
import { RadialGauge, Bar, Line } from 'chartjs-2-react';
|
||||||
|
|
||||||
export default class App extends Component {
|
export default class App extends Component {
|
||||||
render () {
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
|
|
||||||
<div style={{maxWidth: '600px', marginTop: '25px'}}>
|
render () {
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{paddingBottom: '100px'}}>
|
||||||
|
|
||||||
|
<div style={{maxWidth: '600px', marginTop: '25px', margin: 'auto'}}>
|
||||||
<h3 style={{textAlign: 'center'}}>RadialGauge Component:</h3>
|
<h3 style={{textAlign: 'center'}}>RadialGauge Component:</h3>
|
||||||
<RadialGauge />
|
<RadialGauge />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div style={{maxWidth: '600px', marginTop: '25px', margin: 'auto'}}>
|
||||||
|
<h3 style={{textAlign: 'center'}}>Bar Component:</h3>
|
||||||
|
<Bar />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{maxWidth: '600px', marginTop: '25px', margin: 'auto'}}>
|
||||||
|
<h3 style={{textAlign: 'center'}}>Line Component:</h3>
|
||||||
|
<Line />
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "chartjs-2-react",
|
"name": "chartjs-2-react",
|
||||||
"version": "1.0.10",
|
"version": "1.1.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "chartjs-2-react",
|
"name": "chartjs-2-react",
|
||||||
"version": "1.0.12",
|
"version": "1.1.1",
|
||||||
"description": "Libreria de chart.js para React.js",
|
"description": "Libreria de chart.js para React.js",
|
||||||
"author": "aleleba",
|
"author": "aleleba",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
87
src/Bar.jsx
Normal file
87
src/Bar.jsx
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
import React, { Component } from 'react';
|
||||||
|
|
||||||
|
import ID from './Service/ID.js';
|
||||||
|
|
||||||
|
class BarComponent extends Component {
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
|
||||||
|
super(props)
|
||||||
|
|
||||||
|
let id = this.props.id ? this.props.id : ID(),
|
||||||
|
data = {
|
||||||
|
labels: ['Valor 1', 'Valor 2', 'Valor 3', 'Valor 4', 'Valor 5'],
|
||||||
|
datasets: [{
|
||||||
|
label: 'Valor',
|
||||||
|
data: [],
|
||||||
|
backgroundColor: 'rgba(75, 192, 192, 0.2)',
|
||||||
|
borderColor: 'rgba(75, 192, 192, 1)',
|
||||||
|
borderWidth: 1
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options = {
|
||||||
|
responsive: true,
|
||||||
|
scales: {
|
||||||
|
yAxes: [{
|
||||||
|
ticks: {
|
||||||
|
beginAtZero: true
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
legend: {
|
||||||
|
display: true,
|
||||||
|
position: 'bottom'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(let i = 0; i < 5 ; i ++){
|
||||||
|
|
||||||
|
let dato = Math.round(Math.random() * 25)
|
||||||
|
|
||||||
|
data.datasets[0].data.push(dato)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
this.state = {
|
||||||
|
id,
|
||||||
|
chart: null,
|
||||||
|
data,
|
||||||
|
options
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount(){
|
||||||
|
|
||||||
|
var ctx = document.getElementById(`${this.state.id}`).getContext('2d');
|
||||||
|
|
||||||
|
let config = {
|
||||||
|
type: 'bar',
|
||||||
|
data: (this.props.config !== undefined) && (this.props.config.data !== undefined) ? this.props.config.data : this.state.data,
|
||||||
|
options: (this.props.config !== undefined) && (this.props.config.options !== undefined) ? this.props.config.options : this.state.options
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
id: this.props.id !== undefined ? this.props.id : this.state.id,
|
||||||
|
chart: new Chart(ctx, config)
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
render(){
|
||||||
|
|
||||||
|
if (this.state.chart !== null) {
|
||||||
|
|
||||||
|
this.state.chart.update();
|
||||||
|
|
||||||
|
return <canvas id={`${this.state.id}`} />
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return <canvas id={`${this.state.id}`} />
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default BarComponent
|
89
src/Line.jsx
Normal file
89
src/Line.jsx
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import React, { Component } from 'react';
|
||||||
|
|
||||||
|
import ID from './Service/ID.js';
|
||||||
|
|
||||||
|
class LineComponent extends Component {
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
|
||||||
|
super(props)
|
||||||
|
|
||||||
|
let id = this.props.id ? this.props.id : ID(),
|
||||||
|
data = {
|
||||||
|
labels: ['Valor 1', 'Valor 2', 'Valor 3', 'Valor 4', 'Valor 5'],
|
||||||
|
datasets: [{
|
||||||
|
label: 'Valor',
|
||||||
|
fill: false,
|
||||||
|
data: [],
|
||||||
|
backgroundColor: 'rgba(75, 192, 192, 0.2)',
|
||||||
|
borderColor: 'rgba(75, 192, 192, 1)',
|
||||||
|
borderWidth: 1
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options = {
|
||||||
|
responsive: true,
|
||||||
|
scales: {
|
||||||
|
yAxes: [{
|
||||||
|
ticks: {
|
||||||
|
beginAtZero: true
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
legend: {
|
||||||
|
display: true,
|
||||||
|
position: 'bottom'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(let i = 0; i < 5 ; i ++){
|
||||||
|
|
||||||
|
let dato = Math.round(Math.random() * 25)
|
||||||
|
|
||||||
|
data.datasets[0].data.push(dato)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
this.state = {
|
||||||
|
id,
|
||||||
|
chart: null,
|
||||||
|
data,
|
||||||
|
options
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount(){
|
||||||
|
|
||||||
|
var ctx = document.getElementById(`${this.state.id}`).getContext('2d');
|
||||||
|
|
||||||
|
let config = {
|
||||||
|
type: 'line',
|
||||||
|
data: (this.props.config !== undefined) && (this.props.config.data !== undefined) ? this.props.config.data : this.state.data,
|
||||||
|
options: (this.props.config !== undefined) && (this.props.config.options !== undefined) ? this.props.config.options : this.state.options
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
id: this.props.id !== undefined ? this.props.id : this.state.id,
|
||||||
|
chart: new Chart(ctx, config)
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
render(){
|
||||||
|
|
||||||
|
if (this.state.chart !== null) {
|
||||||
|
|
||||||
|
this.state.chart.update();
|
||||||
|
|
||||||
|
return <canvas id={`${this.state.id}`} />
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return <canvas id={`${this.state.id}`} />
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default LineComponent
|
@ -1,5 +1,9 @@
|
|||||||
import RadialGauge from './RadialGauge.jsx';
|
import RadialGauge from './RadialGauge.jsx';
|
||||||
|
import Bar from './Bar.jsx';
|
||||||
|
import Line from './Line.jsx';
|
||||||
|
|
||||||
export { RadialGauge }
|
export { RadialGauge }
|
||||||
|
export { Bar }
|
||||||
|
export { Line }
|
||||||
|
|
||||||
export default { RadialGauge }
|
export default { RadialGauge }
|
||||||
|
Reference in New Issue
Block a user