Color shading inside PieChart IOS - ios

I need to create a color shading effect inside the pie chart for a particular slice only.The required image is as shown( need a light orange color inside the piechart corresponding to orange slice)
Iam using cocoapods charts library. This is the function:
let approvedColor: UIColor = UIColor(red: 19/255, green: 195/255, blue: 30/255, alpha: 1.0)
let pendingColor: UIColor = UIColor(red: 255/255, green: 148/255, blue: 56/255, alpha: 1.0)
let rejectedColor: UIColor = UIColor(red: 225/255, green: 74/255, blue: 74/255, alpha: 1.0)
let surveyData = [50,40,10]
func fillChart() {
var dataEntries = [PieChartDataEntry]()
for (val) in surveyData {
let entry = PieChartDataEntry(value: Double(val), label: nil)
dataEntries.append(entry)
}
let chartDataSet = PieChartDataSet(values: dataEntries, label: "")
chartDataSet.colors = [approvedColor,pendingColor,rejectedColor]
chartDataSet.sliceSpace = 0
chartDataSet.selectionShift = 5
let chartData = PieChartData(dataSet: chartDataSet)
chartView.data = chartData
}

Related

Swift - create array of custom colors

Is there a way to create an array of custom colors? I know I can create custom colors in class UIColors but in my case I need an array of my custom colors.
I would like to achieve something like this:
let listColors: [UIColor] = [
let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1),
let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1),
let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1),
]
You don't need a subclass to create an array do
let listColors = [
UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1),
UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1),
UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
]
access
listColors[index]
or
class Colors {
static let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
static let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1)
static let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
}
access
Colors.color1
or as global
let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1)
let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1)
access
color1
When you create a color divide by 255
UIColor(displayP3Red: 2/255.0, green: 2/255.0, blue: 2/255.0, alpha: 1)
Or if you want to be able to reference them by name or by array index:
let color1 = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), //White
let color2 = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0), //Gray
let color3 = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0), //Red
let listColors: [UIColor] = [color1, color2, color3]
If you are supporting dark and light modes in your App, it is best to define your custom colors as assets in your App and then access them something like this
extension String {
static let blueColorName = "Blue"
static let brownColorName = "Brown"
static let crustaColorName = "Crusta"
static let darkRedColorName = "DarkRed"
static let defaultColorName = "Default"
static let flamingoColorName = "Tango"
static let khakiColorName = "Khaki"
static let greenColorName = "Green"
static let limeColorName = "Lime"
static let lustColorName = "Lust"
static let oliveColorName = "Olive"
static let orangeColorName = "Orange"
static let pinkColorName = "Pink"
static let purpleColorName = "Purple"
static let roseColorName = "Rose"
static let redColorName = "Red"
static let skyColorName = "Sky"
static let steelColorName = "Steel"
static let sunColorName = "Sun"
static let tealColorName = "Teal"
static let yellowColorName = "Yellow"
// Tile Background Colors
static let tileGrayColorName = "TileGray"
static let tileGreenColorName = "TileGreen"
static let tileRedColorName = "TileRed"
static let tileYellowColorName = "TileYellow"
}
extension UIColor {
// MARK: - Custom Color List For Color Picker
static let customColorsList: [(colorName: String, uiColor: UIColor)] = [
("Orange", UIColor(named: .orangeColorName)!),
("Tango", UIColor(named: .flamingoColorName)!),
("Crusta", UIColor(named: .crustaColorName)!),
("Yellow", UIColor(named: .yellowColorName)!),
("Sun", UIColor(named: .sunColorName)!),
("Khaki", UIColor(named: .khakiColorName)!),
("Lime", UIColor(named: .limeColorName)!),
("Green", UIColor(named: .greenColorName)!),
("Olive", UIColor(named: .oliveColorName)!),
("Teal", UIColor(named: .tealColorName)!),
("Sky", UIColor(named: .skyColorName)!),
("Blue", UIColor(named: .blueColorName)!),
("Steel", UIColor(named: .steelColorName)!),
("Pink", UIColor(named: .pinkColorName)!),
("Rose", UIColor(named: .roseColorName)!),
("Purple", UIColor(named: .purpleColorName)!),
("Lust", UIColor(named: .lustColorName)!),
("Dark Red", UIColor(named: .darkRedColorName)!),
("Brown", UIColor(named: .brownColorName)!),
("Default", UIColor(named: .defaultColorName)!)
]
}

