Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
Fix pie chart centering (#138)
Browse files Browse the repository at this point in the history
* Fix pie chart centering

* Create actual pie chart only if we have more than 1 data object
  • Loading branch information
dgladkov authored and marzolfb committed Apr 12, 2017
1 parent 0c4b51f commit 1c34ca4
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export default class PieChart extends Component {

let options = new Options(this.props)

let x = (options.chartWidth / 2) - options.margin.left
let y = (options.chartHeight / 2) - options.margin.top
let x = (options.chartWidth / 2) - (options.margin.left || 0)
let y = (options.chartHeight / 2) - (options.margin.top || 0)

let radius = Math.min(x, y)

Expand All @@ -84,13 +84,7 @@ export default class PieChart extends Component {
R = (R || (this.props.options && this.props.options.R))
R = (R || radius)

let chart = Pie({
center: this.props.center || (this.props.options && this.props.options.center) || [0,0] ,
r,
R,
data: this.props.data,
accessor: this.props.accessor || identity(this.props.accessorKey)
})
let [centerX, centerY] = this.props.center || (this.props.options && this.props.options.center) || [x, y]

let textStyle = fontAdapt(options.label)

Expand All @@ -103,25 +97,32 @@ export default class PieChart extends Component {
let stroke = typeof fill === 'string' ? outerFill : Colors.darkenColor(outerFill)
slices = (
<G>
<Circle r={R} cx={x} cy={y} stroke={stroke} fill={outerFill}/>
<Circle r={r} cx={x} cy={y} stroke={stroke} fill={innerFill}/>
<Circle r={R} cx={centerX} cy={centerY} stroke={stroke} fill={outerFill}/>
<Circle r={r} cx={centerX} cy={centerY} stroke={stroke} fill={innerFill}/>
<Text fontFamily={textStyle.fontFamily}
fontSize={textStyle.fontSize}
fontWeight={textStyle.fontWeight}
fontStyle={textStyle.fontStyle}
fill={textStyle.fill}
textAnchor="middle"
x={x}
y={y - R + ((R-r)/2)}>{item.name}</Text>
x={centerX}
y={centerY - R + ((R-r)/2)}>{item.name}</Text>
</G>
)
}
else {
} else {
let chart = Pie({
center: [centerX, centerY],
r,
R,
data: this.props.data,
accessor: this.props.accessor || identity(this.props.accessorKey)
})

slices = chart.curves.map( (c, i) => {
let fill = (c.item.color && Colors.string(c.item.color)) || this.color(i)
let stroke = typeof fill === 'string' ? fill : Colors.darkenColor(fill)
return (
<G key={ i } x={x} y={y}>
<G key={ i }>
<Path d={c.sector.path.print() } stroke={stroke} fill={fill} fillOpacity={1} />
<G x={options.margin.left} y={options.margin.top}>
<Text fontFamily={textStyle.fontFamily}
Expand Down

0 comments on commit 1c34ca4

Please sign in to comment.