Skip to content

Commit a711447

Browse files
author
Kumar Harsh
committed
feat: support 'scatter' charts out-of-the-box
Chartjs has default config for scatter charts which are different than line charts in four key areas: * x and y axis are both `linear`. * hover mode is `single`. * showLines is `false`. * tooltip title doesn't make sense, as both x and y are both values. Keeping in mind that this default config is already present within the base chartjs library, a different 'Scatter' chart should be exposed by this library - to avoid redundant configs to be written by each developer.
1 parent 15f1448 commit a711447

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ componentWillMount() {
167167
}
168168
```
169169

170+
### Scatter Charts
171+
172+
If you're using Chart.js 2.6 and below, add the `showLines: false` property to your chart options. This was later [added](https://github.com/chartjs/Chart.js/commit/7fa60523599a56255cde78a49e848166bd233c6e) in the default config, so users of later versions would not need to do this extra step.
173+
170174
### Events
171175

172176
#### onElementsClick || getElementsAtEvent (function)

example/src/components/scatter.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import {Scatter} from 'react-chartjs-2';
3+
4+
const data = {
5+
labels: ['Scatter'],
6+
datasets: [
7+
{
8+
label: 'My First dataset',
9+
fill: false,
10+
backgroundColor: 'rgba(75,192,192,0.4)',
11+
pointBorderColor: 'rgba(75,192,192,1)',
12+
pointBackgroundColor: '#fff',
13+
pointBorderWidth: 1,
14+
pointHoverRadius: 5,
15+
pointHoverBackgroundColor: 'rgba(75,192,192,1)',
16+
pointHoverBorderColor: 'rgba(220,220,220,1)',
17+
pointHoverBorderWidth: 2,
18+
pointRadius: 1,
19+
pointHitRadius: 10,
20+
data: [
21+
{ x: 65, y: 75 },
22+
{ x: 59, y: 49 },
23+
{ x: 80, y: 90 },
24+
{ x: 81, y: 29 },
25+
{ x: 56, y: 36 },
26+
{ x: 55, y: 25 },
27+
{ x: 40, y: 18 },
28+
]
29+
}
30+
]
31+
};
32+
33+
export default React.createClass({
34+
displayName: 'ScatterExample',
35+
36+
render() {
37+
return (
38+
<div>
39+
<h2>Scatter Example</h2>
40+
<Scatter data={data} />
41+
</div>
42+
);
43+
}
44+
});

example/src/example.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import HorizontalBarExample from './components/horizontalBar';
1010
import RadarExample from './components/radar';
1111
import PolarExample from './components/polar';
1212
import BubbleExample from './components/bubble';
13+
import ScatterExample from './components/scatter';
1314
import MixedDataExample from './components/mix';
1415
import RandomizedDataLineExample from './components/randomizedLine';
1516
import CrazyDataLineExample from './components/crazyLine';
@@ -37,6 +38,8 @@ class App extends React.Component {
3738
<hr />
3839
<BubbleExample />
3940
<hr />
41+
<ScatterExample />
42+
<hr />
4043
<MixedDataExample />
4144
<hr />
4245
<RandomizedDataLineExample />

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import isEqual from 'lodash.isequal';
66

77
class ChartComponent extends React.Component {
88
static getLabelAsKey = d => d.label;
9-
9+
1010
static propTypes = {
1111
data: PropTypes.oneOfType([
1212
PropTypes.object,
@@ -21,7 +21,7 @@ class ChartComponent extends React.Component {
2121
options: PropTypes.object,
2222
plugins: PropTypes.arrayOf(PropTypes.object),
2323
redraw: PropTypes.bool,
24-
type: PropTypes.oneOf(['doughnut', 'pie', 'line', 'bar', 'horizontalBar', 'radar', 'polarArea', 'bubble']),
24+
type: PropTypes.oneOf(['doughnut', 'pie', 'line', 'bar', 'horizontalBar', 'radar', 'polarArea', 'bubble', 'scatter']),
2525
width: PropTypes.number,
2626
datasetKeyProvider: PropTypes.func
2727
}
@@ -332,5 +332,17 @@ export class Bubble extends React.Component {
332332
}
333333
}
334334

335+
export class Scatter extends React.Component {
336+
render() {
337+
return (
338+
<ChartComponent
339+
{...this.props}
340+
ref={ref => this.chart_instance = ref && ref.chart_instance}
341+
type='scatter'
342+
/>
343+
);
344+
}
345+
}
346+
335347
export const defaults = Chart.defaults;
336348
export {Chart};

0 commit comments

Comments
 (0)