Skip to content

Commit

Permalink
feat(legends): add support for custom symbol shape
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Aug 25, 2018
1 parent 953c572 commit 7419c91
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/legends/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export const legendEffectPropType = PropTypes.shape({
})

export const symbolPropTypes = {
symbolShape: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
symbolSize: PropTypes.number,
symbolSpacing: PropTypes.number,
symbolShape: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
symbolBorderWidth: PropTypes.number,
symbolBorderColor: PropTypes.string,
}
Expand Down
4 changes: 2 additions & 2 deletions packages/legends/src/svg/BoxLegendSvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const BoxLegendSvg = ({
itemOpacity,

// symbol
symbolShape,
symbolSize,
symbolSpacing,
symbolShape,
symbolBorderWidth,
symbolBorderColor,

Expand Down Expand Up @@ -100,9 +100,9 @@ const BoxLegendSvg = ({
itemBackground={itemBackground}
itemOpacity={itemOpacity}
// symbol
symbolShape={symbolShape}
symbolSize={symbolSize}
symbolSpacing={symbolSpacing}
symbolShape={symbolShape}
symbolBorderWidth={symbolBorderWidth}
symbolBorderColor={symbolBorderColor}
// interactivity
Expand Down
4 changes: 2 additions & 2 deletions packages/legends/src/svg/LegendSvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ const LegendSvg = ({
itemBackground,
itemOpacity,
// symbol
symbolShape,
symbolSize,
symbolSpacing,
symbolShape,
symbolBorderWidth,
symbolBorderColor,
// interactivity
Expand Down Expand Up @@ -85,9 +85,9 @@ const LegendSvg = ({
background={itemBackground}
opacity={itemOpacity}
// symbol
symbolShape={symbolShape}
symbolSize={symbolSize}
symbolSpacing={symbolSpacing}
symbolShape={symbolShape}
symbolBorderWidth={symbolBorderWidth}
symbolBorderColor={symbolBorderColor}
// interactivity
Expand Down
31 changes: 18 additions & 13 deletions packages/legends/src/svg/LegendSvgItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { isFunction } from 'lodash'
import {
DIRECTION_LEFT_TO_RIGHT,
DIRECTION_RIGHT_TO_LEFT,
Expand Down Expand Up @@ -63,9 +64,9 @@ export default class LegendSvgItem extends Component {
opacity: 1,

// symbol
symbolShape: 'square',
symbolSize: 16,
symbolSpacing: 8,
symbolShape: 'square',
symbolBorderWidth: 0,
symbolBorderColor: 'transparent',

Expand Down Expand Up @@ -134,9 +135,9 @@ export default class LegendSvgItem extends Component {
background,
opacity,
// symbol
symbolShape,
symbolSize,
symbolSpacing,
symbolShape,
symbolBorderWidth,
symbolBorderColor,
// interactivity
Expand All @@ -161,7 +162,12 @@ export default class LegendSvgItem extends Component {
handler => handler !== undefined
)

const Symbol = symbolByShape[symbolShape]
let Symbol
if (isFunction(symbolShape)) {
Symbol = symbolShape
} else {
Symbol = symbolByShape[symbolShape]
}

return (
<g
Expand All @@ -181,18 +187,17 @@ export default class LegendSvgItem extends Component {
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
/>
<Symbol
x={symbolX}
y={symbolY}
size={style.symbolSize || symbolSize}
fill={data.fill}
borderWidth={
{React.createElement(Symbol, {
x: symbolX,
y: symbolY,
size: style.symbolSize || symbolSize,
fill: data.fill,
borderWidth:
style.symbolBorderWidth !== undefined
? style.symbolBorderWidth
: symbolBorderWidth
}
borderColor={style.symbolBorderColor || symbolBorderColor}
/>
: symbolBorderWidth,
borderColor: style.symbolBorderColor || symbolBorderColor,
})}
<text
textAnchor={labelAnchor}
style={{
Expand Down

0 comments on commit 7419c91

Please sign in to comment.