Color palette
The color palette system allows you to customize the colors used throughout the chart interface. To provide custom colors to the color palette, supply a ColorPaletteProvider via DXCResourceManager.
ColorPaletteProvider
The ColorPaletteProvider protocol defines the interface for managing a collection of colors that can be dynamically added or removed. This provider uses Combine publishers to notify the chart when the palette changes.
Note: ColorPalette uses CGColor, which restricts the use of dynamic colors.
/// Provides a shared palette of colors used by the chart.////// The provider exposes a Combine publisher with the current palette and/// subsequent updates as colors are added or removed.////// Direction: Consumed by the chart module; methods are called from the chart side./// Accent: Keep the palette concise and stable to avoid noisy UI updates.public protocol ColorPaletteProvider {/// A publisher that emits the current list of palette colors and any updates.////// The publisher should deliver values on the main thread since changes/// typically drive UI updates.////// Direction: Observed by chart components./// Accent: Emit only when the effective palette actually changes.var palette: AnyPublisher<[CGColor], Never> { get }/// Adds a color to the palette.////// Implementations may ignore duplicates.////// Direction: Invoked by the chart when user selects or needs a new color./// Accent: Consider normalizing input (e.g., color space) for equality checks.func addColor(_ color: CGColor)/// Removes a color from the palette.////// If the color is not present, implementations may no-op.////// Direction: Invoked by the chart when a color is deselected or no longer needed./// Accent: Debounce cascaded updates if multiple removals happen in quick succession.func removeColor(_ color: CGColor)}
How to provide your own ColorPaletteProvider
This section shows how to assign your custom ColorPaletteProvider implementation to the chart's resource manager, making it available for all chart instances.
// Set it for the chartsDXChartApp.resourceManager.colorPaletteProvider = <#Your Custom ColorPaletteProvider#>
Example implementation
This example shows a demo implementation of ColorPaletteProvider that maintains an in-memory color palette.
import Combineimport UIKitfinal class CustomColorPaletteProvider: ColorPaletteProvider {private let storage: StorageManager = UserDefaults.standardprivate var defaultColors: [CGColor] {DXChartApp.resourceManager.defaultPaletteColors}private lazy var _palette: CurrentValueSubject<[CGColor], Never> = .init(colorsPalette)var palette: AnyPublisher<[CGColor], Never> {_palette.eraseToAnyPublisher()}func addColor(_ color: CGColor) {colorsPalette.append(color)_palette.send(colorsPalette)}func removeColor(_ color: CGColor) {colorsPalette.removeAll { $0 == color }_palette.send(colorsPalette)}var colorsPalette: [CGColor] {get {do {let chartColors = try storage.getObject(forKey: "colorPalette", castTo: [ChartColor].self)return chartColors.map(\.cgColor)} catch {return defaultColors}}set {do {let chartColors = newValue.map { ChartColor(cgColor: $0) }try storage.setObject(chartColors, forKey: "colorPalette")} catch {debugPrint("can't save object (error)")}}}}// Set it for the chartsDXChartApp.resourceManager.colorPaletteProvider = CustomColorPaletteProvider()
Notes
Important considerations when implementing and using the color palette system
- The provider you supply will be used for all chart instances.
- The color palette will use the same colors for dark and light themes.