带圆角的iso-chart BarChart

时间:2015-07-24 10:15:35

标签: ios swift ios-charts

BarChartView中是否有设置为条形图启用圆角?

我希望与其他库iosbarchart达到类似的效果 enter image description here

2 个答案:

答案 0 :(得分:2)

我做了一个ios-charts的分支来大致完成这个:https://github.com/mcconkiee/ios-charts

var yVals: [BarChartDataEntry] = []
                var xVals: [String] = []
                var dataCount = sections.count
                for i in 0...(dataCount - 1) {
                    var dataObjectInstance = sections[i] as SomeCustomDataObject
                    var entry = BarChartDataEntry(value: dataObjectInstance.numeric!, xIndex: i)
                    entry.rounded = true //make the end caps round!
                    if let tintClr = dataObjectInstance.tint as String?{
                        if let clr = UIColor(hexString: tintClr) as UIColor?{
                            entry.tint = clr
                        }
                    }
                    yVals.append(entry)
                    var time = i % 12
                    if(time == 0) {
                        time = 12
                    }
                    if let objVal = dataObjectInstance.objectValue as String?{
                        xVals.append(objVal)
                    }

                }

答案 1 :(得分:0)

这是一个快速的示例四舍五入的条形图。在您的.swift文件中,添加以下代码。

@IBOutlet weak var barView: UIView! 
var BAR_WIDTH_CONSTANT: CGFloat = 9
var BAR_DISTANCE: CGFloat = 14 // distance between your bars
var BAR_MAX_HEIGHT: CGFloat = 50 // maximum height of a bar
let MAX_VALUE: CGFloat = 10 //maximum value of a bar
let MIN_VALUE: CGFloat = 0 // minimum value of a bar

然后在您的viewDidLoad()中添加以下代码。

 var xPos: CGFloat = 0
 var yPos: CGFloat = 0
 let values: [Int] = [1, 2, 3, 4, 5, 6, 7] //values of a Bar chart
 for i in 0..< values.count {
     // draw bar
     let barHeight = getBarHeight(height: CGFloat(values[i]), maxHeight: BAR_MAX_HEIGHT, maxValue: MAX_VALUE, minValue: MIN_VALUE)
     yPos = BAR_MAX_HEIGHT - barHeight
     let frame = CGRect(x: xPos, y: yPos, width: BAR_WIDTH_CONSTANT, height: barHeight)
     let bar = UIView(frame: frame)

     bar.layer.cornerRadius = BAR_WIDTH_CONSTANT < barHeight ? BAR_WIDTH_CONSTANT / 2 : barHeight / 2
     bar.layer.masksToBounds = true
     self.barView.addSubview(bar)

     xPos = xPos + BAR_WIDTH_CONSTANT + BAR_DISTANCE
     // bar drawing complete
}


func getBarHeight(height: CGFloat, maxHeight: CGFloat, maxValue: CGFloat, minValue: CGFloat) -> CGFloat {
        let barHeight = (maxHeight * height ) / (maxValue - minValue)
        return barHeight
}

希望这会有所帮助。