Black Friday deal: 30% off our developer tools, books and apps! Learn more ...Black Friday deal:30% off our tools, books and apps >>

Distinctive patterns for categorical data in Swift Charts

Color is commonly used to distinguish categories in a chart, but we each perceive color differently. Two colors that appear clearly distinct to one person may look almost identical to another, making the categories they represent hard to tell apart. In this post, we'll make such charts readable regardless of how color is perceived. We'll programmatically create repeating patterns to fill bars in Swift Charts, and show them when the user turns on Differentiate Without Color, which SwiftUI exposes through the accessibilityDifferentiateWithoutColor environment value.

Monthly household energy use shown as stacked bars colored by source: grid, battery, and solar, with the same bars below as they appear with protanopia, deuteranopia and tritanopia simulated. Monthly household energy use shown as stacked bars colored by source: grid, battery, and solar, with the same bars below as they appear with protanopia, deuteranopia and tritanopia simulated.

To check how different users may perceive the chart, we can simulate color vision differences and compare the resulting contrast between its colors. I like to use Sim Daltonism, a macOS app that applies these simulations to content on the screen.

In the simulated views, some of the chart's colors become much harder to tell apart. A distinctive pattern alongside color can give users another way to distinguish the categories. Swift Charts accepts any ShapeStyle as a foreground style, including one that repeats an image across a shape. This means we can provide a pattern by drawing a small image and tiling it across each mark.

let size = CGSize(width: 8, height: 8)

let image = Image(size: size) { context in
    context.fill(
        Path(CGRect(origin: .zero, size: size)),
        with: .style(base)
    )
    context.blendMode = .multiply
    let ink = GraphicsContext.Shading.color(
        .black.opacity(0.3)
    )

    var stripes = Path()
    for offset in [-size.width, 0, size.width] {
        stripes.move(
            to: CGPoint(
                x: offset - size.width,
                y: size.height * 2
            )
        )
        stripes.addLine(
            to: CGPoint(
                x: offset + size.width * 2,
                y: -size.height
            )
        )
    }
    context.stroke(stripes, with: ink, lineWidth: 2)
}

The Image(size:label:opaque:colorMode:renderer:) initializer gives us a graphics context to draw into, allowing us to build the tile image programmatically without shipping any image assets. We start by filling an 8-point square with base, the style the category would use without a pattern, such as its color. Then we add diagonal strokes using partially transparent black and the multiply blend mode to darken the color beneath them.

Yellow rounded rectangle filled with continuous diagonal stripes. Yellow rounded rectangle filled with continuous diagonal stripes.

The loop draws the same diagonal three times, offset by one tile width each time. The middle line runs from corner to corner of the tile, and the other two are the matching diagonals of the neighboring tiles, which cover the parts of the stroke that fall into the tile's opposite corners. Each line also extends beyond the tile, so no line ends inside it, and the stripes join across the tile edges to form continuous lines across the shape.

Each category needs a pattern of its own. Since the image repeats, the drawing code only has to produce a single tile of each pattern, and it can switch over the pattern assigned to the category to choose what to draw.

let image = Image(size: size) { context in
    // ... Base fill and ink ...

    switch pattern {
    case .diagonalStripes:
        // ... Diagonal stripe path ...
        context.stroke(stripes, with: ink, lineWidth: 2)
    case .dots:
        let dot = CGRect(x: 2.5, y: 2.5, width: 3, height: 3)
        context.fill(Path(ellipseIn: dot), with: ink)
    }
}

For the dot pattern, we fill a single 3-point circle at the center of the tile. Centering it leaves equal space on all sides, so repeating the image produces a regular grid of evenly spaced dots.

Green rounded rectangle filled with a repeating dot pattern. Green rounded rectangle filled with a repeating dot pattern.

The dots give us a texture distinct from the continuous stripes. Another useful pattern is a checkerboard, formed by drawing two squares in opposite corners of the tile.

let image = Image(size: size) { context in
    // ... Base fill and ink ...

    switch pattern {
    // ... Diagonal stripes and dots ...
    case .checkerboard:
        let half = size.width / 2
        var squares = Path()
        squares.addRect(
            CGRect(x: 0, y: 0, width: half, height: half)
        )
        squares.addRect(
            CGRect(x: half, y: half, width: half, height: half)
        )
        context.fill(squares, with: ink)
    }
}

Each square has half the tile's width and height, so it occupies one quarter of the image. The first starts at the origin, while the second is offset by half the tile size along both axes. When the tile repeats, these darker squares alternate with the untouched base color to form the checkerboard.

Indigo rounded rectangle filled with a checkerboard pattern. Indigo rounded rectangle filled with a checkerboard pattern.

We now have three distinct patterns ready to apply to our chart.

Swift Charts Beyond the Basics by Natalia Panferova and Matthaus Woolard book coverSwift Charts Beyond the Basics by Natalia Panferova and Matthaus Woolard book cover