Gradient colors based on values for a bar graph

I need to define colors for graph based on the values(example : green for values between 0-50 and red for values between 50-100).I tried with gradient colors but it is not as expected. Could anyone help me out in this please? I'm using Swift 3.
I need the bottom of the graph to be green and upper part as red
Declare out of class for this code
class Colors {
var gl:CAGradientLayer!
init() {
let colorTop = UIColor(red: 135.0/255.0, green: 155.0/255.0, blue: 36.0/255.0, alpha: 1.0).cgColor
let colorcenter = UIColor(red: 186.0/255.0, green: 213.0/255.0, blue: 49.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 224.0/255.0, green: 241.0/255.0, blue: 142.0/255.0, alpha: 1.0).cgColor
self.gl = CAGradientLayer()
self.gl.colors = [colorTop, colorcenter, colorBottom]
self.gl.locations = [0.5,0.8, 1.0]
}
}
declate in viewDidLoad Method.
let colors = Colors()
view.backgroundColor = UIColor.clear
let backgroundLayer = colors.gl
backgroundLayer?.frame = Yourview.frame
Yourview.layer.insertSublayer(backgroundLayer!, at: 0)

Gradient color as a Background color of UIButton in Swift4

I have a button and i'm trying to set its background color gradient.I have three hex values which i converted to RGB and written three values in my code and pass all the three values to the button frame. But when i run the app button background not changes according to the color i have set in the code. My code to get three color codes is this,
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
let color1 = UIColor(red: 183, green: 46, blue: 79, alpha: 1)
let color2 = UIColor(red: 232, green: 79, blue: 80, alpha: 1)
let color3 = UIColor(red: 145, green: 21, blue: 79, alpha: 1)
gradientColor.colors = [color1,color2,color3]
self.loginButton.layer.addSublayer(gradientColor)
Try this and see:
#IBOutlet weak var loginButton: UIButton!
func testGradientButton() -> Void {
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
gradientColor.colors = [UIColor.blue.cgColor,UIColor.red.withAlphaComponent(1).cgColor]
self.loginButton.layer.insertSublayer(gradientColor, at: 0)
}
Here is result:
Also note: Values for RGB colors in your code, are much higher than its normal value. See your console log, it might printed following error:
[Graphics] UIColor created with component values far outside the expected range. Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
Divide all values with 255.0 and use insertSubLayer function.
let color1 = UIColor(red: 183.0/255.0, green: 46.0/255.0, blue: 79.0/255.0, alpha: 1)
let color2 = UIColor(red: 232.0/255.0, green: 79.0/255.0, blue: 80.0/255.0, alpha: 1)
let color3 = UIColor(red: 145.0/255.0, green: 21.0/255.0, blue: 79.0/255.0, alpha: 1)
Here is result with your color code values:
func testGradientButton() -> Void {
let gradientColor = CAGradientLayer()
gradientColor.frame = loginButton.frame
let color1 = UIColor(red: 183.0/255.0, green: 46.0/255.0, blue: 79.0/255.0, alpha: 1)
let color2 = UIColor(red: 232.0/255.0, green: 79.0/255.0, blue: 80.0/255.0, alpha: 1)
let color3 = UIColor(red: 145.0/255.0, green: 21.0/255.0, blue: 79.0/255.0, alpha: 1)
gradientColor.colors = [color1.cgColor,color2.cgColor,color3.cgColor]
self.loginButton.layer.insertSublayer(gradientColor, at: 0)
}

Horizontal stacked barChartView with ios-charts (port of MPAndroidChart) in Swift

