2020-02-08 15:15:09 -06:00
|
|
|
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,
|
2020-02-11 09:26:23 -06:00
|
|
|
chart: null,
|
2020-02-08 15:15:09 -06:00
|
|
|
data,
|
2020-02-11 09:26:23 -06:00
|
|
|
options
|
2020-02-08 15:15:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
2020-02-11 09:26:23 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:15:09 -06:00
|
|
|
this.setState({
|
|
|
|
id: this.props.id !== undefined ? this.props.id : this.state.id,
|
2020-02-11 09:26:23 -06:00
|
|
|
chart: new Chart(ctx, config)
|
2020-02-08 15:15:09 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
2020-02-12 08:56:44 -06:00
|
|
|
|
2020-02-11 09:26:23 -06:00
|
|
|
if (this.state.chart !== null) {
|
2020-02-12 08:56:44 -06:00
|
|
|
|
|
|
|
if((this.props.config !== undefined) && (this.props.config.data !== undefined)){
|
|
|
|
this.state.chart.data.labels = this.props.config.data.labels
|
|
|
|
this.state.chart.data.datasets = this.props.config.data.datasets
|
|
|
|
}
|
|
|
|
|
|
|
|
if((this.props.config !== undefined) && (this.props.config.options !== undefined)){
|
|
|
|
this.state.chart.options = this.props.config.options
|
|
|
|
}
|
2020-02-11 09:26:23 -06:00
|
|
|
|
|
|
|
this.state.chart.update();
|
|
|
|
|
|
|
|
return <canvas id={`${this.state.id}`} />
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return <canvas id={`${this.state.id}`} />
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 15:15:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default LineComponent
|