Black Friday 2026 offer: 30% off!$35$25

Build clear, beautiful, and interactive visualizations with Swift Charts

Swift Charts Beyond the Basicsby Natalia Panferova and Matthaus Woolard

  • Reveal patterns and relationships in complex app data
  • Create custom charts that stay responsive and accessible
Black Friday 2026 offer: 30% off!

Build clear, beautiful, and interactive visualizations with Swift Charts

Swift Charts Beyond the Basics by Natalia Panferova and Matthaus Woolard book coverSwift Charts Beyond the Basics by Natalia Panferova and Matthaus Woolard book cover

Swift Charts Beyond the Basics

by Natalia Panferova
and Matthaus Woolard

$35$25

The drawing code lives in a PatternFill structure, which stores the base style and the pattern to draw. Its patternPaint property wraps the tile in ImagePaint, a style that fills a shape by repeating an image.

struct PatternFill<Base: ShapeStyle> {
    enum Pattern {
        case checkerboard, dots, diagonalStripes
    }

    let base: Base
    let pattern: Pattern

    var patternPaint: ImagePaint {
        // ... Tile drawing with switch pattern ...
        return ImagePaint(image: image)
    }
}

extension ShapeStyle {
    var checkerboard: PatternFill<Self> {
        PatternFill(base: self, pattern: .checkerboard)
    }
    // ... dotted and striped ...
}

The extension gives every shape style, including each color, a .checkerboard, .dotted or .striped property. The chart then passes each fill's patternPaint to its foreground style scale.

Chart(usage) { entry in
    // ... Bar marks colored by source ...
}
.chartForegroundStyleScale(
    domain: ["Grid", "Battery", "Solar"],
    range: [
        Color.indigo.checkerboard.patternPaint,
        Color.green.dotted.patternPaint,
        Color.yellow.striped.patternPaint
    ]
)
Monthly household energy use with checkerboard grid bars, dotted battery bars, striped solar bars, and a matching legend. Monthly household energy use with checkerboard grid bars, dotted battery bars, striped solar bars, and a matching legend.

Each category keeps its original color and gains its own pattern. Because the patterns are supplied through the chart's foreground style scale, the legend symbols pick them up too, matching the fills in the bars.

So far, the chart shows the patterns for every user. To show them only when Differentiate Without Color is turned on, the fill needs to read the accessibilityDifferentiateWithoutColor value from the environment it's resolved in.

extension PatternFill: ShapeStyle {
    func resolve(
        in environment: EnvironmentValues
    ) -> some ShapeStyle {
        guard environment.accessibilityDifferentiateWithoutColor else {
            return AnyShapeStyle(base)
        }

        return AnyShapeStyle(patternPaint)
    }
}

Rather than changing the styles we pass to the chart when the accessibility setting changes, we can make PatternFill conform to ShapeStyle so the style itself responds to the environment. Its resolve(in:) method returns the pattern paint when the setting is enabled and the base style otherwise, allowing the chart to use Color.indigo.checkerboard directly.

Chart(usage) { entry in
    // ... Bar marks colored by source ...
}
.chartForegroundStyleScale(
    domain: ["Grid", "Battery", "Solar"],
    range: [
        Color.indigo.checkerboard,
        Color.green.dotted,
        Color.yellow.striped
    ]
)
Monthly household energy use shown as stacked bars with a checkerboard for grid, dots for battery and stripes for solar, with the same bars below as they appear with protanopia, deuteranopia and tritanopia simulated. Monthly household energy use shown as stacked bars with a checkerboard for grid, dots for battery and stripes for solar, with the same bars below as they appear with protanopia, deuteranopia and tritanopia simulated.

With Differentiate Without Color turned on, each category stays distinguishable in the simulated views, even where its color looks almost identical to another, while the color mapping remains the same for every user.

You can find the sample code for this post on GitHub.


If you are looking to deepen your understanding of Swift Charts and learn how to reason about your data and turn it into beautiful, performant, and accessible charts, take a look at our new book Swift Charts Beyond the Basics. It is a rich, practical reference for building advanced data visualizations with the framework. And if you are working with a coding agent, our Mac app SwiftFairy can review the chart code your agent writes. Its Performant Swift Charts guidance flags code that makes charts slow to render, scroll, or respond to selection, right on your Mac.

For more resources on Swift and SwiftUI, check out our other books and book bundles.

We are currently running a Black Friday promotion with 30% off SwiftFairy, our Swift and SwiftUI books and bundles, and Unlimited plans for our EPUB and PDF reader Whio. Visit the Black Friday page to learn more.

SwiftFairy

Black Friday 2026 offer: 30% off!

On-device Swift and SwiftUI reviews with fix guidance

  • Proper SwiftUI
  • SwiftUI Layout & Resizability
  • Performant Swift Charts
  • Modern Swift