I'm trying to recreate a stacked horizontal bar chart similar to this example: http://www.highcharts.com/demo/bar-stacked
My current chart is pretty close to the example, but I don't have the flexibility in the labeling / coloring that I would like to have.
import UIKit
import Charts
#objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter {
var names = [String]()
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return names[Int(value)]
}
public func setValues(values: [String]) {
self.names = values
}
}
class graphController: UIViewController {
#IBOutlet weak var barChartView: HorizontalBarChartView!
func setChart(dataPoints: [String], values: [[Double]]) {
let formatter = BarChartFormatter()
formatter.setValues(values: dataPoints)
let xaxis:XAxis = XAxis()
barChartView.noDataText = "No data."
var dataEntries: [BarChartDataEntry] = []
for j in 0..<values.count {
let dataEntry = BarChartDataEntry(x: Double(j), yValues: values[j], label: dataPoints[j])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: nil)
chartDataSet.colors = [UIColor(red: 0.0, green: 0.5, blue: 1.0, alpha: 1.0), UIColor(red: 0.5, green: 0.5, blue: 1.0, alpha: 1.0), UIColor(red: 1.0, green: 0.5, blue: 1.0, alpha: 1.0)]
chartDataSet.valueTextColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
let chartData = BarChartData(dataSet: chartDataSet)
xaxis.valueFormatter = formatter
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0
barChartView.xAxis.decimals = 0
barChartView.chartDescription?.enabled = false
barChartView.legend.enabled = true
barChartView.rightAxis.enabled = false
barChartView.data = chartData
}
override func viewDidLoad() {
super.viewDidLoad()
barChartView.noDataText = "No activity data."
barChartView.noDataTextColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
barChartView.xAxis.labelTextColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
barChartView.legend.textColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
barChartView.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
barChartView.tintColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
barChartView.gridBackgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
barChartView.borderColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
barChartView.xAxis.gridColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
barChartView.xAxis.axisLineColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
barChartView.rightAxis.enabled = false
barChartView.leftAxis.enabled = false
let names = ["A", "B", "C"]
let values = [[2.0,1.0,1.0], [1.0,2.0,1.0], [1.0,1.0,2.0]]
setChart(dataPoints: names, values: values)
}
}
How can I modify setChart() to specify the colors on a per-bar basis rather than at the data set level, and specify my legend on a per-color basis rather as a "Stack"?

Randomize color in XCode

I'm trying to randomize the text color of a label in XCode with some colors I already created, I've tried different ways and this is the closest I got.
override func viewDidLoad() {
super.viewDidLoad()
let color1 = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1)
let color2 = UIColor(red: 80/255, green: 1, blue: 200/255, alpha: 1)
let color3 = UIColor(red: 150/255, green: 80/255, blue: 1, alpha: 1)
let color4 = UIColor(red: 1, green: 80/255, blue: 80/255, alpha: 1)
let color5 = UIColor(red: 80/255, green: 1, blue: 80/255, alpha: 1)
var randomEight = arc4random_uniform(5)+1
var randomColor:String = String(format:"color%i", randomEight)
randomLabel.textColor = randomColor
}
However, the randomColor variable is a String and I can't transform it to a UIColor.
Put all of the colors in an array, then use randomEight as the index in the array to get the right color. Also, you may want to check the integer division to make sure you get the right colors.
let color1 = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1)
let color2 = UIColor(red: 80/255, green: 1, blue: 200/255, alpha: 1)
let color3 = UIColor(red: 150/255, green: 80/255, blue: 1, alpha: 1)
let color4 = UIColor(red: 1, green: 80/255, blue: 80/255, alpha: 1)
let color5 = UIColor(red: 80/255, green: 1, blue: 80/255, alpha: 1)
let colors = [color1, color2, color3, color4, color5]
let randomEight = Int(arc4random_uniform(UInt32(colors.count)))
randomLabel.textColor = colors[randomEight]
Put the colours in an array, then select a random index, e.g.:
let colors: [UIColor] = [ UIColor.redColor(),
UIColor.blueColor(),
UIColor.greenColor()
] // ^- replace with your own colors
let randomColor = colors[Int(arc4random_uniform(UInt32(colors.count)))]
Note the necessary casts to work with arc4random_uniform. Also note that the indices start at 0, so do not add 1 to the random number.
There is no need for the randomEight (why is it “eight” anyhow?), and if the single text colour is the only place you use the colour, you can even leave out the randomColor and assign directly to randomLabel.textColor.
let randomEight = arc4random_uniform(5)+1
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
if randomEight == 1 {
randomLabel.textColor = UIColor.cyanColor()
}
else if randomEight == 2 {
randomLabel.textColor = UIColor.purpleColor()
}
else if randomEight == 3{
randomLabel.textColor = UIColor.orangeColor()
} else if randomEight == 4 {
randomLabel.textColor = UIColor.blueColor()
}
else {
randomLabel.textColor = UIColor.greenColor()
}
return randomLabel
}
Try this oneliner ;)
randomLabel.textColor = UIColor(red : CGFloat(arc4random_uniform(255)) , green : CGFloat(arc4random_uniform(255)), blue : CGFloat(arc4random_uniform(255)) , alpha : 1)

Resources