IOS 图表 Y 轴未正确对齐 - 图表 Cocoa Pod

Posted

技术标签:

【中文标题】IOS 图表 Y 轴未正确对齐 - 图表 Cocoa Pod【英文标题】:IOS Charts Y- Axis not properly aligned - Charts Cocoa Pod 【发布时间】:2017-06-25 17:47:07 【问题描述】:

我目前正在使用 ios 图表库的链接在这里:Link

目前,图表左轴标签不能代表它们应该显示的内容。 该图表当前已放大以允许平移。

这里是显示左轴的图片:

然而实际的数据集是:

yValues = [0,2,4,5,3,4,2,4,5,4,4]

左轴实际上以 0.8 为增量。

我该如何更改?

这是我需要的:

更新

设置后

leftAxis.granularity = 1

地块是它们应该在的地方。然而,在 [0,1,2,3,4,5] 3 被跳过并且没有显示在左轴上。这是什么原因造成的?

我的代码:

`class ReviewDetailVC: UIViewController, ChartViewDelegate 


@IBOutlet weak var chartView: LineChartView!

var yValues =  [Double]()
var yLabels =  [String]()
var months = [String]()
var xValues = [String]()
let red = UIColor(hue: 0.9639, saturation: 0.62, brightness: 0.93, alpha: 1.0)
let black = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
let grey = UIColor(red:0.81, green:0.81, blue:0.81, alpha:1.0)

let avenirDemi =  UIFont(name: "AvenirNext-DemiBold", size: 14)
override func viewDidLoad() 
    super.viewDidLoad()

    chartView.delegate = self
    // Chart Ui Settings

    chartView.leftAxis.axisMinimum = 0
    chartView.leftAxis.axisMaximum = 5
    chartView.leftAxis.labelCount = 7

    chartView.chartDescription?.text = ""
    chartView.xAxis.labelPosition = .bottom
    chartView.legend.enabled = false
    chartView.scaleYEnabled = false
    chartView.scaleXEnabled = true
    chartView.doubleTapToZoomEnabled = false
    chartView.highlighter = nil
    chartView.rightAxis.enabled = false
    chartView.xAxis.drawGridLinesEnabled = false
    chartView.dragEnabled = true
    chartView.scaleXEnabled = false
    chartView.scaleYEnabled = false
    chartView.zoom(scaleX: 4, scaleY: 1, x: 0, y: CGFloat(AxisDependency.left.rawValue))
    chartView.xAxis.labelFont = avenirDemi!
    chartView.xAxis.labelTextColor = grey
    chartView.leftAxis.labelFont = avenirDemi!
    chartView.leftAxis.labelTextColor = black
    chartView.xAxis.axisLineWidth = 0
    chartView.leftAxis.axisLineWidth = 0
    chartView.leftAxis.granularity = 1
    chartView.xAxis.avoidFirstLastClippingEnabled = true
    chartView.xAxis.granularityEnabled = true
    chartView.xAxis.granularity = 1
     chartView.xAxis.axisMinimum = 0.0
    chartView.leftAxis.gridColor = grey
    chartView.leftAxis.gridLineDashLengths = [4]
    chartView.leftAxis.gridLineWidth = 1.5
    chartView.xAxis.centerAxisLabelsEnabled = true
    chartView.noDataText = "This person has not reviewed your business."

    xValues = ["MAR 10"," MAR 15", "APR 7", "APR 8", "APR 15", "APR 30", "MAY 14", "MAY 21","MAY 31", "MAY 31"]
    yLabels = ["0","1","2","3","4","5"]
    yValues = [0,2,4,5,3,4,2,4,5,4,4]

    let Xformatter = CustomLabelsAxisValueFormatter()
    Xformatter.labels = xValues
    let Yformatter = CustomYAxisLabelValueFormatter()
    Yformatter.Ylabels = yLabels

    chartView.xAxis.valueFormatter = Xformatter
    chartView.leftAxis.valueFormatter = Yformatter



    chartView.animate(yAxisDuration: 1)

    setChart(dataPoints: xValues, values: yValues)






func setChart(dataPoints: [String], values: [Double]) 
    chartView.noDataText = "You need to provide data for the chart."

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count 
        let dataEntry = ChartDataEntry(x: Double(i)+0.5, y: 
  values[i])
        dataEntries.append(dataEntry)
    

    let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
    let chartData = LineChartData(dataSets: [chartDataSet])

    chartDataSet.drawValuesEnabled = false
    chartDataSet.colors = [red]
    chartDataSet.drawCirclesEnabled = true
    chartDataSet.circleRadius = 16
    chartDataSet.circleColors = [red]
    chartDataSet.circleHoleRadius = 12
    chartDataSet.circleHoleColor = UIColor.white
    chartDataSet.lineWidth = 4


    chartView.data = chartData







@IBAction func backBtnPressed(_ sender: Any) 
    self.presentingViewController?.dismiss(animated: true, 
 completion: nil)




class CustomLabelsAxisValueFormatter : NSObject, IAxisValueFormatter 


var labels: [String] = []

func stringForValue(_ value: Double, axis: AxisBase?) -> String 
        let count = self.labels.count

        guard let axis = axis, count > 0 else 

        return ""
        

        let factor = axis.axisMaximum / Double(count)

        let index = Int((value / factor).rounded())

        if index >= 0 && index < count 

            return self.labels[index]
        

    return ""





class CustomYAxisLabelValueFormatter : NSObject, IAxisValueFormatter 


var Ylabels: [String] = []

func stringForValue(_ value: Double, axis: AxisBase?) -> String 
    let count = self.Ylabels.count

    guard let axis = axis, count > 0 else 

        return ""
    

    let factor = axis.axisMaximum / Double(count)

    let index = Int((value / factor).rounded())

    if index >= 0 && index < count 

        return self.Ylabels[index]
    

    return ""



`

【问题讨论】:

【参考方案1】:

当我运行你的代码时,这就是我得到的:

所以我无法重现您的问题,我唯一更改的是取消注释这三行,因为我没有 CustomYAxisLabelValueFormatter 的代码。因此,如果您取消注释这些行,它应该可以工作。

    //let Yformatter = CustomYAxisLabelValueFormatter()
    //Yformatter.Ylabels = yLabels
    //chartView.leftAxis.valueFormatter = Yformatter

【讨论】:

我刚刚更新了问题。我也会发布我的代码。 我试过了,我喜欢这个效果 :) 但它不能修复左轴 谢谢:) 我想我什至不需要格式化程序。摆脱一大段代码总是很高兴!

以上是关于IOS 图表 Y 轴未正确对齐 - 图表 Cocoa Pod的主要内容,如果未能解决你的问题,请参考以下文章

周总结

pyqtgraph 轴未正确显示

在ios图表中使用setVisibleYRangeMinimum时,Y标签未完全显示

iOS图表,删除底部xAxis标签创建的额外空间

echarts如何获取y轴刻度的最大值

在 SwiftUI 中对齐 HStack,但大小不相等