Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add linechart change animation (#117) #239

Open
wants to merge 1 commit into
base: new-version
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions Sources/SwiftUICharts/Base/Animatable/AnimatableVector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import SwiftUI
import enum Accelerate.vDSP

struct AnimatableVector: VectorArithmetic {
static var zero = AnimatableVector(values: [0.0])

static func + (lhs: AnimatableVector, rhs: AnimatableVector) -> AnimatableVector {
let count = min(lhs.values.count, rhs.values.count)
return AnimatableVector(values: vDSP.add(lhs.values[0..<count], rhs.values[0..<count]))
}

static func += (lhs: inout AnimatableVector, rhs: AnimatableVector) {
let count = min(lhs.values.count, rhs.values.count)
vDSP.add(lhs.values[0..<count], rhs.values[0..<count], result: &lhs.values[0..<count])
}

static func - (lhs: AnimatableVector, rhs: AnimatableVector) -> AnimatableVector {
let count = min(lhs.values.count, rhs.values.count)
return AnimatableVector(values: vDSP.subtract(lhs.values[0..<count], rhs.values[0..<count]))
}

static func -= (lhs: inout AnimatableVector, rhs: AnimatableVector) {
let count = min(lhs.values.count, rhs.values.count)
vDSP.subtract(lhs.values[0..<count], rhs.values[0..<count], result: &lhs.values[0..<count])
}

var values: [Double]

mutating func scale(by rhs: Double) {
values = vDSP.multiply(rhs, values)
}

var magnitudeSquared: Double {
vDSP.sum(vDSP.multiply(values, values))
}
}
1 change: 1 addition & 0 deletions Sources/SwiftUICharts/Charts/LineChart/Line.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public struct Line: View {
LineBackgroundShapeView(chartData: chartData,
geometry: geometry,
style: style)
.animation(.easeIn)
}
LineShapeView(chartData: chartData,
geometry: geometry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import SwiftUI

struct LineBackgroundShape: Shape {
struct LineBackgroundShape: Shape, Animatable {
var data: [Double]

var animatableData: AnimatableVector {
get { AnimatableVector(values: data) }
set { data = newValue.values }
}

func path(in rect: CGRect) -> Path {
let path = Path.quadClosedCurvedPathWithPoints(points: data, step: CGPoint(x: 1.0, y: 1.0))
return path
Expand Down
8 changes: 7 additions & 1 deletion Sources/SwiftUICharts/Charts/LineChart/LineShape.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import SwiftUI

struct LineShape: Shape {
struct LineShape: Shape, Animatable {
var data: [Double]

var animatableData: AnimatableVector {
get { AnimatableVector(values: data) }
set { data = newValue.values }
}

func path(in rect: CGRect) -> Path {
let path = Path.quadCurvedPathWithPoints(points: data, step: CGPoint(x: 1.0, y: 1.0))
return path
Expand Down