Giter VIP home page Giter VIP logo

react-svg-piechart's Introduction

react-svg-piechart

npm package Travis Codecov Module formats

A lightweight responsive React pie chart component using only SVG

Getting started

react-svg-piechart

You can download react-svg-piechart from the NPM registry via the npm or yarn commands

yarn add react-svg-piechart
npm install react-svg-piechart --save

If you don't use package manager and you want to include react-svg-piechart directly in your html, you could get it from the UNPKG CDN

https://unpkg.com/react-svg-piechart/umd/react-svg-piechart.js

Demo

See Demo page

Usage

import React from "react"
import ReactSvgPieChart from "react-svg-piechart"

const data = [
  {title: "Data 1", value: 100, color: "#22594e"},
  {title: "Data 2", value: 60, color: "#2f7d6d"},
  {title: "Data 3", value: 30, color: "#3da18d"},
  {title: "Data 4", value: 20, color: "#69c2b0"},
  {title: "Data 5", value: 10, color: "#a1d9ce"},
]

const MyCompo = () => (
  <ReactSvgPieChart
    data={data}
    // If you need expand on hover (or touch) effect
    expandOnHover
    // If you need custom behavior when sector is hovered (or touched)
    onSectorHover={(d, i, e) => {
      if (d) {
        console.log("Mouse enter - Index:", i, "Data:", d, "Event:", e)
      } else {
        console.log("Mouse leave - Index:", i, "Event:", e)
      }
    }}
  />
)

Props

Name PropType Description Default
data Array of data Objects One data is {value: number (required), color: string, title: string, expanded: bool, href: string} []
expandedIndex Number Pass in an index to manually control the expanded sector of the pie
expandOnHover Boolean Active hover and touch (mobile) effects false
onSectorHover Function Callback when one sector is hovered or touched (mobile) - ex: (data, index, event) => {} null
expandSize Number expand size, in pixels. Used if expandOnHover is active or one data has expanded attribute set to true
strokeColor String Sector stroke color "#fff"
startAngle Number Angle to start drawing sectors from measured clockwise from the x-axis 0
angleMargin Number Angle of margin between sectors 0
strokeLinejoin String Sector stroke line join (One of miter, round, bevel) "round"
strokeWidth Number Sector width, in pixels (0 to disable stroke) 1
viewBoxSize Number SVG viewbox width and height 100
transitionDuration String CSS property for transition-duration, set to 0s to disable transition "0s"
transitionTimingFunction String CSS Property for transition-timing-function "ease-out"

Contributing

  • ⇄ Pull/Merge requests and ★ Stars are always welcome.
  • For bugs and feature requests, please create an issue.
  • Pull requests must be accompanied by passing automated tests (npm test).

See CONTRIBUTING.md guidelines

Changelog

See changelog

License

This project is licensed under the MIT License - see the LICENCE.md file for details

react-svg-piechart's People

Contributors

cedricdelpoux avatar dependabot[bot] avatar entozoon avatar haargeeel avatar johnrees avatar mb21 avatar terinjokes avatar thiagoterleski avatar tnormington avatar whamtet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-svg-piechart's Issues

the legend doesn't get the 'bold' font-weight when pie-chart is hovered

Hi,

thanks for the early fix.

  1. One more thing: the legend doesn't get the 'bold' font-weight when pie-chart is hovered.
    Update :
<div className="portlet light col-md-4 dashboard-pie">
                <div className="pie-legend">
                {data.map(({label, value, color}, i) => (
                        <div key={i}>
                            <span style={{"background": color}}></span>
                            <span style={{"fontWeight": this.state.expandedSector === i ? "bold" : null, "color": color}}>
                                {label} : {value}
                            </span>
                        </div>
                    ))}
                </div>
                <div className="pie-chart">
                    <PieChart
                        data= {data}
                        expandedSector={expandedSector}
                        onSectorHover={this.handleMouseEnterOnSector}
                        sectorStrokeWidth={2}
                        expandOnHover
                        shrinkOnTouchEnd
                    />
                </div>
            </div>

