Created
May 25, 2017 08:07
-
-
Save utsengar/0d080d9b75e62063c925149e9bc2b7a6 to your computer and use it in GitHub Desktop.
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
// | |
// ProgrammaticalDemo.swift | |
// PieCharts | |
// | |
// Created by Hyun Min Choi on 2017. 1. 23.. | |
// Copyright © 2017년 Ivan Schuetz. All rights reserved. | |
// | |
import UIKit | |
import PieCharts | |
class ProgrammaticalDemo: UIViewController, PieChartDelegate { | |
let chartView: PieChart = PieChart(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.addSubview(chartView) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
chartView.layers = [createCustomViewsLayer(), createTextLayer()] | |
chartView.delegate = self | |
chartView.models = createModels() // order is important - models have to be set at the end | |
} | |
// MARK: - PieChartDelegate | |
func onSelected(slice: PieSlice, selected: Bool) { | |
print("Selected: \(selected), slice: \(slice)") | |
} | |
// MARK: - Models | |
fileprivate func createModels() -> [PieSliceModel] { | |
let alpha: CGFloat = 0.5 | |
return [ | |
PieSliceModel(value: 2.1, color: UIColor.yellow.withAlphaComponent(alpha)), | |
PieSliceModel(value: 3, color: UIColor.blue.withAlphaComponent(alpha)), | |
PieSliceModel(value: 1, color: UIColor.green.withAlphaComponent(alpha)), | |
] | |
} | |
// MARK: - Layers | |
fileprivate func createCustomViewsLayer() -> PieCustomViewsLayer { | |
let viewLayer = PieCustomViewsLayer() | |
let settings = PieCustomViewsLayerSettings() | |
settings.viewRadius = 135 | |
settings.hideOnOverflow = false | |
viewLayer.settings = settings | |
return viewLayer | |
} | |
fileprivate func createTextLayer() -> PiePlainTextLayer { | |
let textLayerSettings = PiePlainTextLayerSettings() | |
textLayerSettings.viewRadius = 75 | |
textLayerSettings.hideOnOverflow = true | |
textLayerSettings.label.font = UIFont.systemFont(ofSize: 12) | |
textLayerSettings.label.textColor = UIColor.black | |
let formatter = NumberFormatter() | |
formatter.maximumFractionDigits = 1 | |
textLayerSettings.label.textGenerator = {slice in | |
return formatter.string(from: slice.data.percentage * 100 as NSNumber).map{"\($0)%"} ?? "" | |
} | |
let textLayer = PiePlainTextLayer() | |
textLayer.settings = textLayerSettings | |
return textLayer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment