I'm importing SwiftUI, Foundation and Charts into an iOS app I'm writing in Swift in Xcode Playgrounds and am getting this error:
error: Couldn't look up symbols: protocol witness table for Foundation.Date : Charts.Plottable in Charts
the code looks like this in just two example files:
file 1, the view
import Foundation
import SwiftUI
import Charts
import PlaygroundSupport
struct FirstChart_WithDates: View {
private let data = ChartDateAndDoubleModel.mockData(months: 3)
var body: some View {
Chart(data) { item in
BarMark(
x: .value("Label", item.date, unit: .month),
y: .value("Value", item.value)
)
}
.padding()
.aspectRatio(1, contentMode: .fit)
.dynamicTypeSize(.accessibility1)
ChartDateAndDoubleModelView(data: data)
}
}
struct ChartDateAndDoubleModelView: View {
var data: [ChartDateAndDoubleModel]
var body: some View {
VStack {
HeaderRowView(texts: ["date", "value"])
ForEach(data) { datum in
HStack {
Text(datum.date.formatted(date: .abbreviated, time: .omitted))
.frame(maxWidth: .infinity)
// TODO: Format for 2 decimal places.
Text(datum.value, format: .number.precision(.fractionLength(2)))
.frame(maxWidth: .infinity)
}
}
}
.padding(.bottom, 8)
.overlay(.quaternary, in: .rect(cornerRadius: 8).stroke())
.padding()
}
}
struct HeaderRowView: View {
var texts: [String]
var body: some View {
HStack(spacing: 2.0) {
ForEach(texts, id: \.self) { text in
Text(text)
.padding(4)
.frame(maxWidth: .infinity)
.background(.quaternary)
}
}
.font(.headline)
.mask(UnevenRoundedRectangle(topLeadingRadius: 8, topTrailingRadius: 8))
}
}
and file 2, the model:
import Foundation
import SwiftUI
import Charts
// ChartDateAndDoubleModel.swift
//
//
// Created by Michael Isbell on 2/10/26.
//
public struct ChartDateAndDoubleModel: Identifiable {
public let id = UUID()
public let date: Date
public let value: Double
}
public extension ChartDateAndDoubleModel {
static func mockData(months: Int = 5) -> [ChartDateAndDoubleModel] {
var data: [ChartDateAndDoubleModel] = []
let calendar = Calendar(identifier: .gregorian)
let currentDate = Date()
for month in 0..<months {
//add month to current date and append to data
data.append(
ChartDateAndDoubleModel(
date: calendar.date(byAdding: .month, value: month, to: currentDate)!,
value: Double.random(in: 0...1000)
)
)
}
return data
}
}