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 | |
---|---|---|---|
783d248f1e | |||
5170fae311 | |||
c8306d2382 |
14930
example/package-lock.json
generated
14930
example/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "chart-2-react-example",
|
"name": "chartjs-2-react-example",
|
||||||
"homepage": "https://aleleba.github.io/chart-2-react",
|
"homepage": "https://aleleba.github.io/chartjs-2-react",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": true,
|
"private": true,
|
||||||
@ -9,7 +9,7 @@
|
|||||||
"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",
|
||||||
"chart-2-react": "file:.."
|
"chartjs-2-react": "file:.."
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start",
|
"start": "react-scripts start",
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
|
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
|
||||||
|
|
||||||
<title>chart-2-react</title>
|
<title>chartjs-2-react</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"short_name": "chart-2-react",
|
"short_name": "chartjs-2-react",
|
||||||
"name": "chart-2-react",
|
"name": "chartjs-2-react",
|
||||||
"start_url": "./index.html",
|
"start_url": "./index.html",
|
||||||
"display": "standalone",
|
"display": "standalone",
|
||||||
"theme_color": "#000000",
|
"theme_color": "#000000",
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
|
|
||||||
import { RadialGaugue } from 'chart-2-react'
|
import { RadialGauge } from 'chartjs-2-react'
|
||||||
|
|
||||||
export default class App extends Component {
|
export default class App extends Component {
|
||||||
render () {
|
render () {
|
||||||
return (
|
return (
|
||||||
<div style={{maxWidth: '600px', marginTop: '25px'}}>
|
<div>
|
||||||
<h3 style={{textAlign: 'center'}}>RadialGaugue Component:</h3>
|
|
||||||
<RadialGaugue />
|
<div style={{maxWidth: '600px', marginTop: '25px'}}>
|
||||||
|
<h3 style={{textAlign: 'center'}}>RadialGauge Component:</h3>
|
||||||
|
<RadialGauge />
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "chart-2-react",
|
"name": "chartjs-2-react",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "chartjs-2-react",
|
"name": "chartjs-2-react",
|
||||||
"version": "1.0.7",
|
"version": "1.0.10",
|
||||||
"description": "Libreria de chart.js para React.js",
|
"description": "Libreria de chart.js para React.js",
|
||||||
"author": "aleleba",
|
"author": "aleleba",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": "aleleba/chart-2-react",
|
"repository": "aleleba/chartjs-2-react",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.es.js",
|
"module": "dist/index.es.js",
|
||||||
"jsnext:main": "dist/index.es.js",
|
"jsnext:main": "dist/index.es.js",
|
||||||
|
@ -4,30 +4,38 @@ import Chart from 'chart.js';
|
|||||||
|
|
||||||
import 'chartjs-chart-radial-gauge/build/Chart.RadialGauge.umd.min.js';
|
import 'chartjs-chart-radial-gauge/build/Chart.RadialGauge.umd.min.js';
|
||||||
|
|
||||||
|
import ID from './Service/ID.js'
|
||||||
|
|
||||||
class RadialGauge extends Component {
|
class RadialGauge extends Component {
|
||||||
|
|
||||||
componentDidMount(){
|
constructor(props) {
|
||||||
|
super(props)
|
||||||
|
|
||||||
var ctx = document.getElementById('chart').getContext('2d');
|
let porcentaje = Math.round(Math.random() * 100),
|
||||||
|
porcentajeTexto = `${porcentaje} %`
|
||||||
/*var gradientStroke = ctx.createLinearGradient(500, 0, 100, 0);
|
|
||||||
gradientStroke.addColorStop(0, "#2A969E");
|
let id = this.props.id ? this.props.id : ID()
|
||||||
gradientStroke.addColorStop(1, "#2A969E");*/
|
|
||||||
//gradientStroke.addColorStop(2, "#f49080");
|
|
||||||
|
|
||||||
var config
|
this.state = {
|
||||||
|
id,
|
||||||
|
chart: null,
|
||||||
|
porcentaje,
|
||||||
|
porcentajeTexto
|
||||||
|
}
|
||||||
|
|
||||||
var randomScalingFactor = function() {
|
}
|
||||||
return Math.round(Math.random() * 100);
|
|
||||||
};
|
|
||||||
|
|
||||||
config = {
|
componentDidMount() {
|
||||||
|
|
||||||
|
var ctx = document.getElementById(`${this.state.id}`).getContext('2d');
|
||||||
|
|
||||||
|
var config = {
|
||||||
type: 'radialGauge',
|
type: 'radialGauge',
|
||||||
data: {
|
data: {
|
||||||
labels: this.props.config ? (this.props.config.data ? (this.props.config.data.labels || ["Porcentaje"]) : ["Porcentaje"]) : ["Porcentaje"],
|
labels: this.props.config ? (this.props.config.data ? (this.props.config.data.labels || ["Porcentaje"]) : ["Porcentaje"]) : ["Porcentaje"],
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
data: this.props.config ? ((this.props.config.data) ? (this.props.config.data.datasets !== undefined ? [this.props.config.data.datasets.data] : [randomScalingFactor()]) : [randomScalingFactor()]) : [randomScalingFactor()],
|
data: this.props.config ? ((this.props.config.data) ? (this.props.config.data.datasets !== undefined ? [this.props.config.data.datasets.data] : [this.state.porcentaje]) : [this.state.porcentaje]) : [this.state.porcentaje],
|
||||||
backgroundColor: this.props.config ? ( this.props.config.data ? (((this.props.config.data.datasets !== undefined) && (this.props.config.data.datasets.backgroundColor)) ? this.props.config.data.datasets.backgroundColor : '#2A969E') : '#2A969E') : '#2A969E',
|
backgroundColor: this.props.config ? ( this.props.config.data ? (((this.props.config.data.datasets !== undefined) && (this.props.config.data.datasets.backgroundColor)) ? this.props.config.data.datasets.backgroundColor : '#2A969E') : '#2A969E') : '#2A969E',
|
||||||
borderWidth: this.props.config ? ( this.props.config.data ? (((this.props.config.data.datasets !== undefined) && (this.props.config.data.datasets.borderWidth)) ? this.props.config.data.datasets.borderWidth : 0) : 0) : 0,
|
borderWidth: this.props.config ? ( this.props.config.data ? (((this.props.config.data.datasets !== undefined) && (this.props.config.data.datasets.borderWidth)) ? this.props.config.data.datasets.borderWidth : 0) : 0) : 0,
|
||||||
label: this.props.config ? ( this.props.config.data ? (((this.props.config.data.datasets !== undefined) && (this.props.config.data.datasets.label)) ? this.props.config.data.datasets.label : "Porcentaje") : "Porcentaje") : "Porcentaje"
|
label: this.props.config ? ( this.props.config.data ? (((this.props.config.data.datasets !== undefined) && (this.props.config.data.datasets.label)) ? this.props.config.data.datasets.label : "Porcentaje") : "Porcentaje") : "Porcentaje"
|
||||||
@ -87,15 +95,31 @@ class RadialGauge extends Component {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.setState({
|
||||||
new Chart(ctx, config);
|
chart: new Chart(ctx, config)
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
|
||||||
<canvas id="chart" />
|
if (this.state.chart !== null) {
|
||||||
)
|
|
||||||
|
this.props.config ? ((this.props.config.data) ? (this.props.config.data.datasets !== undefined ? (this.state.chart.data.datasets[0].data[0] = this.props.config.data.datasets.data) : [this.state.porcentaje]) : [this.state.porcentaje]) : [this.state.porcentaje]
|
||||||
|
|
||||||
|
//this.state.chart.data.datasets[0].data[0] = this.props.config.data.datasets.data)
|
||||||
|
|
||||||
|
this.props.config ? ((this.props.config.options) ? (this.props.config.options.centerArea !== undefined ? (this.state.chart.options.centerArea.text = this.props.config.options.centerArea.text) : null) : null) : null
|
||||||
|
|
||||||
|
//this.state.chart.options.centerArea.text = this.props.config.options.centerArea.text
|
||||||
|
|
||||||
|
this.state.chart.update();
|
||||||
|
|
||||||
|
return <canvas id={`${this.state.id}`} />
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return <canvas id={`${this.state.id}`} />
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
src/Service/ID.js
Normal file
25
src/Service/ID.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Generate unique IDs for use as pseudo-private/protected names.
|
||||||
|
// Similar in concept to
|
||||||
|
// <http://wiki.ecmascript.org/doku.php?id=strawman:names>.
|
||||||
|
//
|
||||||
|
// The goals of this function are twofold:
|
||||||
|
//
|
||||||
|
// * Provide a way to generate a string guaranteed to be unique when compared
|
||||||
|
// to other strings generated by this function.
|
||||||
|
// * Make the string complex enough that it is highly unlikely to be
|
||||||
|
// accidentally duplicated by hand (this is key if you're using `ID`
|
||||||
|
// as a private/protected name on an object).
|
||||||
|
//
|
||||||
|
// Use:
|
||||||
|
//
|
||||||
|
// var privateName = ID();
|
||||||
|
// var o = { 'public': 'foo' };
|
||||||
|
// o[privateName] = 'bar';
|
||||||
|
var ID = function () {
|
||||||
|
// Math.random should be unique because of its seeding algorithm.
|
||||||
|
// Convert it to base 36 (numbers + letters), and grab the first 9 characters
|
||||||
|
// after the decimal.
|
||||||
|
return '_' + Math.random().toString(36).substr(2, 9);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ID
|
@ -1,3 +1,5 @@
|
|||||||
import RadialGaugue from './RadialGauge.jsx';
|
import RadialGauge from './RadialGauge.jsx';
|
||||||
|
|
||||||
export default { RadialGaugue }
|
export { RadialGauge }
|
||||||
|
|
||||||
|
export default { RadialGauge }
|
||||||
|
Reference in New Issue
Block a user