Skip to content

Commit

Permalink
Rc 2.5.0 (#163)
Browse files Browse the repository at this point in the history
* Add legend support to charts

* added polyfill for find
  • Loading branch information
jerairrest authored Jul 15, 2017
1 parent 3a3323f commit 6e32667
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 6 deletions.
62 changes: 62 additions & 0 deletions example/src/components/legend-handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';
import {Pie} from 'react-chartjs-2';

const data = {
labels: [
'Red',
'Green',
'Yellow'
],
datasets: [{
data: [300, 50, 100],
backgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
],
hoverBackgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
]
}]
};

const legendOpts = {
onClick: (e, item) => alert(`Item with text ${item.text} and index ${item.index} clicked`),
onHover: (e, item) => alert(`Item with text ${item.text} and index ${item.index} hovered`),
};

export default React.createClass({
displayName: 'LegendExample',

getInitialState() {
return {
legend: legendOpts
}
},

applyLegendSettings() {
const { value } = this.legendOptsInput;

try {
const opts = JSON.parse(value);
this.setState({
legend: opts
});
} catch(e) {
alert(e.message);
throw Error(e);
}
},

render() {
return (
<div>
<h2>Legend Handlers Example</h2>
<p>Hover over label and click</p>
<Pie data={data} legend={this.state.legend} />
</div>
);
}
})
74 changes: 74 additions & 0 deletions example/src/components/legend-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';
import {Pie} from 'react-chartjs-2';

const data = {
labels: [
'Red',
'Green',
'Yellow'
],
datasets: [{
data: [300, 50, 100],
backgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
],
hoverBackgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
]
}]
};

const legendOpts = {
display: true,
position: 'top',
fullWidth: true,
reverse: false,
labels: {
fontColor: 'rgb(255, 99, 132)'
}
};

export default React.createClass({
displayName: 'LegendExample',

getInitialState() {
return {
legend: legendOpts
}
},

applyLegendSettings() {
const { value } = this.legendOptsInput;

try {
const opts = JSON.parse(value);
this.setState({
legend: opts
});
} catch(e) {
alert(e.message);
throw Error(e);
}
},

render() {
return (
<div>
<h2>Legend Options Example</h2>
<textarea
cols="40"
rows="15"
ref={input => { this.legendOptsInput = input; }}
defaultValue={JSON.stringify(this.state.legend, null, 2)}></textarea>
<div>
<button onClick={this.applyLegendSettings}>Apply legend settings</button>
</div>
<Pie data={data} legend={this.state.legend} redraw />
</div>
);
}
})
6 changes: 6 additions & 0 deletions example/src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import ScatterExample from './components/scatter';
import MixedDataExample from './components/mix';
import RandomizedDataLineExample from './components/randomizedLine';
import CrazyDataLineExample from './components/crazyLine';
import LegendOptionsExample from './components/legend-options'
import LegendHandlersExample from './components/legend-handlers'

class App extends React.Component {
render() {
Expand Down Expand Up @@ -45,6 +47,10 @@ class App extends React.Component {
<RandomizedDataLineExample />
<hr />
<CrazyDataLineExample />
<hr />
<LegendOptionsExample />
<hr />
<LegendHandlersExample />
</div>
);
}
Expand Down
4 changes: 2 additions & 2 deletions example/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<body>
<div class="container">
<h1>react-chartjs-2</h1>
<h2><a href="https://github.com/gor181/react-chartjs-2">View project on GitHub</a></h2>
<h2><a href="https://github.com/jerairrest/react-chartjs-2">View project on GitHub</a></h2>
<!-- the example app is rendered into this div -->
<div id="app"></div>
<div class="hint">
<!-- put any hints about your component example here -->
</div>
<div class="footer">
Copyright &copy; 2016 Goran Udosic.
Copyright &copy; 2017 Jeremy Ayerst.
</div>
</div>
<script src="common.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-chartjs-2",
"version": "2.4.1",
"version": "2.5.0",
"description": "react-chartjs-2",
"main": "lib/index.js",
"author": "Goran Udosic",
Expand Down
50 changes: 49 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import Chart from 'chart.js';
import isEqual from 'lodash.isequal';


//Taken from MDN
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}

// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];

// 5. Let k be 0.
var k = 0;

// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}

// 7. Return undefined.
return undefined;
}
});
}


class ChartComponent extends React.Component {
static getLabelAsKey = d => d.label;

Expand All @@ -22,6 +68,7 @@ class ChartComponent extends React.Component {
plugins: PropTypes.arrayOf(PropTypes.object),
redraw: PropTypes.bool,
type: function(props, propName, componentName) {

if(!Object.keys(Chart.controllers).find((chartType) => chartType === props[propName])){
return new Error(
'Invalid chart type `' + props[propName] + '` supplied to' +
Expand Down Expand Up @@ -203,6 +250,7 @@ class ChartComponent extends React.Component {
const {options, legend, type, redraw, plugins} = this.props;
const node = this.element;
const data = this.memoizeDataProps();
options.legend = legend;

this.chart_instance = new Chart(node, {
type,
Expand Down
5 changes: 3 additions & 2 deletions test/__tests__/Chart_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,15 @@ describe('<Chart />', () => {
it('renders on props.options change', () => {
const spy = sinon.spy(Chart.prototype, 'render');
const wrapper = mountComponent({ options: {} });
const defaultLegendOpts = wrapper.prop('legend');

expect(spy.callCount).to.equal(1);

wrapper.setProps({ options: {} });
wrapper.setProps({ options: { legend: defaultLegendOpts } });

expect(spy.callCount).to.equal(1);

wrapper.setProps({ options: { a: 1 } });
wrapper.setProps({ options: { legend: defaultLegendOpts, a: 1 } });

expect(spy.callCount).to.equal(2);

Expand Down

0 comments on commit 6e32667

Please sign in to comment.