Skip to content

Commit

Permalink
feat(pie): restore border for PieCanvas
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Nov 4, 2020
1 parent 789d52a commit eab1311
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/pie/src/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
useTheme,
useDimensions,
} from '@nivo/core'
import { getInheritedColorGenerator } from '@nivo/colors'
import { getInheritedColorGenerator, useInheritedColor } from '@nivo/colors'
import { PieSvgDefaultProps, PieSvgPropTypes } from './props'
import PieSlice from './PieSlice'
import PieRadialLabels from './PieRadialLabels'
Expand Down Expand Up @@ -113,7 +113,7 @@ const Pie = ({

const getSliceLabel = useMemo(() => getLabelGenerator(sliceLabel), [sliceLabel])

const borderColor = getInheritedColorGenerator(_borderColor, theme)
const borderColor = useInheritedColor(_borderColor, theme)

const boundDefs = bindDefs(defs, dataWithArc, fill)

Expand Down
22 changes: 18 additions & 4 deletions packages/pie/src/PieCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import React, { useEffect, useRef } from 'react'
import { useDimensions, useTheme, withContainer } from '@nivo/core'
import { renderLegendToCanvas } from '@nivo/legends'
import { useInheritedColor } from '@nivo/colors'
import { PieSvgDefaultProps, PieSvgPropTypes } from './props'
import { useNormalizedData, usePieFromBox } from './hooks'
import { drawSliceLabels, drawRadialLabels } from './canvas'

const PieCanvas = ({
data,
Expand All @@ -29,6 +31,8 @@ const PieCanvas = ({
innerRadius: innerRadiusRatio,
cornerRadius,
colors,
borderColor,
borderWidth,
legends,
isInteractive,
}) => {
Expand Down Expand Up @@ -62,6 +66,8 @@ const PieCanvas = ({
cornerRadius,
})

const getBorderColor = useInheritedColor(borderColor, theme)

useEffect(() => {
canvasEl.current.width = outerWidth * pixelRatio
canvasEl.current.height = outerHeight * pixelRatio
Expand All @@ -84,15 +90,22 @@ const PieCanvas = ({
dataWithArc.forEach(arc => {
ctx.beginPath()
ctx.fillStyle = arc.color
//this.ctx.strokeStyle = getBorderColor({ ...arc.data, color: arc.color })
//this.ctx.lineWidth = borderWidth

ctx.strokeStyle = getBorderColor(arc)
ctx.lineWidth = borderWidth

arcGenerator(arc.arc)

ctx.fill()
//if (borderWidth > 0) this.ctx.stroke()

if (borderWidth > 0) {
ctx.stroke()
}
})

// legends assume a box rather than a center,
// that's why we restore previously saved position here.
ctx.restore()

legends.forEach(legend => {
renderLegendToCanvas(ctx, {
...legend,
Expand All @@ -117,6 +130,7 @@ const PieCanvas = ({
dataWithArc,
legends,
theme,
getBorderColor,
])

return (
Expand Down
17 changes: 0 additions & 17 deletions packages/pie/src/PieCanvasRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { getHoveredArc, getRelativeCursor, getLabelGenerator, Container } from '@nivo/core'
import { getInheritedColorGenerator } from '@nivo/colors'
import { renderLegendToCanvas } from '@nivo/legends'
import { arcPropType } from './props'
import { drawSliceLabels, drawRadialLabels } from './canvas'
import PieTooltip from './PieTooltip'
Expand Down Expand Up @@ -198,22 +197,6 @@ class PieCanvasRenderer extends Component {
theme,
})
}

this.ctx.restore()

legends.forEach(legend => {
renderLegendToCanvas(this.ctx, {
...legend,
data: arcs.map(arc => ({
id: arc.data.id,
label: arc.data.id,
color: arc.color,
})),
containerWidth: width,
containerHeight: height,
theme,
})
})
}

render() {
Expand Down
1 change: 0 additions & 1 deletion packages/pie/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export const PiePropTypes = {
.isRequired,
})
).isRequired,
//boundDefs: PropTypes.array.isRequired, // computed

// interactivity
isInteractive: PropTypes.bool,
Expand Down

0 comments on commit eab1311

Please sign in to comment.