How to use xAxis with Label in Charts-frameworks

核能气质少年 提交于 2020-02-08 05:07:56

问题


As title,After I check How to add Strings on X Axis in iOS-charts? this post. I still don't know where can I put "formato.stringForValue(Double(i), axis: taxis)" in for-in loop, as I know, when I implement this protocol ,this function will send out a "String".My question is how does it connect to my xAxis data??

Following is my code ,please refer to it.

    //setChartData
func setChart(dataPoints:[String],values:[Double]){

    self.barChartView.noDataText = "You need to provide data for the chart."
    let xaxis = XAxis()
    let fomatter = BarChartFomatter()

    fomatter.setValues(values: dataPoints)


    var dataEntries:[BarChartDataEntry] = []

    for i in 0..<dataPoints.count{
        let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
         *fomatter.stringForValue(Double(i), axis: xaxis)*
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
    let data = BarChartData(dataSet: chartDataSet)

    xaxis.valueFormatter = fomatter
    self.barChartView.backgroundColor = UIColor.clear
    self.barChartView.xAxis.drawLabelsEnabled = false
    self.barChartView.chartDescription?.enabled = false
    self.barChartView.legend.enabled = false
    self.barChartView.rightAxis.enabled = false
    self.barChartView.xAxis.valueFormatter = xaxis.valueFormatter
    self.barChartView.xAxis.labelPosition = .bottom
    //self.barChartView.description = "Test String"
    barChartView.xAxis.granularityEnabled = true
    barChartView.xAxis.granularity = 1.0 //default granularity is 1.0, but it is better to be explicit
    barChartView.xAxis.decimals = 0
    self.barChartView.data = data

}

And my BarChartFomatter as follow:

import UIKit
@objc(BarChartFomatter)
open class BarChartFomatter: 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
  }
}

Result: There is no text under the chart

Please help me , thanks

来源:https://stackoverflow.com/questions/40376720/how-to-use-xaxis-with-label-in-charts-frameworks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!