A SwiftUI wrapper for Chart.js that allows you to easily create beautiful, responsive charts in your iOS and macOS applications.
- Create various chart types supported by Chart.js (bar, line, pie, doughnut, etc.)
- Fully customizable with Chart.js configuration options
- Works on iOS and macOS (fallback message for tvOS and watchOS)
- SwiftUI integration with
Charts
view - Responsive design that adapts to different screen sizes
- iOS 13.0+ /
macOS 10.15+(Coming Soon) - Swift 5.9+
- Xcode 16.0+
Add this package to your project by adding the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/1998code/ChartJS-for-Swift", from: "1.0.0")
]
Or add it directly from Xcode:
- File > Swift Packages > Add Package Dependency
- Enter the repository URL:
https://github.com/1998code/ChartJS-for-Swift
import SwiftUI
import ChartJS
struct ContentView: View {
let dataJson = """
{
"labels": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"datasets": [{
"label": "Sales 2025",
"data": [12, 19, 3, 5, 2, 3],
"backgroundColor": "rgba(75, 192, 192, 0.2)",
"borderColor": "rgba(75, 192, 192, 1)",
"borderWidth": 1
}]
}
"""
let configJson = """
{
"type": "bar",
"options": {
"responsive": true,
"plugins": {
"title": {
"display": true,
"text": "Monthly Sales"
}
},
"scales": {
"y": {
"beginAtZero": true
}
}
}
}
"""
var body: some View {
VStack {
Charts(dataJson: dataJson, configJson: configJson)
.frame(height: 300)
.padding()
Text("Monthly Sales Chart")
.font(.caption)
}
}
}
You can create different chart types by changing the type
property in the configuration:
// Line chart
let lineConfigJson = """
{
"type": "line",
"options": {
"responsive": true,
"plugins": {
"title": {
"display": true,
"text": "Line Chart"
}
}
}
}
"""
// Pie chart
let pieConfigJson = """
{
"type": "pie",
"options": {
"responsive": true,
"plugins": {
"title": {
"display": true,
"text": "Pie Chart"
}
}
}
}
"""
For the best responsive experience, use GeometryReader:
GeometryReader { geometry in
Charts(dataJson: dataJson, configJson: configJson)
.frame(height: geometry.size.width > geometry.size.height ?
geometry.size.height : // landscape
geometry.size.height * 0.3) // portrait
.padding()
}
If you need to add custom JavaScript initialization, use the scriptSetup
parameter:
let scriptSetup = """
<script>
// Custom JavaScript to run before chart initialization
function customFunction() {
console.log('Custom setup complete');
}
customFunction();
</script>
"""
Charts(dataJson: dataJson, configJson: configJson, scriptSetup: scriptSetup)
- iOS: Fully supported
- macOS: Coming Soon
- tvOS: (WebKit not supported)
- watchOS: (WebKit not supported)
If you encounter any issues:
- Make sure your JSON is valid
- Check that your data structure matches what Chart.js expects
- For complex charts, test your configuration on the Chart.js website first
This project is available under the MIT license. See the LICENSE file for more info.
- Chart.js - Simple yet flexible JavaScript charting library
This doc is also available in:
English | 繁中 / 简中 / 粵語 | 日本語 | Español
Please feel free to open a pull request and add new language(s) or fix any typos/mistakes.