Skip to content
This repository was archived by the owner on Sep 17, 2021. It is now read-only.
25 changes: 15 additions & 10 deletions src/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default class PieChart extends Component {
fontFamily: 'Arial',
fontSize: 14,
bold: true,
color: '#ECF0F1'
color: '#ECF0F1',
show: true,
}
},
}
Expand All @@ -67,7 +68,11 @@ export default class PieChart extends Component {

render() {
const noDataMsg = this.props.noDataMessage || 'No data available'
if (this.props.data === undefined) return (<ReactText>{noDataMsg}</ReactText>)
let noDataView = (<ReactText>{noDataMsg}</ReactText>)
if(!Array.isArray(this.props.data)) return noDataView
let accessor = this.props.accessor || identity(this.props.accessorKey)
let data = this.props.data.filter((item) => accessor(item) > 0)
if(!data.length) return noDataView

let options = new Options(this.props)

Expand All @@ -88,31 +93,31 @@ export default class PieChart extends Component {
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)
data,
accessor
})

let textStyle = fontAdapt(options.label)

let slices

if (this.props.data.length === 1) {
let item = this.props.data[0]
if (data.length === 1) {
let item = data[0]
let outerFill = (item.color && Colors.string(item.color)) || this.color(0)
let innerFill = this.props.monoItemInnerFillColor || '#fff'
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}/>
<Text fontFamily={textStyle.fontFamily}
{textStyle.show ? <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>
y={y - R + ((R-r)/2)}>{item.name}</Text> : null}
</G>
)
}
Expand All @@ -124,14 +129,14 @@ export default class PieChart extends Component {
<G key={ i } x={x} y={y}>
<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}
{textStyle.show ? <Text fontFamily={textStyle.fontFamily}
fontSize={textStyle.fontSize}
fontWeight={textStyle.fontWeight}
fontStyle={textStyle.fontStyle}
fill={textStyle.fill}
textAnchor="middle"
x={c.sector.centroid[0]}
y={c.sector.centroid[1]}>{ c.item.name }</Text>
y={c.sector.centroid[1]}>{ c.item.name }</Text> : null}
</G>
</G>
)
Expand Down