Skip to content

Commit

Permalink
feat(markers): improve radar & line charts markers
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Aug 9, 2017
1 parent 1918772 commit 18c4347
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 126 deletions.
4 changes: 4 additions & 0 deletions src/Nivo.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const defaultTheme = {
strokeWidth: 1,
strokeDasharray: '',
},
markers: {
textColor: '#000',
fontSize: '11px',
},
}

export default {
Expand Down
8 changes: 8 additions & 0 deletions src/components/charts/SvgWrapper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import React from 'react'

const SvgWrapper = ({ width, height, margin, children }) =>
Expand Down
4 changes: 2 additions & 2 deletions src/components/charts/bubble/Bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
import React, { Component } from 'react'
import _ from 'lodash'
import { convertLabel } from '../../../lib/propertiesConverters'
import { getLabelGenerator } from '../../../lib/propertiesConverters'
import { bubblePropTypes, bubbleDefaultProps } from './BubbleProps'
import BubblePlaceholders from './BubblePlaceholders'
import { getColorGenerator } from '../../../lib/colorUtils'
Expand All @@ -23,7 +23,7 @@ const createNodes = ({
labelTextColor,
labelTextDY,
}) => {
const label = convertLabel(_label, labelFormat)
const label = getLabelGenerator(_label, labelFormat)
const borderColorFn = getColorGenerator(borderColor)
const textColorFn = getColorGenerator(labelTextColor)

Expand Down
11 changes: 11 additions & 0 deletions src/components/charts/line/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const Line = ({
markersColor,
markersBorderWidth,
markersBorderColor,
enableMarkersLabel,
markersLabel,
markersLabelFormat,
markersLabelYOffset,

// theming
theme,
Expand Down Expand Up @@ -89,6 +93,11 @@ const Line = ({
color={getInheritedColorGenerator(markersColor)}
borderWidth={markersBorderWidth}
borderColor={getInheritedColorGenerator(markersBorderColor)}
enableLabel={enableMarkersLabel}
label={markersLabel}
labelFormat={markersLabelFormat}
labelYOffset={markersLabelYOffset}
theme={theme}
{...motionProps}
/>
)
Expand Down Expand Up @@ -193,6 +202,7 @@ Line.propTypes = {
markersColor: PropTypes.any.isRequired,
markersBorderWidth: PropTypes.number.isRequired,
markersBorderColor: PropTypes.any.isRequired,
enableMarkersLabel: PropTypes.bool.isRequired,

// theming
theme: PropTypes.object.isRequired,
Expand Down Expand Up @@ -223,6 +233,7 @@ export const LineDefaultProps = {
markersColor: 'inherit',
markersBorderWidth: 0,
markersBorderColor: 'inherit',
enableMarkersLabel: false,

// theming
theme: {},
Expand Down
165 changes: 101 additions & 64 deletions src/components/charts/line/LineMarkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { TransitionMotion, spring } from 'react-motion'
import { motionPropTypes } from '../../../props'
import { getLabelGenerator } from '../../../lib/propertiesConverters'
import MarkersItem from '../../markers/MarkersItem'

export default class LineMarkers extends Component {
static propTypes = {
Expand All @@ -22,13 +24,29 @@ export default class LineMarkers extends Component {
color: PropTypes.func.isRequired,
borderWidth: PropTypes.number.isRequired,
borderColor: PropTypes.func.isRequired,

// labels
enableLabel: PropTypes.bool.isRequired,
label: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
labelFormat: PropTypes.string,
labelYOffset: PropTypes.number,

// theming
theme: PropTypes.shape({
markers: PropTypes.shape({
textColor: PropTypes.string.isRequired,
fontSize: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,

// motion
...motionPropTypes,
}

static defaultProps = {
animate: true,
motionStiffness: 90,
motionDamping: 15,
// labels
enableLabel: false,
label: 'y',
}

render() {
Expand All @@ -38,84 +56,103 @@ export default class LineMarkers extends Component {
color,
borderWidth,
borderColor,

// labels
enableLabel,
label,
labelFormat,
labelYOffset,

// theming
theme,

// motion
animate,
motionStiffness,
motionDamping,
} = this.props

const getLabel = getLabelGenerator(label, labelFormat)

const points = lines.reduce((acc, line) => {
const { id, points } = line

return [
...acc,
...points.map(point => ({
key: `${id}.${point.x}`,
x: point.x,
y: point.y,
fill: color(line),
stroke: borderColor(line),
})),
...points.map(point => {
const pointData = {
serie: { id },
x: point.key,
y: point.value,
}

return {
key: `${id}.${point.x}`,
x: point.x,
y: point.y,
fill: color(line),
stroke: borderColor(line),
label: enableLabel ? getLabel(pointData) : null,
}
}),
]
}, [])

let circles
if (animate === true) {
const springConfig = {
motionDamping,
motionStiffness,
}

circles = (
<TransitionMotion
styles={points.map(point => {
return {
key: point.key,
data: {
fill: point.fill,
stroke: point.stroke,
},
style: {
x: spring(point.x, springConfig),
y: spring(point.y, springConfig),
size: spring(size, springConfig),
},
}
})}
>
{interpolatedStyles =>
<g>
{interpolatedStyles.map(({ key, style, data }) =>
<circle
key={key}
cx={style.x}
cy={style.y}
r={style.size / 2}
fill={data.fill}
stroke={data.stroke}
strokeWidth={borderWidth}
/>
)}
</g>}
</TransitionMotion>
)
} else {
circles = points.map(point =>
<circle
key={point.key}
cx={point.x}
cy={point.y}
r={size / 2}
fill={point.fill}
stroke={point.stroke}
strokeWidth={borderWidth}
/>
if (animate !== true) {
return (
<g>
{points.map(point =>
<MarkersItem
key={point.key}
x={point.x}
y={point.y}
size={size}
color={point.fill}
borderWidth={borderWidth}
borderColor={point.stroke}
label={point.label}
labelYOffset={labelYOffset}
theme={theme}
/>
)}
</g>
)
}
const springConfig = {
motionDamping,
motionStiffness,
}

return (
<g>
{circles}
</g>
<TransitionMotion
styles={points.map(point => {
return {
key: point.key,
data: point,
style: {
x: spring(point.x, springConfig),
y: spring(point.y, springConfig),
size: spring(size, springConfig),
},
}
})}
>
{interpolatedStyles =>
<g>
{interpolatedStyles.map(({ key, style, data: point }) =>
<MarkersItem
key={key}
{...style}
color={point.fill}
borderWidth={borderWidth}
borderColor={point.stroke}
label={point.label}
labelYOffset={labelYOffset}
theme={theme}
/>
)}
</g>}
</TransitionMotion>
)
}
}
14 changes: 14 additions & 0 deletions src/components/charts/radar/Radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const Radar = ({
markersColor,
markersBorderWidth,
markersBorderColor,
enableMarkersLabel,
markersLabel,
markersLabelFormat,
markersLabelYOffset,

// theming
theme,
Expand Down Expand Up @@ -94,13 +98,19 @@ const Radar = ({
/>
{enableMarkers &&
<RadarMarkers
facets={facets}
data={data}
radiusScale={radiusScale}
angleStep={angleStep}
size={markersSize}
color={markersColor}
borderWidth={markersBorderWidth}
borderColor={markersBorderColor}
enableLabel={enableMarkersLabel}
label={markersLabel}
labelFormat={markersLabelFormat}
labelYOffset={markersLabelYOffset}
theme={theme}
{...motionProps}
/>}
</g>
Expand Down Expand Up @@ -140,6 +150,10 @@ Radar.propTypes = {
markersColor: PropTypes.any,
markersBorderWidth: PropTypes.number,
markersBorderColor: PropTypes.any,
enableMarkersLabel: PropTypes.bool,
markersLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
markersLabelFormat: PropTypes.string,
markersLabelYOffset: PropTypes.number,

// theming
theme: PropTypes.object.isRequired,
Expand Down
Loading

0 comments on commit 18c4347

Please sign in to comment.