diff --git a/example/src/App.js b/example/src/App.js index 19aad13..faba080 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -5,9 +5,13 @@ import { RadialGauge } from 'chartjs-2-react' export default class App extends Component { render () { return ( -
-

RadialGauge Component:

- +
+ +
+

RadialGauge Component:

+ +
+
) } diff --git a/package.json b/package.json index 68331fc..01c8849 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chartjs-2-react", - "version": "1.0.9", + "version": "1.0.10", "description": "Libreria de chart.js para React.js", "author": "aleleba", "license": "MIT", diff --git a/src/RadialGauge.jsx b/src/RadialGauge.jsx index 799ee8a..dfa1332 100644 --- a/src/RadialGauge.jsx +++ b/src/RadialGauge.jsx @@ -4,6 +4,8 @@ import Chart from 'chart.js'; import 'chartjs-chart-radial-gauge/build/Chart.RadialGauge.umd.min.js'; +import ID from './Service/ID.js' + class RadialGauge extends Component { constructor(props) { @@ -11,8 +13,11 @@ class RadialGauge extends Component { let porcentaje = Math.round(Math.random() * 100), porcentajeTexto = `${porcentaje} %` + + let id = this.props.id ? this.props.id : ID() this.state = { + id, chart: null, porcentaje, porcentajeTexto @@ -22,7 +27,7 @@ class RadialGauge extends Component { componentDidMount() { - var ctx = document.getElementById('chart').getContext('2d'); + var ctx = document.getElementById(`${this.state.id}`).getContext('2d'); var config = { type: 'radialGauge', @@ -110,10 +115,10 @@ class RadialGauge extends Component { this.state.chart.update(); - return + return } else { - return + return } } } diff --git a/src/Service/ID.js b/src/Service/ID.js new file mode 100644 index 0000000..216249b --- /dev/null +++ b/src/Service/ID.js @@ -0,0 +1,25 @@ +// Generate unique IDs for use as pseudo-private/protected names. +// Similar in concept to +// . +// +// 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 \ No newline at end of file