Pie Chart

<wa-pie-chart> Stable Since 3.3 Pro Included with Web Awesome Pro

Pie charts show the proportional composition of a whole as slices of a circle. They work best with a small number of categories where the relative proportions matter more than exact values.

Get Pie Chart with Web Awesome Pro! Subscribing to Web Awesome Pro gives you every Pro component, plus premium themes, color tools, team collaboration, and more.

For a variation with a hollow center, see Doughnut Chart.

<wa-pie-chart id="pie-hero" label="Browser Market Share" description="A pie chart showing browser market share with Chrome leading at 65%">
</wa-pie-chart>
<script type="module">
  const chart = document.querySelector('#pie-hero');

  chart.config = {
    data: {
      labels: ['Chrome', 'Safari', 'Firefox', 'Edge', 'Other'],
      datasets: [{
        label: 'Market Share',
        data: [65, 18, 8, 5, 4]
      }]
    }
  };
</script>

For advanced configuration such as custom plugins and direct Chart.js access, see <wa-chart>.

Examples

Link to This Section

Providing Data with JavaScript

Link to This Section

For dynamic data, set the config property directly. The chart will re-render automatically.

<wa-pie-chart id="pie-js-example" label="Budget Allocation" description="A pie chart of budget allocation">
</wa-pie-chart>
<script type="module">
  const chart = document.querySelector('#pie-js-example');

  chart.config = {
    data: {
      labels: ['Engineering', 'Marketing', 'Sales', 'Operations'],
      datasets: [{
        label: 'Budget',
        data: [40, 25, 20, 15]
      }]
    }
  };
</script>

Note that config is shallowly reactive. If you mutate the existing object in place, you must reassign it to trigger a re-render!

Providing Data with JSON

Link to This Section

Place a <script type="application/json"> tag inside the component with your chart data. Each value in the data array corresponds to a label.

<wa-pie-chart label="Budget Allocation" description="A pie chart showing how a budget is allocated across departments">
  <script type="application/json">
    {
      "data": {
        "labels": ["Engineering", "Marketing", "Sales", "Operations"],
        "datasets": [{
          "label": "Budget",
          "data": [40, 25, 20, 15]
        }]
      }
    }
  </script>
</wa-pie-chart>

Custom Slice Colors

Link to This Section

Override the default color palette using the --fill-color-* and --border-color-* CSS custom properties to apply custom colors to each slice.

<wa-pie-chart
  id="pie-colors"
  label="Custom Colors"
  description="A pie chart with custom slice colors"
  style="
    --fill-color-1: color-mix(in srgb, var(--wa-color-blue-60) 70%, transparent);
    --border-color-1: var(--wa-color-blue-60);
    --fill-color-2: color-mix(in srgb, var(--wa-color-cyan-60) 70%, transparent);
    --border-color-2: var(--wa-color-cyan-60);
    --fill-color-3: color-mix(in srgb, var(--wa-color-purple-60) 70%, transparent);
    --border-color-3: var(--wa-color-purple-60);
  "
>
</wa-pie-chart>
<script type="module">
  const chart = document.querySelector('#pie-colors');

  chart.config = {
    data: {
      labels: ['Desktop', 'Mobile', 'Tablet'],
      datasets: [{
        label: 'Traffic',
        data: [55, 35, 10]
      }]
    }
  };
</script>

Legend

Link to This Section

Use the legend-position attribute to control where the legend appears. For pie charts, placing the legend on the side can help prevent overlap. Add without-legend to hide it entirely.

<wa-pie-chart id="pie-legend" legend-position="right" label="Legend on Right" description="A pie chart with the legend on the right side">
</wa-pie-chart>
<script type="module">
  const chart = document.querySelector('#pie-legend');

  chart.config = {
    data: {
      labels: ['Rent', 'Food', 'Transport', 'Entertainment', 'Savings'],
      datasets: [{
        label: 'Monthly Spending',
        data: [35, 25, 15, 10, 15]
      }]
    }
  };
</script>

Disabling Tooltips

Link to This Section

Use the without-tooltip attribute to hide the tooltips that appear when hovering over slices.

<wa-pie-chart id="pie-tooltip" without-tooltip label="No Tooltips" description="A pie chart with tooltips disabled">
</wa-pie-chart>
<script type="module">
  const chart = document.querySelector('#pie-tooltip');

  chart.config = {
    data: {
      labels: ['A', 'B', 'C'],
      datasets: [{
        label: 'Values',
        data: [40, 35, 25]
      }]
    }
  };
</script>

Disabling Animations

Link to This Section

Use the without-animation attribute to disable chart transitions.

<wa-pie-chart id="pie-anim" without-animation label="No Animation" description="A pie chart with animation disabled">
</wa-pie-chart>
<script type="module">
  const chart = document.querySelector('#pie-anim');

  chart.config = {
    data: {
      labels: ['A', 'B', 'C'],
      datasets: [{
        label: 'Values',
        data: [40, 35, 25]
      }]
    }
  };
</script>

Importing

Link to This Section

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.

CDN npm Self-Hosted React

Import this component directly from the CDN:

import 'https://ka-f.webawesome.com/[email protected]/components/pie-chart/pie-chart.js';

After installing Web Awesome via npm, import this component:

import '@awesome.me/webawesome/dist/components/pie-chart/pie-chart.js';

If you're self-hosting Web Awesome, import this component from your server:

import './webawesome/dist/components/pie-chart/pie-chart.js';

To import this component for React 18 or below, use the following code:

import WaPieChart from '@awesome.me/webawesome/dist/react/pie-chart/index.js';

Slots

Link to This Section

Learn more about using slots.

Attributes & Properties

Link to This Section

Learn more about attributes and properties.

CSS custom properties

Link to This Section

Learn more about CSS custom properties.

Need a hand? Report a bug Ask for help
    No results
    Navigate Enter Select Esc Close