Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
494 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// MiniBarChart.swift | ||
// react-native-simple-grid | ||
// | ||
// Created by Vittorio Cellucci on 2022-08-03. | ||
// | ||
|
||
import Foundation | ||
class MiniBarChart : MiniChartRenderer { | ||
|
||
override func render(_ ctx: CGContext, rect: CGRect) { | ||
guard let data = data else {return} | ||
guard let rows = data.qMatrix else {return} | ||
if (rect.size.height == 0) {return} | ||
ctx.clear(rect) | ||
getBandWidth(rect: rect, data: data) | ||
getScale(rect:rect, data:data) | ||
var x = padding + horizontalPadding / 2; | ||
var index = 0 | ||
for row in rows { | ||
mainColor.set() | ||
let value = row[1].qNum ?? 1.0 | ||
let height = value * scale; | ||
let y = rect.height - height - verticalPadding / 2 | ||
let rect = CGRect(x: x, y: y, width: bandWidth, height:height) | ||
setColor(index, value: value, count: rows.count) | ||
ctx.fill(rect) | ||
x += padding * 2 + bandWidth | ||
index += 1 | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// MiniChart.swift | ||
// react-native-simple-grid | ||
// | ||
// Created by Vittorio Cellucci on 2022-08-03. | ||
// | ||
|
||
import Foundation | ||
class MiniChartRenderer { | ||
var data: Matrix? | ||
var representation: Representation? | ||
var firstColor = UIColor.systemBlue | ||
var lastColor = UIColor.systemBlue | ||
var mainColor = UIColor.systemBlue | ||
var positiveColor = UIColor.systemBlue | ||
var negativeColor = UIColor.systemBlue | ||
var maxColor = UIColor.systemBlue | ||
var minColor = UIColor.systemBlue | ||
var bandWidth = 0.0 | ||
var padding = 0.0 | ||
var maxValue = Double.infinity | ||
var minValue = -Double.infinity | ||
var globalMaxValue = Double.infinity | ||
var globalMinValue = -Double.infinity | ||
var yScale = Double.infinity | ||
var scale = 0.0 | ||
var verticalPadding = 0.0 | ||
var horizontalPadding = 0.0 | ||
var showDots = false | ||
|
||
init() { | ||
|
||
} | ||
|
||
init(rep: Representation) { | ||
self.representation = rep | ||
mainColor = ColorParser().fromCSS(cssString: rep.miniChart?.colors?.main?.color ?? "black") | ||
firstColor = ColorParser().fromCSS(cssString: rep.miniChart?.colors?.first?.color ?? "black") | ||
lastColor = ColorParser().fromCSS(cssString: rep.miniChart?.colors?.last?.color ?? "black") | ||
positiveColor = ColorParser().fromCSS(cssString: rep.miniChart?.colors?.positive?.color ?? "black") | ||
negativeColor = ColorParser().fromCSS(cssString: rep.miniChart?.colors?.negative?.color ?? "black") | ||
maxColor = ColorParser().fromCSS(cssString: rep.miniChart?.colors?.max?.color ?? "black") | ||
minColor = ColorParser().fromCSS(cssString: rep.miniChart?.colors?.min?.color ?? "black") | ||
showDots = rep.miniChart?.showDots ?? false | ||
} | ||
|
||
func setColor(_ index: Int, value: Double, count: Int) { | ||
if(value == maxValue && maxColor != .clear) { | ||
maxColor.set() | ||
return | ||
} | ||
|
||
if(value == minValue && minColor != .clear) { | ||
minColor.set() | ||
return | ||
} | ||
|
||
if(index == 0 && firstColor != .clear) { | ||
firstColor.set() | ||
return | ||
} | ||
|
||
if(index == count - 1 && lastColor != .clear) { | ||
lastColor.set() | ||
return; | ||
} | ||
|
||
mainColor.set() | ||
} | ||
|
||
func getBandWidth(rect: CGRect, data: Matrix) { | ||
let count = data.qMatrix?.count ?? 1 | ||
horizontalPadding = rect.width * 0.05; | ||
let width = rect.width * 0.95; | ||
let totalBandWidth = width / CGFloat(count) | ||
bandWidth = totalBandWidth * 0.8 | ||
padding = totalBandWidth * 0.1 | ||
} | ||
|
||
func getScale(rect: CGRect, data: Matrix) { | ||
let height = rect.height - 8; | ||
verticalPadding = 8 | ||
scale = height / yScale | ||
} | ||
|
||
func render(_ ctx: CGContext, rect: CGRect){} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// MiniChartView.swift | ||
// react-native-simple-grid | ||
// | ||
// Created by Vittorio Cellucci on 2022-08-03. | ||
// | ||
|
||
import Foundation | ||
import QuartzCore | ||
|
||
class MiniChartView : UIView { | ||
var miniChart = MiniChartRenderer() | ||
|
||
func setChartData(data: DataCell, representedAs rep: Representation) { | ||
self.backgroundColor = UIColor.clear | ||
self.layer.contentsScale = UIScreen.main.scale | ||
if rep.miniChart?.type == "bars" { | ||
miniChart = MiniBarChart(rep: rep) | ||
} else if(rep.miniChart?.type == "sparkline") { | ||
miniChart = MiniSparkLineChart(rep: rep) | ||
} else if(rep.miniChart?.type == "dots") { | ||
miniChart = MiniDotGraph(rep: rep) | ||
} else if(rep.miniChart?.type == "posNeg") { | ||
miniChart = PositiveNegativeChart(rep: rep) | ||
} | ||
miniChart.data = data.qMiniChart | ||
miniChart.maxValue = miniChart.data?.qMax ?? Double.infinity | ||
miniChart.minValue = miniChart.data?.qMin ?? -Double.infinity | ||
miniChart.globalMaxValue = rep.globalMax ?? Double.infinity | ||
miniChart.globalMinValue = rep.globalMin ?? -Double.infinity | ||
miniChart.yScale = miniChart.maxValue | ||
if(rep.miniChart?.yAxis?.scale == "global") { | ||
miniChart.yScale = miniChart.globalMaxValue | ||
} | ||
} | ||
|
||
override func draw(_ rect: CGRect) { | ||
guard let ctx = UIGraphicsGetCurrentContext() else { return } | ||
miniChart.render(ctx, rect: rect) | ||
} | ||
} |
Oops, something went wrong.