代码之家  ›  专栏  ›  技术社区  ›  reza_khalafi

如何在iOS图表的左轴上添加字符串?

  •  0
  • reza_khalafi  · 技术社区  · 6 年前

    let leftAxis = chartView.leftAxis
    leftAxis.removeAllLimitLines()
    leftAxis.gridLineDashLengths = [5, 5]
    leftAxis.drawLimitLinesBehindDataEnabled = true
    leftAxis.labelTextColor = .white
    leftAxis.axisMinimum = 0
    leftAxis.drawLabelsEnabled = true
    let array = ["a","b","c","e","f","g","h"]
    leftAxis.valueFormatter = IndexAxisValueFormatter(values: array)
    leftAxis.granularity = 1
    

    为什么不把我的标签显示在左轴上?

    1 回复  |  直到 6 年前
        1
  •  4
  •   AlexSmet    6 年前

    IndexAxisValueFormatter 用于传递x轴标签的数组,而不是y轴标签。您需要通过实现 IAxisValueFormatter 协议。

    MyLeftAxisFormatter 班级。

    class MyLeftAxisFormatter: NSObject, IAxisValueFormatter {
    
        var labels: [Int : String] = [ 0 : "zero", 3 : "three", 6 : "six", 9 : "nine", 12 : "twelve", 15 : "fifteen", 18 : "eighteen"]
    
        func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            return labels[Int((value).rounded())] ?? ""
        }
    }
    

    把它作为我图表上的左轴。

    lineChartView.leftAxis.valueFormatter = MyLeftAxisFormatter()
    

    Y-Axis Labels

    请记住,y轴的显示值是由图表引擎根据各种因素(如视图高度、最小值/最大值等)计算的,当您更改源数据时,这些值可能会更改。