Using swift and core plot, does anyone know how I can draw bar charts that display the y-axis data horizontally rather than vertically?
Thanks
Using swift and core plot, does anyone know how I can draw bar charts that display the y-axis data horizontally rather than vertically?
Thanks
@jamescal. Thanks very much for your question! Hmmmmm, my suggestion is to go through the following tutorial:
https://www.raywenderlich.com/131985/core-plot-tutorial-getting-started, and if you’re still unable to find the solution, come back here, and perhaps we can flag the author of the tutorial to answer your question!
I hope this helps!
All the best!
Hello,
I work with the same issue. I am sharing my code blocks below. I do not refactor yet.
//
// ViewController.swift
import UIKit
class ViewController: UIViewController {
var frameWidth = 0
var onePart = 0.0
override func viewDidLoad() {
super.viewDidLoad()
frameWidth = (Int(self.view.frame.width) - 100)
onePart = Double(frameWidth) / Double(100)
NSLog("frame Width %i One Part %d", frameWidth,onePart)
drawlines(lineNumber:1, percent:30, linename:"Bar1")
drawlines(lineNumber:2, percent:85, linename:"Bar2")
drawlines(lineNumber:3, percent:49, linename:"Bar3")
drawlines(lineNumber:4, percent:100, linename:"Bar4")
}
func drawlines (lineNumber num:Int , percent val:Double, linename name:String){
let startpoint=50
let distance=80
let start = CGPoint(x:20,y:Int(num*distance)+startpoint)
let end = CGPoint(x:Int(val*onePart)+20,y:Int(num*distance)+startpoint)
//first label settings
let lbl = UILabel()
lbl.frame = CGRect(x: start.x, y: start.y - 25, width: 55, height: 15)
lbl.font = lbl.font.withSize(15)
lbl.text = name
view.addSubview(lbl)
//red part of line
drawLine(startpoint: start, endpint: end,linecolor: UIColor.red.cgColor,linewidth:11.0, textValue: String(val))
//gray part of line
let nextpt = Int(Double(100 - val)*onePart) + Int(val*onePart)
let nstart = CGPoint(x:Int(val*onePart)+20,y:Int(num*distance)+startpoint)
let nend = CGPoint(x:Int(nextpt)+20,y:Int(num*distance)+startpoint)
drawLine(startpoint: nstart, endpint: nend, linecolor: UIColor.gray.cgColor,linewidth:11.0, textValue: String(val))
//value label
let lbl2 = UILabel()
lbl2.frame = CGRect(x: nend.x + 15, y: start.y - 10, width: 90, height: 20)
lbl2.font = lbl2.font.withSize(15)
lbl2.text = String(format: "%.0f %%", val)
view.addSubview(lbl2)
}
// Main DrawLine Method
func drawLine(startpoint start:CGPoint, endpint end:CGPoint, linecolor color: CGColor , linewidth widthline:CGFloat, textValue: String){
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = color
shapeLayer.lineWidth = widthline
view.layer.addSublayer(shapeLayer)
}
}