GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Swift Chart - BarMark Gantt vertical steps

Forums > SwiftUI

Hi,

I've created a horizontal BarMark chart to serve as a simplistic gantt. The issue I have is that the bars don't stack vertically when I have a task for the same team member (in the same date period), instead they overlap.

The code I've used to initialise the BarMark:

BarMark(
    xStart: .value("Start Date", plan.start),
    xEnd: .value("End Date", plan.end),
    y: .value("Person", plan.demandName),
    height: .fixed(25)
)

The way the chart works is that on the left-most column I have the people who are completing the work, and the bar marks represent the specific work itself (i.e. specific days scheduled to complete a task/project).

I've tried using .position but it doesn't quite work. Any ideas on how to force overlapping bars to step down on the vertical axis? Or perhaps an alternative approach, if anyone has achieved similar.

   

To create a vertical Gantt chart with steps in Swift using Swift Charts, you can use BarMark to represent each task or step. Here’s a simple example:

swift Copy code import SwiftUI import Charts

struct GanttChartView: View { var data: [(String, Date, Date)] = [ ("Task 1", Date(), Calendar.current.date(byAdding: .day, value: 2, to: Date())!), ("Task 2", Calendar.current.date(byAdding: .day, value: 1, to: Date())!, Calendar.current.date(byAdding: .day, value: 3, to: Date())!), ("Task 3", Calendar.current.date(byAdding: .day, value: 3, to: Date())!, Calendar.current.date(byAdding: .day, value: 5, to: Date())!) ]

var body: some View {
    Chart(data, id: \.0) { task in
        BarMark(
            xStart: .value("Start", task.1),
            xEnd: .value("End", task.2),
            y: .value("Task", task.0)
        )
        .clipShape(RoundedRectangle(cornerRadius: 5))
    }
    .chartYAxis {
        AxisMarks(preset: .aligned)
    }
    .frame(height: 300)
    .padding()
}

}

struct GanttChartView_Previews: PreviewProvider { static var previews: some View { GanttChartView() } } This example uses dummy data for simplicity. Customize the data and chart properties as needed to fit your specific use case.

   

Thanks, but if it was as easy as asking ChatGPT I wouldn't be here...

   

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.