Skip to content

Commit 25a4559

Browse files
committed
added onElementsClick feature
1 parent 92a8068 commit 25a4559

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,23 @@ import {Doughnut} from 'react-chartjs-2';
4040
* data: PropTypes.object.isRequired,
4141
* height: PropTypes.number,
4242
* legend: PropTypes.object,
43+
* onElementsClick: PropTypes.func,
4344
* options: PropTypes.object,
4445
* redraw: PropTypes.bool,
4546
* width: PropTypes.number
4647

48+
### Events
49+
50+
#### onElementsClick (function)
51+
52+
A function to be called when mouse clicked on chart elememts, will return all element at that point as an array. [Check](https://github.com/chartjs/Chart.js/blob/master/docs/09-Advanced.md#getelementsatevente)
53+
54+
```
55+
{
56+
onElementsClick: (elems) => {}
57+
// `elems` is an array of chartElements
58+
}
59+
```
4760

4861
## Development (`src`, `lib` and the build process)
4962

dist/react-chartjs-2.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var ChartComponent = _react2['default'].createClass({
4242
data: _react.PropTypes.object.isRequired,
4343
height: _react.PropTypes.number,
4444
legend: _react.PropTypes.object,
45+
onElementsClick: _react.PropTypes.func,
4546
options: _react.PropTypes.object,
4647
redraw: _react.PropTypes.bool,
4748
type: _react.PropTypes.oneOf(['doughnut', 'pie', 'line', 'bar', 'horizontalBar', 'radar', 'polarArea']),
@@ -134,14 +135,25 @@ var ChartComponent = _react2['default'].createClass({
134135
});
135136
},
136137

138+
handleOnClick: function handleOnClick(evt) {
139+
var elems = this.chart_instance.getElementsAtEvent(evt);
140+
if (elems.length) {
141+
var onElementsClick = this.props.onElementsClick;
142+
143+
onElementsClick(elems);
144+
}
145+
},
146+
137147
render: function render() {
138148
var _props3 = this.props;
139149
var height = _props3.height;
140150
var width = _props3.width;
151+
var onElementsClick = _props3.onElementsClick;
141152

142153
return _react2['default'].createElement('canvas', {
143154
height: height,
144-
width: width
155+
width: width,
156+
onClick: typeof onElementsClick === 'function' ? this.handleOnClick : null
145157
});
146158
}
147159
});

dist/react-chartjs-2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Chart.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var ChartComponent = _react2['default'].createClass({
4040
data: _react.PropTypes.object.isRequired,
4141
height: _react.PropTypes.number,
4242
legend: _react.PropTypes.object,
43+
onElementsClick: _react.PropTypes.func,
4344
options: _react.PropTypes.object,
4445
redraw: _react.PropTypes.bool,
4546
type: _react.PropTypes.oneOf(['doughnut', 'pie', 'line', 'bar', 'horizontalBar', 'radar', 'polarArea']),
@@ -132,14 +133,25 @@ var ChartComponent = _react2['default'].createClass({
132133
});
133134
},
134135

136+
handleOnClick: function handleOnClick(evt) {
137+
var elems = this.chart_instance.getElementsAtEvent(evt);
138+
if (elems.length) {
139+
var onElementsClick = this.props.onElementsClick;
140+
141+
onElementsClick(elems);
142+
}
143+
},
144+
135145
render: function render() {
136146
var _props3 = this.props;
137147
var height = _props3.height;
138148
var width = _props3.width;
149+
var onElementsClick = _props3.onElementsClick;
139150

140151
return _react2['default'].createElement('canvas', {
141152
height: height,
142-
width: width
153+
width: width,
154+
onClick: typeof onElementsClick === 'function' ? this.handleOnClick : null
143155
});
144156
}
145157
});

src/Chart.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ const ChartComponent = React.createClass({
1111
data: PropTypes.object.isRequired,
1212
height: PropTypes.number,
1313
legend: PropTypes.object,
14+
onElementsClick: PropTypes.func,
1415
options: PropTypes.object,
1516
redraw: PropTypes.bool,
1617
type: PropTypes.oneOf(['doughnut', 'pie', 'line', 'bar', 'horizontalBar', 'radar', 'polarArea']),
17-
width: PropTypes.number
18+
width: PropTypes.number,
1819
},
1920

2021
getDefaultProps() {
@@ -26,7 +27,7 @@ const ChartComponent = React.createClass({
2627
type: 'doughnut',
2728
height: 200,
2829
width: 200,
29-
redraw: false
30+
redraw: false,
3031
};
3132
},
3233

@@ -94,13 +95,22 @@ const ChartComponent = React.createClass({
9495
});
9596
},
9697

98+
handleOnClick(evt) {
99+
const elems = this.chart_instance.getElementsAtEvent(evt);
100+
if (elems.length) {
101+
const {onElementsClick} = this.props;
102+
onElementsClick(elems);
103+
}
104+
},
105+
97106
render() {
98-
const {height, width} = this.props;
107+
const {height, width, onElementsClick} = this.props;
99108

100109
return (
101110
<canvas
102111
height={height}
103112
width={width}
113+
onClick={typeof onElementsClick === 'function' ? this.handleOnClick : null}
104114
/>
105115
);
106116
}

0 commit comments

Comments
 (0)