Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import './App.css';
  3. import {PieChart, Pie, Sector} from 'recharts'
  4.  
  5. const data = [
  6. { name: 'Group A', value: 400 },
  7. { name: 'Group B', value: 300 },
  8. { name: 'Group C', value: 300 },
  9. { name: 'Group D', value: 200 },
  10. ];
  11.  
  12. const renderActiveShape = (props) => {
  13. const RADIAN = Math.PI / 180;
  14. const {
  15. cx, cy, midAngle, innerRadius, outerRadius, startAngle, endAngle,
  16. fill, payload,
  17. } = props;
  18. const sin = Math.sin(-RADIAN * midAngle);
  19. const cos = Math.cos(-RADIAN * midAngle);
  20. const mx = cx + (innerRadius - 18) * cos;
  21. const my = cy + (innerRadius - 18) * sin;
  22. const ex = mx + (cos >= 0 ? 1 : -1) * 10;
  23. const ey = my;
  24. return (
  25. <g>
  26. <text x={cx} y={cy} dy={8} textAnchor="middle" fill={fill}>{payload.name}</text>
  27. <Sector
  28. cx={cx}
  29. cy={cy}
  30. innerRadius={innerRadius}
  31. outerRadius={outerRadius}
  32. startAngle={startAngle}
  33. endAngle={endAngle}
  34. fill={fill}
  35. />
  36. <g transform={`translate(${ex + (cos >= 0 ? 1 : -1) * 2} ${ey})`}>
  37. <g transform={`rotate(${-midAngle + 180})`}>
  38. {/* Draw the triangle with custom shape */}
  39. <polygon points="5,-5 5,5 -5,0 " style={{fill:'lime', stroke:'purple', strokeWidth:'1' }}/>
  40. </g>
  41. </g>
  42. </g>
  43. );
  44. };
  45.  
  46. class PieCustom extends Component{
  47. state = {
  48. activeIndex: 0,
  49. };
  50.  
  51. onPieEnter = (data, index) => {
  52. this.setState({
  53. activeIndex: index,
  54. });
  55. };
  56.  
  57. render(){
  58. return (
  59. <div>
  60. <PieChart width={400} height={400}>
  61. <Pie
  62. activeIndex={this.state.activeIndex}
  63. activeShape={renderActiveShape}
  64. data={data}
  65. cx={200}
  66. cy={200}
  67. innerRadius={60}
  68. outerRadius={80}
  69. fill="#8884d8"
  70. dataKey="value"
  71. onMouseEnter={this.onPieEnter}
  72. />
  73. </PieChart>
  74. </div>
  75. );
  76. }
  77. }
  78.  
  79.  
  80. function App() {
  81. return <PieCustom />
  82. }
  83.  
  84. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement