File size: 11,861 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{
  "name": "donut-chart",
  "snippet": null,
  "examples": [
    {
      "name": "donut-chart-basic",
      "content": "\"use client\"\nexport const DonutChartBasic = () => {\n  const chart = useChart({\n    data: [\n      { name: \"windows\", value: 400, color: \"blue.solid\" },\n      { name: \"mac\", value: 300, color: \"orange.solid\" },\n      { name: \"linux\", value: 300, color: \"pink.solid\" },\n      { name: \"other\", value: 200, color: \"green.solid\" },\n    ],\n  })\n\n  return (\n    <Chart.Root boxSize=\"200px\" chart={chart} mx=\"auto\">\n      <PieChart>\n        <Tooltip\n          cursor={false}\n          animationDuration={100}\n          content={<Chart.Tooltip hideLabel />}\n        />\n        <Pie\n          innerRadius={80}\n          outerRadius={100}\n          isAnimationActive={false}\n          data={chart.data}\n          dataKey={chart.key(\"value\")}\n          nameKey=\"name\"\n        >\n          {chart.data.map((item) => {\n            return <Cell key={item.name} fill={chart.color(item.color)} />\n          })}\n        </Pie>\n      </PieChart>\n    </Chart.Root>\n  )\n}\n",
      "hasSnippet": false,
      "importPaths": [
        "import { Chart, useChart } from \"@chakra-ui/charts\"",
        "import { Cell, Pie, PieChart, Tooltip } from \"recharts\""
      ],
      "importPath": "import { DonutChart } from \"@chakra-ui/react\"",
      "npmDependencies": [
        "@chakra-ui/charts",
        "recharts"
      ]
    },
    {
      "name": "donut-chart-with-angle-padding",
      "content": "\"use client\"\nexport const DonutChartWithAnglePadding = () => {\n  const chart = useChart({\n    data: [\n      { name: \"windows\", value: 400, color: \"blue.solid\" },\n      { name: \"mac\", value: 300, color: \"orange.solid\" },\n      { name: \"linux\", value: 300, color: \"pink.solid\" },\n    ],\n  })\n\n  return (\n    <Chart.Root boxSize=\"200px\" chart={chart} mx=\"auto\">\n      <PieChart>\n        <Tooltip\n          cursor={false}\n          animationDuration={100}\n          content={<Chart.Tooltip hideLabel />}\n        />\n        <Pie\n          innerRadius={80}\n          outerRadius={100}\n          isAnimationActive={false}\n          data={chart.data}\n          dataKey={chart.key(\"value\")}\n          paddingAngle={8}\n          cornerRadius={4}\n        >\n          {chart.data.map((item) => (\n            <Cell key={item.name} fill={chart.color(item.color)} />\n          ))}\n        </Pie>\n      </PieChart>\n    </Chart.Root>\n  )\n}\n",
      "hasSnippet": false,
      "importPaths": [
        "import { Chart, useChart } from \"@chakra-ui/charts\"",
        "import { Cell, Pie, PieChart, Tooltip } from \"recharts\""
      ],
      "importPath": "import { DonutChart } from \"@chakra-ui/react\"",
      "npmDependencies": [
        "@chakra-ui/charts",
        "recharts"
      ]
    },
    {
      "name": "donut-chart-with-centered-text",
      "content": "\"use client\"\nexport const DonutChartWithCenteredText = () => {\n  const chart = useChart({\n    data: [\n      { name: \"windows\", value: 400, color: \"blue.solid\" },\n      { name: \"mac\", value: 300, color: \"orange.solid\" },\n      { name: \"linux\", value: 300, color: \"pink.solid\" },\n      { name: \"other\", value: 200, color: \"green.solid\" },\n    ],\n  })\n\n  return (\n    <Chart.Root boxSize=\"200px\" chart={chart} mx=\"auto\">\n      <PieChart>\n        <Tooltip\n          cursor={false}\n          animationDuration={100}\n          content={<Chart.Tooltip hideLabel />}\n        />\n        <Pie\n          innerRadius={80}\n          outerRadius={100}\n          isAnimationActive={false}\n          data={chart.data}\n          dataKey={chart.key(\"value\")}\n          nameKey=\"name\"\n        >\n          <Label\n            content={({ viewBox }) => (\n              <Chart.RadialText\n                viewBox={viewBox}\n                title={chart.getTotal(\"value\").toLocaleString()}\n                description=\"users\"\n              />\n            )}\n          />\n          {chart.data.map((item) => (\n            <Cell key={item.color} fill={chart.color(item.color)} />\n          ))}\n        </Pie>\n      </PieChart>\n    </Chart.Root>\n  )\n}\n",
      "hasSnippet": false,
      "importPaths": [
        "import { Chart, useChart } from \"@chakra-ui/charts\"",
        "import { Cell, Label, Pie, PieChart, Tooltip } from \"recharts\""
      ],
      "importPath": "import { DonutChart } from \"@chakra-ui/react\"",
      "npmDependencies": [
        "@chakra-ui/charts",
        "recharts"
      ]
    },
    {
      "name": "donut-chart-with-detached-segment",
      "content": "\"use client\"\nexport const DonutChartWithDetachedSegment = () => {\n  const chart = useChart({\n    data: [\n      { name: \"windows\", value: 400, color: \"blue.solid\" },\n      { name: \"mac\", value: 300, color: \"orange.solid\" },\n      { name: \"linux\", value: 300, color: \"pink.solid\" },\n      { name: \"other\", value: 200, color: \"green.solid\" },\n    ],\n  })\n\n  return (\n    <Chart.Root boxSize=\"200px\" chart={chart} mx=\"auto\">\n      <PieChart>\n        <Tooltip\n          cursor={false}\n          animationDuration={100}\n          content={<Chart.Tooltip hideLabel />}\n        />\n        <Pie\n          innerRadius={60}\n          outerRadius={100}\n          isAnimationActive={false}\n          data={chart.data}\n          dataKey={chart.key(\"value\")}\n          nameKey={chart.key(\"name\")}\n          activeIndex={0}\n          activeShape={<Sector outerRadius={120} />}\n          strokeWidth={5}\n        >\n          {chart.data.map((item) => (\n            <Cell key={item.name} fill={chart.color(item.color)} />\n          ))}\n        </Pie>\n      </PieChart>\n    </Chart.Root>\n  )\n}\n",
      "hasSnippet": false,
      "importPaths": [
        "import { Chart, useChart } from \"@chakra-ui/charts\"",
        "import { Cell, Pie, PieChart, Sector, Tooltip } from \"recharts\""
      ],
      "importPath": "import { DonutChart } from \"@chakra-ui/react\"",
      "npmDependencies": [
        "@chakra-ui/charts",
        "recharts"
      ]
    },
    {
      "name": "donut-chart-with-other-label",
      "content": "\"use client\"\n\ninterface DataItem {\n  name: string\n  value: number\n  color: string\n}\n\nconst rawData: DataItem[] = [\n  { name: \"windows\", value: 400, color: \"blue.solid\" },\n  { name: \"mac\", value: 300, color: \"orange.solid\" },\n  { name: \"linux\", value: 150, color: \"pink.solid\" },\n  { name: \"chrome\", value: 80, color: \"purple.solid\" },\n  { name: \"firefox\", value: 60, color: \"red.solid\" },\n  { name: \"safari\", value: 40, color: \"yellow.solid\" },\n  { name: \"edge\", value: 30, color: \"cyan.solid\" },\n  { name: \"opera\", value: 20, color: \"teal.solid\" },\n]\n\nconst threshold = 100\n\n// Group items with value < 100 into \"Other\"\nconst data = rawData.reduce<DataItem[]>((acc, item) => {\n  if (item.value >= threshold) {\n    acc.push(item)\n  } else {\n    const otherIndex = acc.findIndex((i) => i.name === \"Other\")\n    if (otherIndex === -1) {\n      acc.push({ name: \"Other\", value: item.value, color: \"gray.emphasized\" })\n    } else {\n      acc[otherIndex].value += item.value\n    }\n  }\n  return acc\n}, [])\n\nexport const DonutChartWithOtherLabel = () => {\n  const chart = useChart({ data: data })\n\n  const label = (entry: DataItem) => {\n    const percent = chart.getValuePercent(\"value\", entry.value)\n    return `${entry.name} (${percent.toFixed(1)}%)`\n  }\n\n  return (\n    <Chart.Root aspectRatio=\"square\" maxW=\"sm\" chart={chart} mx=\"auto\">\n      <PieChart>\n        <Tooltip\n          cursor={false}\n          animationDuration={100}\n          content={<Chart.Tooltip hideLabel />}\n        />\n        <Pie\n          innerRadius={60}\n          outerRadius={100}\n          isAnimationActive={false}\n          data={chart.data}\n          dataKey={chart.key(\"value\")}\n          nameKey={chart.key(\"name\")}\n          label={label}\n          labelLine={{ strokeWidth: 1 }}\n        >\n          {chart.data.map((item) => (\n            <Cell key={item.name} fill={chart.color(item.color)} />\n          ))}\n        </Pie>\n      </PieChart>\n    </Chart.Root>\n  )\n}\n",
      "hasSnippet": false,
      "importPaths": [
        "import { Chart, useChart } from \"@chakra-ui/charts\"",
        "import { Cell, Pie, PieChart, Tooltip } from \"recharts\""
      ],
      "importPath": "import { DonutChart } from \"@chakra-ui/react\"",
      "npmDependencies": [
        "@chakra-ui/charts",
        "recharts"
      ]
    },
    {
      "name": "donut-chart-with-point-label",
      "content": "\"use client\"\nexport const DonutChartWithPointLabel = () => {\n  const chart = useChart({\n    data: [\n      { name: \"windows\", value: 400, color: \"blue.solid\" },\n      { name: \"mac\", value: 300, color: \"orange.solid\" },\n      { name: \"linux\", value: 300, color: \"pink.solid\" },\n      { name: \"other\", value: 200, color: \"green.solid\" },\n    ],\n  })\n\n  return (\n    <Chart.Root boxSize=\"200px\" chart={chart} mx=\"auto\">\n      <PieChart margin={{ left: 40 }}>\n        <Tooltip\n          cursor={false}\n          animationDuration={100}\n          content={<Chart.Tooltip hideLabel />}\n        />\n        <Pie\n          innerRadius={80}\n          outerRadius={100}\n          isAnimationActive={false}\n          data={chart.data}\n          dataKey={chart.key(\"value\")}\n          nameKey=\"name\"\n          labelLine={{ strokeWidth: 1 }}\n          label={{\n            fill: chart.color(\"fg.muted\"),\n          }}\n        >\n          {chart.data.map((item) => (\n            <Cell\n              key={item.name}\n              strokeWidth={2}\n              fill={chart.color(item.color)}\n            />\n          ))}\n        </Pie>\n      </PieChart>\n    </Chart.Root>\n  )\n}\n",
      "hasSnippet": false,
      "importPaths": [
        "import { Chart, useChart } from \"@chakra-ui/charts\"",
        "import { Cell, Pie, PieChart, Tooltip } from \"recharts\""
      ],
      "importPath": "import { DonutChart } from \"@chakra-ui/react\"",
      "npmDependencies": [
        "@chakra-ui/charts",
        "recharts"
      ]
    },
    {
      "name": "donut-chart-with-start-and-end-angle",
      "content": "\"use client\"\nexport const DonutChartWithStartAndEndAngle = () => {\n  const chart = useChart({\n    data: [\n      { name: \"windows\", value: 400, color: \"blue.solid\" },\n      { name: \"mac\", value: 300, color: \"orange.solid\" },\n      { name: \"linux\", value: 300, color: \"pink.solid\" },\n      { name: \"other\", value: 200, color: \"green.solid\" },\n    ],\n  })\n\n  return (\n    <Chart.Root boxSize=\"200px\" chart={chart} mx=\"auto\">\n      <PieChart>\n        <Tooltip\n          cursor={false}\n          animationDuration={100}\n          content={<Chart.Tooltip hideLabel />}\n        />\n        <Pie\n          innerRadius={60}\n          outerRadius={100}\n          isAnimationActive={false}\n          data={chart.data}\n          dataKey={chart.key(\"value\")}\n          nameKey=\"name\"\n          startAngle={180}\n          endAngle={0}\n        >\n          {chart.data.map((item) => (\n            <Cell key={item.name} fill={chart.color(item.color)} />\n          ))}\n        </Pie>\n      </PieChart>\n    </Chart.Root>\n  )\n}\n",
      "hasSnippet": false,
      "importPaths": [
        "import { Chart, useChart } from \"@chakra-ui/charts\"",
        "import { Cell, Pie, PieChart, Tooltip } from \"recharts\""
      ],
      "importPath": "import { DonutChart } from \"@chakra-ui/react\"",
      "npmDependencies": [
        "@chakra-ui/charts",
        "recharts"
      ]
    }
  ]
}