Skip to content

Commit e5b1a1a

Browse files
authored
Merge pull request #158 from ekscentrysytet/master
Add legend support to charts
2 parents 3a3323f + cc48b6e commit e5b1a1a

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react';
2+
import {Pie} from 'react-chartjs-2';
3+
4+
const data = {
5+
labels: [
6+
'Red',
7+
'Green',
8+
'Yellow'
9+
],
10+
datasets: [{
11+
data: [300, 50, 100],
12+
backgroundColor: [
13+
'#FF6384',
14+
'#36A2EB',
15+
'#FFCE56'
16+
],
17+
hoverBackgroundColor: [
18+
'#FF6384',
19+
'#36A2EB',
20+
'#FFCE56'
21+
]
22+
}]
23+
};
24+
25+
const legendOpts = {
26+
onClick: (e, item) => alert(`Item with text ${item.text} and index ${item.index} clicked`),
27+
onHover: (e, item) => alert(`Item with text ${item.text} and index ${item.index} hovered`),
28+
};
29+
30+
export default React.createClass({
31+
displayName: 'LegendExample',
32+
33+
getInitialState() {
34+
return {
35+
legend: legendOpts
36+
}
37+
},
38+
39+
applyLegendSettings() {
40+
const { value } = this.legendOptsInput;
41+
42+
try {
43+
const opts = JSON.parse(value);
44+
this.setState({
45+
legend: opts
46+
});
47+
} catch(e) {
48+
alert(e.message);
49+
throw Error(e);
50+
}
51+
},
52+
53+
render() {
54+
return (
55+
<div>
56+
<h2>Legend Handlers Example</h2>
57+
<p>Hover over label and click</p>
58+
<Pie data={data} legend={this.state.legend} />
59+
</div>
60+
);
61+
}
62+
})
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from 'react';
2+
import {Pie} from 'react-chartjs-2';
3+
4+
const data = {
5+
labels: [
6+
'Red',
7+
'Green',
8+
'Yellow'
9+
],
10+
datasets: [{
11+
data: [300, 50, 100],
12+
backgroundColor: [
13+
'#FF6384',
14+
'#36A2EB',
15+
'#FFCE56'
16+
],
17+
hoverBackgroundColor: [
18+
'#FF6384',
19+
'#36A2EB',
20+
'#FFCE56'
21+
]
22+
}]
23+
};
24+
25+
const legendOpts = {
26+
display: true,
27+
position: 'top',
28+
fullWidth: true,
29+
reverse: false,
30+
labels: {
31+
fontColor: 'rgb(255, 99, 132)'
32+
}
33+
};
34+
35+
export default React.createClass({
36+
displayName: 'LegendExample',
37+
38+
getInitialState() {
39+
return {
40+
legend: legendOpts
41+
}
42+
},
43+
44+
applyLegendSettings() {
45+
const { value } = this.legendOptsInput;
46+
47+
try {
48+
const opts = JSON.parse(value);
49+
this.setState({
50+
legend: opts
51+
});
52+
} catch(e) {
53+
alert(e.message);
54+
throw Error(e);
55+
}
56+
},
57+
58+
render() {
59+
return (
60+
<div>
61+
<h2>Legend Options Example</h2>
62+
<textarea
63+
cols="40"
64+
rows="15"
65+
ref={input => { this.legendOptsInput = input; }}
66+
defaultValue={JSON.stringify(this.state.legend, null, 2)}></textarea>
67+
<div>
68+
<button onClick={this.applyLegendSettings}>Apply legend settings</button>
69+
</div>
70+
<Pie data={data} legend={this.state.legend} redraw />
71+
</div>
72+
);
73+
}
74+
})

example/src/example.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import ScatterExample from './components/scatter';
1414
import MixedDataExample from './components/mix';
1515
import RandomizedDataLineExample from './components/randomizedLine';
1616
import CrazyDataLineExample from './components/crazyLine';
17+
import LegendOptionsExample from './components/legend-options'
18+
import LegendHandlersExample from './components/legend-handlers'
1719

1820
class App extends React.Component {
1921
render() {
@@ -45,6 +47,10 @@ class App extends React.Component {
4547
<RandomizedDataLineExample />
4648
<hr />
4749
<CrazyDataLineExample />
50+
<hr />
51+
<LegendOptionsExample />
52+
<hr />
53+
<LegendHandlersExample />
4854
</div>
4955
);
5056
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class ChartComponent extends React.Component {
203203
const {options, legend, type, redraw, plugins} = this.props;
204204
const node = this.element;
205205
const data = this.memoizeDataProps();
206+
options.legend = legend;
206207

207208
this.chart_instance = new Chart(node, {
208209
type,

test/__tests__/Chart_spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,15 @@ describe('<Chart />', () => {
122122
it('renders on props.options change', () => {
123123
const spy = sinon.spy(Chart.prototype, 'render');
124124
const wrapper = mountComponent({ options: {} });
125+
const defaultLegendOpts = wrapper.prop('legend');
125126

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

128-
wrapper.setProps({ options: {} });
129+
wrapper.setProps({ options: { legend: defaultLegendOpts } });
129130

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

132-
wrapper.setProps({ options: { a: 1 } });
133+
wrapper.setProps({ options: { legend: defaultLegendOpts, a: 1 } });
133134

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

0 commit comments

Comments
 (0)