Pie Chart

Beautiful charts. Built using chartjs and react-chartjs-2. Copy and paste into your apps or run the command into your CLI.


1. Install utilities

You need to install some necessary packages to use the below Pie Charts. Please run the command into your terminal.

bun add chart.js react-chartjs-2

2. Setup files

Create a file named as chart-utils.ts inside your lib folder and paste the code.

"use client";

import { useTheme } from "next-themes";
import { useEffect, useState } from "react";

export type ChartColors = {
  primary: string;
  secondary: string;
  tertiary: string;
  quaternary: string;
  quinary: string;
  senary: string;
  background: string;
  grid: string;
  text: string;
};

export function useChartColors(): ChartColors {
  const { theme } = useTheme();
  const [colors, setColors] = useState<ChartColors>({
    primary: "rgb(99, 102, 241)",
    secondary: "rgb(168, 85, 247)",
    tertiary: "rgb(249, 115, 22)",
    quaternary: "rgb(34, 197, 94)",
    quinary: "rgb(239, 68, 68)",
    senary: "rgb(6, 182, 212)",
    background: "rgb(255, 255, 255)",
    grid: "rgb(243, 244, 246)",
    text: "rgb(17, 24, 39)",
  });

  useEffect(() => {
    if (theme === "dark") {
      setColors({
        primary: "rgb(129, 140, 248)",
        secondary: "rgb(192, 132, 252)",
        tertiary: "rgb(251, 146, 60)",
        quaternary: "rgb(74, 222, 128)",
        quinary: "rgb(248, 113, 113)",
        senary: "rgb(34, 211, 238)",
        background: "rgb(17, 24, 39)",
        grid: "rgb(55, 65, 81)",
        text: "rgb(243, 244, 246)",
      });
    } else {
      setColors({
        primary: "rgb(99, 102, 241)",
        secondary: "rgb(168, 85, 247)",
        tertiary: "rgb(249, 115, 22)",
        quaternary: "rgb(34, 197, 94)",
        quinary: "rgb(239, 68, 68)",
        senary: "rgb(6, 182, 212)",
        background: "rgb(255, 255, 255)",
        grid: "rgb(243, 244, 246)",
        text: "rgb(17, 24, 39)",
      });
    }
  }, [theme]);

  return colors;
}

export const months = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December",
];

export const categories = [
  "Desktop",
  "Mobile",
  "Tablet",
  "Smart TV",
  "Wearables",
  "Other",
];

export const pieCategories = [
  "Product A",
  "Product B",
  "Product C",
  "Product D",
  "Product E",
];

export const generateRandomData = (count: number, max: number, min = 0) => {
  return Array.from({ length: count }, () =>
    Math.floor(Math.random() * (max - min + 1) + min)
  );
};

export const generatePieColors = (colors: ChartColors) => [
  colors.primary,
  colors.secondary,
  colors.tertiary,
  colors.quaternary,
  colors.quinary,
  colors.senary,
];

export const createGradient = (
  ctx: CanvasRenderingContext2D,
  color: string,
  opacity = 0.2
) => {
  const gradient = ctx.createLinearGradient(0, 0, 0, 400);
  gradient.addColorStop(
    0,
    color.replace("rgb", "rgba").replace(")", `, ${opacity})`)
  );
  gradient.addColorStop(1, color.replace("rgb", "rgba").replace(")", ", 0)"));
  return gradient;
};

Thats it. You are ready to go!


Separator None Pie Chart

Live Preview
Open in

Separator None Pie Chart

Label Pie Chart

Live Preview
Open in

Label Pie Chart

Legend Pie Chart

Live Preview
Open in

Legend Pie Chart

Donut Chart

Live Preview
Open in

Donut Chart

Donut Active Chart

Live Preview
Open in

Donut Active Chart

Donut with Text

Live Preview
Open in

Donut with Center Text

Interactive Pie Chart

Live Preview
Open in

Interactive Pie Chart

On this page