Skip to content

Commit

Permalink
feat: support 'scatter' charts out-of-the-box
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kumar Harsh committed Jul 3, 2017
1 parent 15f1448 commit a711447
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ componentWillMount() {
}
```

### Scatter Charts

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.

### Events

#### onElementsClick || getElementsAtEvent (function)
Expand Down
44 changes: 44 additions & 0 deletions example/src/components/scatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import {Scatter} from 'react-chartjs-2';

const data = {
labels: ['Scatter'],
datasets: [
{
label: 'My First dataset',
fill: false,
backgroundColor: 'rgba(75,192,192,0.4)',
pointBorderColor: 'rgba(75,192,192,1)',
pointBackgroundColor: '#fff',
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: 'rgba(75,192,192,1)',
pointHoverBorderColor: 'rgba(220,220,220,1)',
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [
{ x: 65, y: 75 },
{ x: 59, y: 49 },
{ x: 80, y: 90 },
{ x: 81, y: 29 },
{ x: 56, y: 36 },
{ x: 55, y: 25 },
{ x: 40, y: 18 },
]
}
]
};

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

render() {
return (
<div>
<h2>Scatter Example</h2>
<Scatter data={data} />
</div>
);
}
});
3 changes: 3 additions & 0 deletions example/src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import HorizontalBarExample from './components/horizontalBar';
import RadarExample from './components/radar';
import PolarExample from './components/polar';
import BubbleExample from './components/bubble';
import ScatterExample from './components/scatter';
import MixedDataExample from './components/mix';
import RandomizedDataLineExample from './components/randomizedLine';
import CrazyDataLineExample from './components/crazyLine';
Expand Down Expand Up @@ -37,6 +38,8 @@ class App extends React.Component {
<hr />
<BubbleExample />
<hr />
<ScatterExample />
<hr />
<MixedDataExample />
<hr />
<RandomizedDataLineExample />
Expand Down
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isEqual from 'lodash.isequal';

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

static propTypes = {
data: PropTypes.oneOfType([
PropTypes.object,
Expand All @@ -21,7 +21,7 @@ class ChartComponent extends React.Component {
options: PropTypes.object,
plugins: PropTypes.arrayOf(PropTypes.object),
redraw: PropTypes.bool,
type: PropTypes.oneOf(['doughnut', 'pie', 'line', 'bar', 'horizontalBar', 'radar', 'polarArea', 'bubble']),
type: PropTypes.oneOf(['doughnut', 'pie', 'line', 'bar', 'horizontalBar', 'radar', 'polarArea', 'bubble', 'scatter']),
width: PropTypes.number,
datasetKeyProvider: PropTypes.func
}
Expand Down Expand Up @@ -332,5 +332,17 @@ export class Bubble extends React.Component {
}
}

export class Scatter extends React.Component {
render() {
return (
<ChartComponent
{...this.props}
ref={ref => this.chart_instance = ref && ref.chart_instance}
type='scatter'
/>
);
}
}

export const defaults = Chart.defaults;
export {Chart};

0 comments on commit a711447

Please sign in to comment.