plouc / nivo

nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
https://nivo.rocks
MIT License
13.08k stars 1.02k forks source link

Total inside donut (pie variation) #434

Closed taschetto closed 5 years ago

taschetto commented 5 years ago

Greetings! Congratulations for this wonderful library.

Is it possible to render something inside a donut chart (pie variation)? Something like this:

https://i.stack.imgur.com/6MdV3.png

If so, any ideas on how to achive something like this?

Thanks in advance.

Eric0201 commented 5 years ago

i have same question too

plouc commented 5 years ago

Not now, but it will be available soon, using layers, a feature already implemented on other components which allow to add extra layers (or re-order existing ones).

Eric0201 commented 5 years ago

Not now, but it will be available soon, using layers, a feature already implemented on other components which allow to add extra layers (or re-order existing ones).

how to add text to center pie chart? Any way to solve it? Thanks

plouc commented 5 years ago

@Eric0201 using an extra layer when it's available :)

plouc commented 5 years ago

Otherwise you can wrap you chart inside a div and add an extra div using an higher z-index on top of the chart, but don't forget to disable events for that div though, using pointer-events: none; in order to have the tooltip working.

plouc commented 5 years ago

I've created an example of this approach, you can put whatever you want inside the extra div => https://codesandbox.io/s/w27xwy0xlk

Eric0201 commented 5 years ago

I've created an example of this approach, you can put whatever you want inside the extra div => https://codesandbox.io/s/w27xwy0xlk

thanks you very much

kaeland commented 4 years ago

@plouc Hi, this is an excellent library. Thank you for all the work you've put into making this!

Is the layered approach, mentioned above, ready for use with pie charts?

thisizkp commented 4 years ago

@kaeland I don't think it supports layers yet - https://github.com/plouc/nivo/blob/master/packages/pie/src/Pie.js#L36

wyze commented 4 years ago

@kaeland The Pie chart doesn't support layers yet.

Edit: I hadn't refreshed the page and it didn't show your reply @thisizkp, thanks for pointing to the source!

kaeland commented 4 years ago

Got it. Thank you all for such fast replies too!

thisizkp commented 4 years ago

@plouc @wyze How do we make the total render within the SVG? Is it possible now? Or do we have to wait until the layers are implemented?

wyze commented 4 years ago

Currently you would need to absolute position a div, or other element, in the middle of the chart. We haven't migrated pie chart to layers yet.

climbingcode commented 4 years ago

Is there a timeline available for layers in pie charts release?

wyze commented 3 years ago

It is available in the latest version. Here is a storybook example: https://nivo.rocks/storybook/?path=/story/pie--adding-a-metric-in-the-center-using-a-custom-layer

piehouserat commented 3 years ago

is it possible to do the same with the ResponsivePieCanvas component?

silvercondor commented 3 years ago

using the storybook example but unable to change the text color

const CenteredMetric = ({ dataWithArc, centerX, centerY }) => {
        let total = 0
        dataWithArc.forEach(datum => {
            total += datum.value
        })

        return (
            <text
                x={centerX}
                y={centerY}
                textAnchor="middle"
                dominantBaseline="central"
                style={{
                    color:"#FFFFFF",
                    fontSize: '24px',
                }}
            >
                {total}
            </text>
        )
    }

still shows as black color but fontsize adjustment works

silvercondor commented 3 years ago

using the storybook example but unable to change the text color

const CenteredMetric = ({ dataWithArc, centerX, centerY }) => {
        let total = 0
        dataWithArc.forEach(datum => {
            total += datum.value
        })

        return (
            <text
                x={centerX}
                y={centerY}
                textAnchor="middle"
                dominantBaseline="central"
                style={{color:'#FFFFFF',
                    fontSize: '24px',
                }}
            >
                {total}
            </text>
        )
    }

still shows as black color but fontsize adjustment works

Answering my own question in case anyone ends up in the same situation as me

The color should be under fill prop.

Final answer

const CenteredMetric = ({ dataWithArc, centerX, centerY }) => {
        let total = 0
        dataWithArc.forEach(datum => {
            total += datum.value
        })

        return (
            <text
                x={centerX}
                y={centerY}
                textAnchor="middle"
                dominantBaseline="central"
                fill='white'
                style={{
                    fontSize: '24px',
                }}
            >
                {total}
            </text>
        )
    }