Skip to content

Commit 723218b

Browse files
committed
Minor addition to GraphUtils
Modified 1 file: src/Element/ui/other/graph/GraphUtils.swift
1 parent 2c105a1 commit 723218b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Element/ui/other/graph/GraphUtils.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@ class GraphUtils{
55
struct GraphConfig{
66
var size:CGSize, position:CGPoint, spacing:CGSize, vValues:[CGFloat], maxValue:CGFloat, leftMargin:CGFloat, topMargin:CGFloat
77
}
8+
/**
9+
* New
10+
* Returns graph points (Basically the coordinates of where to place the visual graph points)
11+
*/
12+
static func points2(rect:CGRect, spacing:CGSize, vValues:[CGFloat]) -> [CGPoint]{
13+
guard let maxValue:CGFloat = vValues.max() else {fatalError("err: \(vValues.count)")}/*Finds the largest number in among vValues*/
14+
let x:CGFloat = rect.x//spacing.width
15+
let y:CGFloat = rect.height - (rect.y)//the y point to start from, basically bottom
16+
let h:CGFloat = rect.height - (rect.y)//the height to work within
17+
return vValues.indices.reduce([]) { (acc:[CGPoint],i:Int) in
18+
let p:CGPoint = {
19+
let value:CGFloat = vValues[i]
20+
let ratio:CGFloat = value/maxValue/*a value between 0-1*/
21+
//ratio = ratio.isNaN ? 0 : ratio//cases can be
22+
//Swift.print("ratio: " + "\(ratio)")
23+
let dist:CGFloat = h*ratio
24+
let x:CGFloat = x + (i * spacing.width)
25+
let y:CGFloat = y - dist
26+
let _y:CGFloat = y.isNaN ? rect.height - rect.y : y//⚠️️ quick fix, for when vValue is 0
27+
return CGPoint(x,_y)
28+
}()
29+
return acc + [p]
30+
}
31+
}
832
/**
933
* Returns graph points (Basically the coordinates of where to place the visual graph points)
1034
* NOTE: ⚠️️ Modulates the vValues to fit a predefined rect.

0 commit comments

Comments
 (0)