review comments updated
This commit is contained in:
parent
396c22f243
commit
be094b3cf9
@ -9,50 +9,13 @@
|
||||
import Foundation
|
||||
|
||||
@objcMembers public class DoughnutChartModel: MoleculeProtocol {
|
||||
|
||||
public var backgroundColor: String?
|
||||
public static var identifier: String = "doughnutChart"
|
||||
public var title: LabelModel?
|
||||
public var subtitle: LabelModel?
|
||||
public var sections: [ChartItemModel]
|
||||
public var lineWidth: CGFloat = 30.0
|
||||
public var lineGap: CGFloat = 0.05 //For 3px gap
|
||||
public var spaceRequired = true
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case list
|
||||
case title
|
||||
case subtitle
|
||||
case sections
|
||||
}
|
||||
|
||||
required public init(from decoder: Decoder) throws {
|
||||
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
||||
self.title = try typeContainer.decodeMoleculeIfPresent(codingKey: .title) as? LabelModel
|
||||
self.subtitle = try typeContainer.decodeMoleculeIfPresent(codingKey: .subtitle) as? LabelModel
|
||||
//self.sections = try typeContainer.decodeMolecules(codingKey: .sections) as! [ChartItemModel]
|
||||
|
||||
//TODO: Hasve to check with scott
|
||||
self.sections = []
|
||||
guard var container = try? typeContainer.nestedUnkeyedContainer(forKey: .sections) else {
|
||||
return
|
||||
}
|
||||
var sections = [ChartItemModel]()
|
||||
while !container.isAtEnd {
|
||||
if let element = try container
|
||||
.decodeIfPresent(ChartItemModel.self) {
|
||||
sections.append(element)
|
||||
}
|
||||
}
|
||||
self.sections = sections
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeModelIfPresent(title, forKey: .title)
|
||||
try container.encodeModelIfPresent(subtitle, forKey: .subtitle)
|
||||
try container.encodeModels(sections as [Model], forKey: .sections)
|
||||
}
|
||||
public var lineWidth: CGFloat?
|
||||
public var spaceRequired: Bool?
|
||||
}
|
||||
|
||||
|
||||
@ -62,24 +25,4 @@ import Foundation
|
||||
public var percent: CGFloat
|
||||
public var color: String
|
||||
public static var identifier: String = "chartItem"
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case label
|
||||
case percent
|
||||
case color
|
||||
}
|
||||
|
||||
required public init(from decoder: Decoder) throws {
|
||||
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
||||
self.label = try typeContainer.decodeMolecule(codingKey: .label) as! LabelModel
|
||||
self.percent = try typeContainer.decode(CGFloat.self, forKey: .percent)
|
||||
self.color = try typeContainer.decode(String.self, forKey: .color)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeModel(label, forKey: .label)
|
||||
try container.encode(percent, forKey: .percent)
|
||||
try container.encode(color, forKey: .color)
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,7 +21,6 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol {
|
||||
var heightConstraint: NSLayoutConstraint?
|
||||
|
||||
private var sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: 150)!
|
||||
//private var lineSizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: 30)!
|
||||
|
||||
var height: CGFloat = 150 {
|
||||
didSet {
|
||||
@ -105,7 +104,6 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol {
|
||||
subTitleLabel.setWithModel(doughnutChartModel.subtitle, delegateObject, additionalData)
|
||||
titleLabel.textAlignment = .center
|
||||
subTitleLabel.textAlignment = .center
|
||||
//lineSizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: doughnutChartModel.lineWidth)!
|
||||
updateLabelContainer()
|
||||
drawGraph()
|
||||
}
|
||||
@ -118,20 +116,17 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol {
|
||||
height: widthHeight)
|
||||
if let doughnutChart = doughnutChartModel {
|
||||
var prevPercent: CGFloat = 0.0
|
||||
//If Single item in array. We don't need gap
|
||||
doughnutChartModel?.lineGap = (doughnutChart.sections.count == 1) ? 0.0 : doughnutChart.lineGap
|
||||
for model in doughnutChart.sections {
|
||||
prevPercent += drawBar(model, prevPercent, doughnutChart.spaceRequired, doughnutChart.lineGap)
|
||||
prevPercent += drawBar(model, prevPercent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func drawBar(_ chartModel: ChartItemModel, _ prevPercent: CGFloat, _ spaceRequired: Bool, _ lineGap: CGFloat) -> CGFloat {
|
||||
func drawBar(_ chartModel: ChartItemModel, _ prevPercent: CGFloat) -> CGFloat {
|
||||
|
||||
let shapeLayer = CAShapeLayer()
|
||||
shapeLayer.frame = containerLayer.frame
|
||||
shapeLayer.lineWidth = lineWidth()
|
||||
//lineSizeObject.getValueBasedOnApplicationWidth()
|
||||
shapeLayer.fillColor = nil
|
||||
shapeLayer.strokeColor = UIColor.mfGet(forHex: chartModel.color).cgColor
|
||||
|
||||
@ -141,7 +136,7 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol {
|
||||
let clockwise = true
|
||||
|
||||
let value: CGFloat = chartModel.percent
|
||||
let gap: CGFloat = spaceRequired ? lineGap : 0.0
|
||||
let gap: CGFloat = spaceReuired() ? lineGap() : 0.0
|
||||
let startAngle = ((prevPercent / 100.0) * 2 * .pi) - (0.5 * .pi)
|
||||
let endAngle = ((value / 100.0) * 2 * .pi) + startAngle - gap
|
||||
let circlePath = UIBezierPath(arcCenter: arcCenter,
|
||||
@ -167,7 +162,15 @@ open class DoughnutChart: View, MVMCoreUIViewConstrainingProtocol {
|
||||
|
||||
func lineWidth() -> CGFloat {
|
||||
return doughnutChartModel?.lineWidth ?? 30
|
||||
//lineSizeObject.getValueBasedOnApplicationWidth() + 5
|
||||
}
|
||||
|
||||
func lineGap() -> CGFloat {
|
||||
//If array is having the single item then no space required
|
||||
return doughnutChartModel?.sections.count == 1 ? 0.0 : 0.05
|
||||
}
|
||||
|
||||
func spaceReuired() -> Bool {
|
||||
return doughnutChartModel?.spaceRequired ?? true
|
||||
}
|
||||
|
||||
func updateLabelContainer() {
|
||||
|
||||
@ -171,9 +171,4 @@ class ColorViewWithLabel: View, MVMCoreUIViewConstrainingProtocol {
|
||||
colorView.backgroundColor = UIColor.mfGet(forHex: chartItemModel.color)
|
||||
}
|
||||
|
||||
override func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
|
||||
Label.setUILabel(label, withJSON: json?.optionalDictionaryForKey("label"), delegate: delegateObject, additionalData: additionalData)
|
||||
colorView.backgroundColor = UIColor.mfGet(forHex: json?.optionalStringForKey("color") ?? "#000000")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user