How to add string labels( xAxis labels) to horizontal bar in Charts framework

后端 未结 4 1246
囚心锁ツ
囚心锁ツ 2020-12-20 07:52

I am using charts framework for drawing charts. I need to add some strings in left of every bars. In my code always there are two bars, one for Income and o

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 08:27

    I am using an extension for this

    extension BarChartView {
    
        private class BarChartFormatter: NSObject, IAxisValueFormatter {
    
            var labels: [String] = []
    
            func stringForValue(_ value: Double, axis: AxisBase?) -> String {
                return labels[Int(value)]
            }
    
            init(labels: [String]) {
                super.init()
                self.labels = labels
            }
        }
    
        func setBarChartData(xValues: [String], yValues: [Double], label: String) {
    
            var dataEntries: [BarChartDataEntry] = []
    
            for i in 0..

    you can use this in code like this

    chartView.setBarChartData(xValues: xEntries, yValues: yEntries, label: "Income")
    

    where,

    //You need to add values dynamically
    var yEntries: [Double] = [1000, 200, 500]
    var xEntries: [String] = ["Income1", "Income2", "Income3"]
    

    Hope this works.

提交回复
热议问题