Skip to content

Commit

Permalink
Radar chart tooltips (#992)
Browse files Browse the repository at this point in the history
* RadarChart: Add ability to show tooltips at the end of each polygon

* Add showcase file for radar chart with tooltips

* Fix eslint errors

* Updates based on review: add onSeriesMouseOver, convert from CustomSVGSeries to MarkSeries

* Fix lint errors

* Added new params to documentation

* Fix merge conflicts
  • Loading branch information
emilysallstrom authored and mcnuttandrew committed Oct 15, 2018
1 parent 7a67a4a commit 6b87bd5
Show file tree
Hide file tree
Showing 14 changed files with 645 additions and 82 deletions.
8 changes: 8 additions & 0 deletions docs/custom-svg-series.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ Callback is triggered with two arguments. `value` is the data point, `info` obje
- `index` is the index of the data point in the array of data;
- `event` is the event object.
See [interaction](interaction.md)

#### onValueMouseOver (optional)
Type: `function(d, {event})`
`mouseover` event handler for the elements corresponding separate data points. First argument received is, `d`, the relevant data point, and second an object with the only `event` property.

#### onValueMouseOut (optional)
Type: `function(d, {event})`
`mouseout` event handler for the elements corresponding separate data points. First argument received is, `d`, the relevant data point, and second an object with the only `event` property.
20 changes: 20 additions & 0 deletions docs/radar-chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,23 @@ Specify the tick format for all axes. Will be over-ridden by tickFormats specifi
Type: `number`

The angle of the first axis in radians. Defaults to PI / 2.

#### renderAxesOverPolygons (optional)
Type: `boolean`
By default, the axes are rendered beneath the data (polygons) on the radar chart. Setting this to true will reverse the rendering, drawing the axes on top. This can be useful if you have a lot of data or your polygons have high opacity.

#### onValueMouseOver (optional)
Type: `function(d, {event})`
`mouseover` event handler for the elements corresponding separate data points, where each data point is a point on the radar chart. First argument received, `d`, is the relevant data point, including the `domain`, `name`, and `value` of the data point. The second argument is the `event` property.

#### onValueMouseOut (optional)
Type: `function(d, {event})`
`mouseout` event handler for the elements corresponding separate data points. First argument received is, `d`, the relevant data point, and second an object with the only `event` property.

#### onSeriesMouseOver (optional)
Type: `function(d, {event})`
`mouseover` event handler for the elements corresponding separate data points. First argument received is, `d`, the relevant data point, and second an object with the only `event` property.

#### onSeriesMouseOut (optional)
Type: `function(d, {event})`
`mouseout` event handler for the elements corresponding separate data points. First argument received is, `d`, the relevant data point, and second an object with the only `event` property.
4 changes: 4 additions & 0 deletions showcase/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ import ArcSeriesExample from './radial-chart/arc-series-example';
import BasicRadarChart from './radar-chart/basic-radar-chart';
import AnimatedRadarChart from './radar-chart/animated-radar-chart';
import FourQuadrantRadarChart from './radar-chart/four-quadrant-radar-chart';
import RadarChartWithTooltips from './radar-chart/radar-chart-with-tooltips';
import RadarChartSeriesTooltips from './radar-chart/radar-chart-series-tooltips';

import BasicParallelCoordinates from './parallel-coordinates/basic-parallel-coordinates';
import AnimatedParallelCoordinates from './parallel-coordinates/animated-parallel-coordinates';
Expand Down Expand Up @@ -270,6 +272,8 @@ const mainShowCase = {
AnimatedRadarChart,
BasicRadarChart,
FourQuadrantRadarChart,
RadarChartWithTooltips,
RadarChartSeriesTooltips,

BasicParallelCoordinates,
AnimatedParallelCoordinates,
Expand Down
25 changes: 23 additions & 2 deletions showcase/plot/custom-svg-all-the-marks.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
YAxis,
VerticalGridLines,
HorizontalGridLines,
CustomSVGSeries
CustomSVGSeries,
Hint
} from 'index';

import ShowcaseButton from '../showcase-components/showcase-button';
Expand All @@ -52,12 +53,21 @@ function generateData(reversed) {
const DATA = generateData(false);
const REVERSED_DATA = generateData(true);

const tipStyle = {
display: 'flex',
color: '#fff',
background: '#000',
alignItems: 'center',
padding: '5px'
};

export default class Example extends React.Component {
state = {
reverse: false
};
render() {
const {reverse} = this.state;
const {reverse, hoveredCell} = this.state;

return (
<div>
<ShowcaseButton
Expand All @@ -73,7 +83,18 @@ export default class Example extends React.Component {
animation
style={{stroke: 'red', fill: 'orange'}}
data={reverse ? REVERSED_DATA : DATA}
onValueMouseOver={ v => {
this.setState({hoveredCell: v});
}}
onValueMouseOut={v => this.setState({hoveredCell: false})}
/>
{hoveredCell && (
<Hint value={hoveredCell}>
<div style={tipStyle}>
{hoveredCell.customComponent}
</div>
</Hint>
)}
</XYPlot>
</div>
);
Expand Down
110 changes: 110 additions & 0 deletions showcase/radar-chart/radar-chart-series-tooltips.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (c) 2016 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import React, {Component} from 'react';
import {format} from 'd3-format';

import RadarChart from 'radar-chart';
import {Hint} from 'index';


const DATA = [
{
name: 'Mercedes',
mileage: 7,
price: 10,
safety: 8,
performance: 9,
interior: 7,
warranty: 7
},
{
name: 'Honda',
mileage: 8,
price: 6,
safety: 9,
performance: 6,
interior: 3,
warranty: 9
},
{
name: 'Chevrolet',
mileage: 5,
price: 4,
safety: 6,
performance: 4,
interior: 5,
warranty: 6
}
];

const basicFormat = format('.2r');
const wideFormat = format('.3r');

const tipStyle = {
display: 'flex',
color: '#fff',
background: '#000',
alignItems: 'center',
padding: '5px'
};

export default class BasicRadarChart extends Component {
state = {
hoveredCell: false
};

render() {
const {hoveredCell} = this.state;

return (
<RadarChart
data={DATA}
tickFormat={t => wideFormat(t)}
startingAngle={0}
domains={[
{name: 'mileage', domain: [0, 10]},
{
name: 'price',
domain: [2, 16],
tickFormat: t => `$${basicFormat(t)}`,
getValue: d => d.price
},
{name: 'safety', domain: [5, 10], getValue: d => d.safety},
{name: 'performance', domain: [0, 10], getValue: d => d.performance},
{name: 'interior', domain: [0, 7], getValue: d => d.interior},
{name: 'warranty', domain: [10, 2], getValue: d => d.warranty}
]}
width={400}
height={300}
onSeriesMouseOver={(data) => {
this.setState({hoveredCell: data.event[0]});
}}
onSeriesMouseOut={() => this.setState({hoveredCell: false})}
>
{hoveredCell && (
<Hint value={{x: 0, y: 0}}>
<div style={tipStyle}>{hoveredCell.name}</div>
</Hint>
)}
</RadarChart>
);
}
}
Loading

0 comments on commit 6b87bd5

Please sign in to comment.