Skip to content

Commit dfb0226

Browse files
Add legend options examples
1 parent 94f2294 commit dfb0226

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
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
@@ -13,6 +13,8 @@ import BubbleExample from './components/bubble';
1313
import MixedDataExample from './components/mix';
1414
import RandomizedDataLineExample from './components/randomizedLine';
1515
import CrazyDataLineExample from './components/crazyLine';
16+
import LegendOptionsExample from './components/legend-options'
17+
import LegendHandlersExample from './components/legend-handlers'
1618

1719
class App extends React.Component {
1820
render() {
@@ -42,6 +44,10 @@ class App extends React.Component {
4244
<RandomizedDataLineExample />
4345
<hr />
4446
<CrazyDataLineExample />
47+
<hr />
48+
<LegendOptionsExample />
49+
<hr />
50+
<LegendHandlersExample />
4551
</div>
4652
);
4753
}

0 commit comments

Comments
 (0)