Guest User

Untitled

a guest
Nov 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. import React from "react";
  2. import { Pie } from "@vx/shape";
  3. import { Group } from "@vx/group";
  4. import { scaleOrdinal } from "@vx/scale";
  5. import { LegendOrdinal } from "@vx/legend";
  6. //import data from "./CategoryByDay.json";
  7.  
  8. const data = [
  9. {
  10. "Category": "Category Name 1",
  11. "Total": 643401
  12. },
  13. {
  14. "Category": "Category Name 2",
  15. "Total": 1062500
  16. },
  17. {
  18. "Category": "Category Name 3",
  19. "Total": 670000
  20. }
  21. ]
  22.  
  23. function Label({ x, y, children }) {
  24. return (
  25. <text fill="black" textAnchor="middle" x={x} y={y} dy=".33em" fontSize={14}>
  26. {children}
  27. </text>
  28. );
  29. }
  30.  
  31. export default ({
  32. title,
  33. width,
  34. height,
  35. events = false,
  36. margin = {
  37. top: 30,
  38. left: 20,
  39. right: 20,
  40. bottom: 30,
  41. },
  42. }) => {
  43. if (width < 10) return null;
  44. const radius = Math.min(width, height) / 2;
  45.  
  46. const color = scaleOrdinal({
  47. domain: data.Category,
  48. range: [
  49. "#87cefa",
  50. "#d4ea62",
  51. "#d56464",
  52. ],
  53. });
  54.  
  55. return (
  56. <div>
  57. <h4>{title}</h4>
  58. <svg width={width} height={height}>
  59. <Group top={radius} left={width / 2}>
  60. <Pie
  61. data={data}
  62. pieValue={d => d.Total}
  63. outerRadius={radius - radius / 10}
  64. innerRadius={radius - radius / 1.8}
  65. fill={function(d) {
  66. return color(d.index);
  67. }}
  68. cornerRadius={0}
  69. padAngle={0}
  70. centroid={(centroid, arc) => {
  71. const [x, y] = centroid;
  72. const { startAngle, endAngle } = arc;
  73. if (endAngle - startAngle < 0.1) return null;
  74. return (
  75. <Label fontSize={10} x={x} y={y}>
  76. {arc.data.Category}
  77. </Label>
  78. );
  79. }}
  80. />
  81. </Group>
  82. </svg>
  83. <div
  84. style={{
  85. fontSize: '14px',
  86. }}
  87. >
  88. <LegendOrdinal
  89. scale={color}
  90. direction="column"
  91. labelMargin="0"
  92. labelFormat={label => `${label}`}
  93.  
  94. />
  95. </div>
  96. </div>
  97. );
  98. };
Add Comment
Please, Sign In to add comment