I would like to have different fill colors in my chart. What I want to achieve is that if the values drop below 0 the fill color should be red. If they are above the fill color should be red. My code looks as follows:
import SwiftUI
import Charts
struct DataPoint: Identifiable {
let id: UUID = UUID()
let x: Int
let y: Int
}
struct AlternatingChartView: View {
enum Gradients {
static let greenGradient = LinearGradient(gradient: Gradient(colors: [.green, .white]), startPoint: .top, endPoint: .bottom)
static let blueGradient = LinearGradient(gradient: Gradient(colors: [.white, .blue]), startPoint: .top, endPoint: .bottom)
}
let data: [DataPoint] = [
DataPoint(x: 1, y: 10),
DataPoint(x: 2, y: -5),
DataPoint(x: 3, y: 20),
DataPoint(x: 4, y: -8),
DataPoint(x: 5, y: 15),
]
var body: some View {
Chart {
ForEach(data) { data in
AreaMark(
x: .value("Data Point", data.x),
y: .value("amount", data.y))
.interpolationMethod(.catmullRom)
.foregroundStyle(data.y < 0 ? Color.red : Color.green)
LineMark(
x: .value("Data Point", data.x),
y: .value("amount", data.y))
.interpolationMethod(.catmullRom)
.foregroundStyle(Color.black)
.lineStyle(StrokeStyle.init(lineWidth: 4))
}
}
.frame(height: 200)
}
}
#Preview {
AlternatingChartView()
}
The result looks like this:
I also tried using
foregroundStyle(by:)
and
chartForegroundStyleScale(_:)
but the result was, that two separate areas had been drawn. One for the below and one for the above zero datapoints.
So, what would be the right approach to have two different fill colors?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi,
I'm try to convert from CoreData to SwiftData.
Problem is that I'm using inheritance in CoreData.
So, I have a base class "animal" and "cat" and "bird" inherit from it.
I can have a array of type "animal" which contains both cats and dogs.
Now as I try to move to SwiftData I tried the following approach
protocol Animal {
var name: String { get set }
}
@Model
class Cat: Animal {
var name: String
var legs: Int
}
@Model
class Bird: Animal {
var name: String
var legs: Int
var wings: Int
}
@Model
class Enviroment {
//leads to error:
//Type 'any Animal' cannot conform to 'PersistentModel'
var animals: [any Animal]
}
So how to I get around of this? I would like to store multiple types of animals (classes that conform to protocol animal) … but how can I achieve that?
Thanks
Hi,
is there a way to different swift packages for different build configuration?
I.e. when I do Release-Builds I'd like to use MyPackage and for debug builds I'd like to use MyPackage-Debug.
My first thought was that I could handle this in the Package.swift but .target does not support configurations in .when but only plattforms.
So is there any way to use different packages depending on configuration?
Hi,
I have a strange problem when drawing NSAttributedStrings in SwiftUI. Compared to render it in AppKit the SwiftUI characters are 1pt smaller.
To display the string in SwiftUI I have to use NSViewRepresentable but it does not matter if I render the view in draw() of NSView or use a NSTextField to display it. I have to use NSViewRepresentable as I also have to use CMYK-Colors.
The font used is "The Sans Mono-W8 ExtraBold".
Can anyone point my for a hint? Or is this a bug?
I try to use some CMYK-Colours with SwiftUI, the view is exported to a PDF.
The problem is that after the export, the colours are no longer correct and it seems that SwiftUI transforms them in to sRGB.
I have a pretty complex and dynamic SwiftUI View which wrapps around multiple other Views which have different dynamic contents (images, texts …)
But I also need those views as a thumbnail with a fixed height.
I.e. something like a magazine where a page is displayed and the other pages are displayed in a thumbnail bar at the bottom of the screen.
The thumbnails should be presented in a UICollectionView (the rest of the App is still UIKit!)
I tried to scale down the UIView (coming from UIHostingController) using Autolayout but had no success with that.
My next approach would be creating preview images but to render and SwiftUI-View as image I need it to be displayed on the screen … but how to do that?
Thank you very much in advance