174 lines
7.3 KiB
Swift
174 lines
7.3 KiB
Swift
//
|
|
// DoughnutChartView.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Murugan, Vimal on 26/12/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
@objcMembers open class DoughnutChartView: View {
|
|
|
|
var doughnutChart = DoughnutChart(frame: CGRect.zero)
|
|
var colorLablesStack = ColorViewLabelsStack()
|
|
var container = MVMCoreUICommonViewsUtility.commonView()
|
|
var doughnutChartModel: DoughnutChartModel? {
|
|
get { return model as? DoughnutChartModel }
|
|
}
|
|
|
|
open override func setupView() {
|
|
super.setupView()
|
|
guard container.superview == nil else {
|
|
return
|
|
}
|
|
|
|
addSubview(container)
|
|
doughnutChart.translatesAutoresizingMaskIntoConstraints = false
|
|
container.addSubview(doughnutChart)
|
|
colorLablesStack.translatesAutoresizingMaskIntoConstraints = false
|
|
container.addSubview(colorLablesStack)
|
|
|
|
NSLayoutConstraint.constraintPinSubview(container, pinTop: true, pinBottom: true, pinLeft: true, pinRight: true)
|
|
doughnutChart.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
|
|
doughnutChart.topAnchor.constraint(equalTo: container.topAnchor, constant: PaddingFour).isActive = true
|
|
container.bottomAnchor.constraint(greaterThanOrEqualTo: doughnutChart.bottomAnchor).isActive = true
|
|
|
|
let containerBottomAnchor = container.bottomAnchor.constraint(equalTo: doughnutChart.bottomAnchor)
|
|
containerBottomAnchor.priority = UILayoutPriority(rawValue: 200)
|
|
containerBottomAnchor.isActive = true
|
|
|
|
let colorLablesBottomAnchor = container.bottomAnchor.constraint(equalTo: colorLablesStack.bottomAnchor)
|
|
colorLablesBottomAnchor.priority = UILayoutPriority(rawValue: 204)
|
|
colorLablesBottomAnchor.isActive = true
|
|
|
|
let colorLablesTopAnchor = colorLablesStack.topAnchor.constraint(equalTo: doughnutChart.topAnchor)
|
|
colorLablesTopAnchor.priority = UILayoutPriority(rawValue: 204)
|
|
colorLablesTopAnchor.isActive = true
|
|
|
|
colorLablesStack.topAnchor.constraint(greaterThanOrEqualTo: doughnutChart.topAnchor).isActive = true
|
|
container.bottomAnchor.constraint(greaterThanOrEqualTo: colorLablesStack.bottomAnchor).isActive = true
|
|
container.trailingAnchor.constraint(greaterThanOrEqualTo: colorLablesStack.trailingAnchor).isActive = true
|
|
|
|
let centerY = colorLablesStack.centerYAnchor.constraint(equalTo: doughnutChart.centerYAnchor)
|
|
centerY.priority = .defaultLow
|
|
centerY.isActive = true
|
|
|
|
colorLablesStack.leadingAnchor.constraint(equalTo: doughnutChart.trailingAnchor, constant: PaddingThree).isActive = true
|
|
colorLablesStack.backgroundColor = UIColor.clear
|
|
|
|
}
|
|
|
|
open override func updateView(_ size: CGFloat) {
|
|
super.updateView(size)
|
|
doughnutChart.updateView(size)
|
|
colorLablesStack.updateView(size)
|
|
}
|
|
|
|
open override func reset() {
|
|
super.reset()
|
|
colorLablesStack.reset()
|
|
}
|
|
|
|
open override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
|
|
super.setWithModel(model, delegateObject, additionalData)
|
|
doughnutChart.clearLayers()
|
|
colorLablesStack.removeAllItemViews()
|
|
doughnutChart.setWithModel(model, delegateObject, additionalData)
|
|
colorLablesStack.setWithModel(model, delegateObject, additionalData)
|
|
}
|
|
|
|
open override func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
|
|
if model == nil {
|
|
let data = try! JSONSerialization.data(withJSONObject: json!)
|
|
let decoder = JSONDecoder()
|
|
let model = try! decoder.decode(DoughnutChartModel.self, from: data)
|
|
setWithModel(model, delegateObject, additionalData as? [String : AnyHashable])
|
|
} else {
|
|
setWithModel(model, delegateObject, additionalData as? [String : AnyHashable])
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
class ColorViewLabelsStack: MoleculeStackView {
|
|
|
|
var dougnutChartModel: DoughnutChartModel?
|
|
override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
|
|
let previousModel = self.dougnutChartModel
|
|
removeAllItemViews()
|
|
guard let dougnutChartModel = model as? DoughnutChartModel else {
|
|
return
|
|
}
|
|
var items: [StackItem]?
|
|
if previousModel?.sections.count == dougnutChartModel.sections.count {
|
|
items = stackItems
|
|
}
|
|
stackItems = []
|
|
self.model = MoleculeStackModel(molecules: [])
|
|
for (index, chartItemModel) in dougnutChartModel.sections.enumerated() {
|
|
let colorViewWithLabel = items?[index].view as? ColorViewWithLabel ?? ColorViewWithLabel()
|
|
colorViewWithLabel.setWithModel(chartItemModel, delegateObject, additionalData)
|
|
addView(colorViewWithLabel, lastItem: index == dougnutChartModel.sections.count - 1)
|
|
}
|
|
self.dougnutChartModel = dougnutChartModel
|
|
restack()
|
|
}
|
|
}
|
|
|
|
class ColorViewWithLabel: View, MVMCoreUIViewConstrainingProtocol {
|
|
|
|
var label = Label.commonLabelB2(true)
|
|
var colorView = MVMCoreUICommonViewsUtility.commonView()
|
|
private var sizeObject = MFStyler.sizeObjectGeneric(forCurrentDevice: 8)!
|
|
var heightConstraint: NSLayoutConstraint?
|
|
|
|
override func setupView() {
|
|
super.setupView()
|
|
guard colorView.superview == nil else {
|
|
return
|
|
}
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
addSubview(colorView)
|
|
addSubview(label)
|
|
|
|
heightConstraint = colorView.heightAnchor.constraint(equalToConstant: sizeObject.getValueBasedOnApplicationWidth())
|
|
heightConstraint?.isActive = true
|
|
colorView.widthAnchor.constraint(equalTo: colorView.heightAnchor).isActive = true
|
|
colorView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
|
colorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
|
|
|
label.leadingAnchor.constraint(equalTo: colorView.trailingAnchor, constant: PaddingOne).isActive = true
|
|
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
|
trailingAnchor.constraint(greaterThanOrEqualTo: label.trailingAnchor).isActive = true
|
|
bottomAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
|
|
|
|
}
|
|
|
|
override func updateView(_ size: CGFloat) {
|
|
super.updateView(size)
|
|
label.updateView(size)
|
|
heightConstraint?.constant = sizeObject.getValueBased(onSize: size)
|
|
setNeedsDisplay()
|
|
}
|
|
|
|
override func reset() {
|
|
super.reset()
|
|
label.reset()
|
|
}
|
|
|
|
override func setAsMolecule() {
|
|
label.setAsMolecule()
|
|
}
|
|
|
|
override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
|
|
super.setWithModel(model, delegateObject, additionalData)
|
|
guard let chartItemModel = model as? ChartItemModel else {
|
|
return
|
|
}
|
|
label.setWithModel(chartItemModel.label, delegateObject, additionalData)
|
|
colorView.backgroundColor = UIColor.mfGet(forHex: chartItemModel.color)
|
|
}
|
|
|
|
}
|