graphic icon indicating copy to clipboard operation
graphic copied to clipboard

Pie Chart, how to add a gap between each section?

Open lsaudon opened this issue 7 months ago • 1 comments

I'd like there to be spaces between each value.

I try with size in IntervalMark but nothing change

Actual Expected
Pie Chart Pie Chart

lsaudon avatar Jun 12 '25 10:06 lsaudon

To create a gap between the sections of a pie chart using the Flutter graphic library, you need to employ a workaround by styling the border of each pie slice. The library, at present, does not offer a direct gap or spacing property for its pie chart implementation.

The solution involves using the style property of the IntervalElement. By setting the strokeWidth to a desired gap size and the strokeColor to the chart's background color, you can effectively create the illusion of a space between each section.

Implementation

Here is a code example demonstrating how to add a 2-pixel white gap between the sections of a pie chart:

import 'package:flutter/material.dart';
import 'package:graphic/graphic.dart';

class PieChartWithGaps extends StatelessWidget {
  final List<Map<String, dynamic>> data = [
    {'category': 'A', 'value': 20},
    {'category': 'B', 'value': 30},
    {'category': 'C', 'value': 40},
    {'category': 'D', 'value': 10},
  ];

  @override
  Widget build(BuildContext context) {
    return Chart(
      data: data,
      variables: {
        'category': Variable(
          accessor: (Map map) => map['category'] as String,
        ),
        'value': Variable(
          accessor: (Map map) => map['value'] as num,
        ),
      },
      transforms: [
        Proportion(
          variable: 'value',
          as: 'percent',
        ),
      ],
      coord: PolarCoord(
        transposed: true,
        dimCount: 1,
      ),
      elements: [
        IntervalElement(
          variable: 'percent',
          color: ColorAttr(
            variable: 'category',
            values: [
              Colors.blue,
              Colors.green,
              Colors.orange,
              Colors.red,
            ],
          ),
          // Add a border to simulate a gap
          style: PaintStyle(
            strokeColor: Colors.white, // Color of the gap (usually background)
            strokeWidth: 2,           // Width of the gap
          ),
        ),
      ],
    );
  }
}

Key Concepts

  • IntervalElement: In the graphic library, a pie chart is fundamentally an IntervalElement in a PolarCoord (polar coordinate system).
  • style Property: This property of the IntervalElement allows you to customize the painting style of the chart elements. It accepts a PaintStyle object.
  • PaintStyle: This is where you define visual properties like strokeColor and strokeWidth.
    • strokeColor: Set this to the color of your chart's background to create the appearance of an empty space.
    • strokeWidth: This determines the thickness of the "gap".

By applying a stroke to each pie section that matches the background, you create a visual separation, achieving the desired gap effect in a clean and efficient manner.

This solution was generated by an AI. I would appreciate any feedback on whether it effectively solved your problem!

For more detailed information, you can always refer to the official Graphic documentation. Good luck with your project!

entronad avatar Jun 14 '25 06:06 entronad