removing <span style={{"background": color}}></span> from the above fixes the issue 1

  1. Also still getting this error in console:
Error: <path> attribute d: Expected number, "… A150,150 0 1,1 NaN,NaN z".
setValueForProperty @ DOMPropertyOperations.js:141
Error: <path> attribute d: Expected number, "M150,150 LNaN,NaN A150,150…".
setValueForProperty @ DOMPropertyOperations.js:141

many thanks

Error in path attribute

Iam getting this error ..

Error: attribute d: Expected number, "… A150,150 0 1,1 NaN,NaN z".

Allow Sector colour change

Could you add a way to change colors of the rendered sectors? This will pass a better informed message.

Chart isn't responsive

•Facebook : 100
•Twitter : 60
•Google Plus : 1
•Pinterest : 2
•Linked In : 1

Run chart with that parameters. You'll see chart isn't responsive... Component can't draw adequately with other parameters.
Chunk with the biggest value drown with small width.

Change the overall size in an html page

1、my code set style height={300}, but it has no effect , how do i set to change the whole pie size ,
while viewBoxSize don't change 's height .
`<PieChart
data={Piedata}
// If you need expand on hover (or touch) effect
expandOnHover
expandSize={1}
// If you need custom behavior when sector is hovered (or touched)
viewBoxSize={15}
strokeWidth={1}
onSectorHover={(d, i, e) => {
if (d) {
console.log("Mouse enter - Index:", i, "Data:", d, "Event:", e)
} else {
console.log("Mouse leave - Index:", i, "Event:", e)
}
}}
style={{height: 300}}

/>
`

Peer dependency with react

We got following issue when we were trying to build our app on Altus.
npm WARN [email protected] requires a peer of react@~15.3.0 but none was installed.

Is it possible we make the peer dependency for this plug more flexible? like
"peerDependencies": {
"react": "^15.3.0"
},

The wrong color is used when one of the values is 0 - using ver 2.0.2

We are passing in 2 values in data: a 0 and a 1. We get the full circle drawn as expected but it is drawn in the color for the 1st value which is 0 and not for the second value which is 1. What we see and the Props from the React debug tool:

image

Props
data: Array[2]
0:{…}
color: "#2ecc71" <--- green color
value: 0
1:{…}
color: "#3498db" <--- blue color
value: 1

expandOnHover: false
expandSize: 5
onSectorHover: null
sectorStrokeWidth: 1
shrinkOnTouchEnd: false
strokeColor: "#fff"
strokeLinejoin: "round"
strokeWidth: 1
viewBoxSize: 100

Change radius

how to change radius of the pie without changing the distance between arcs of the pie

Add touch events for iOS

Hi,
I realised that the onSectorHover doesn't work for iOS touch gestures. But there is more than one way to implement it and I'm not sure how optional you would want to add this feature.

  1. Just adding onTouchStart
        return (
          <path
            key={'sector' + i}
            d={dPath}
            fill={d.color || palette[i % palette.length]}
            stroke='#fff'
            strokeWidth={sectorStrokeWidth}
            onTouchStart={() => this.handleSectorHover(i)}
            // onTouchEnd for only expanding while touching?
            onMouseEnter={() => this.handleSectorHover(i)}
            onMouseLeave={() => this.handleSectorHover(null)}
          />
        )
  1. Make it optional:
<PieChart
  data={ data }
  expandedSector={expandedSector}
  onSectorHover={this.handleMouseEnterOnSector}
  sectorStrokeWidth={2}
  expandOnHover
  expandOnTouch // optional
/>

You library is cool because it's very lightweight so I did not want to add big conditions (2. solution) in the code if you like the first approach more.

Cheers

display title on area

Currently, the title is visible only when the section is hovered, could there be added a prop that would let the title be displayed in the data section all the time.

Chart is incomplete

I am using version 2.1.0.

This is my code:

<ReactSvgPieChart data={[{"title":"a","value":0.3,"color":"#123456"},{"title":"b","value":0.4,"color":"#654321"}]} />

And this is the result:

It seems that this happens when there is more than one section and at least one of the values is less than 1.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.