繁体   English   中英

iOS 图表的 x 轴上的重复值

[英]Repeating values on x-axis for iOS Charts

我正在使用iOS 图表并且我的 x 轴上有一个重复的标签。
在此处输入图片说明

我得到了很多答案(第一第二第三)我应该设置粒度设置:

xAxis.granularityEnabled = true
xAxis.granularity = 1.0

但是,当我做 x 轴上的标签时,标签就会消失。

在此处输入图片说明

我对 x 轴的设置:

let xAxis = lineChartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .boldSystemFont(ofSize: 12)
xAxis.setLabelCount(6, force: false)
xAxis.labelTextColor = UIColor(red: 0, green: 0, blue: 255/255, alpha: 1.0)
xAxis.axisLineColor = UIColor(white: 0.2, alpha: 0.4)
xAxis.gridColor = UIColor(white: 0.8, alpha: 0.4)
xAxis.valueFormatter = ChartXAxisFormatter()

我根据文档的建议创建了一个自定义类来格式化传递到 x 轴的值:

class ChartXAxisFormatter: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.setLocalizedDateFormatFromTemplate("MM/dd")
        let date = Date(timeIntervalSince1970: value)
        return dateFormatter.string(from: date)
    }
}

更新

感谢 @aiwiguna 并在 Charts repo的帮助下,我能够创建一个自定义类,该类包含两件事:日期(Double)和相应的索引(Int),而不仅仅是日期。

class ChartXAxisFormatter: NSObject, IAxisValueFormatter {
    private var dates: [Double]?
    
    convenience init(usingDates dateArr: [Double]) {
        self.init()
        self.dates = dateArr
    }
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        print("value: \(value)")
        print("dates: \(dates)")
        let index = Int(value)
    
        guard let dates = dates, index < dates.count else {
            return "?"
        }
        
        let date = dates[index]
        let dateFormatter = DateFormatter()
        dateFormatter.setLocalizedDateFormatFromTemplate("MM/dd")
        let formattedDate = Date(timeIntervalSince1970: date.rounded())
        return dateFormatter.string(from: formattedDate)
    }
}

这个想法是有一个日期数组[1598409060.0, 1598409060.0] ,并通过使用索引访问它一个一个地返回它们。 记录valueindex确实表明情况确实如此。

问题是,虽然减少了重复标签的数量以匹配绘图数量,但重复标签仍然存在:

在此处输入图片说明

两个图都应该在一个 x 轴标签“8/25”上彼此堆叠。

我正在使用enumerated()来提取ChartDataEntry的索引:

var dateArr = [Double]()
for (index, fetchedMetric) in fetchedMetrics.enumerated() {
    let date = fetchedMetric.date.timeIntervalSince1970
    dateArr.append(date)
    if var yValues = metricDict[fetchedMetric.unit] {
        yValues.append(ChartDataEntry(x: Double(index), y: Double(truncating: fetchedMetric.value)))
        metricDict.updateValue(yValues, forKey: fetchedMetric.unit)
    } else {
        metricDict.updateValue([ChartDataEntry(x: Double(index), y: Double(truncating: fetchedMetric.value))], forKey: fetchedMetric.unit)
    }
}

这是因为当您设置图表设置(粒度 = 1 和 ChartXAxisFormatter)时,每秒都在 xAxis 中显示

我更喜欢使用数组日期在轴上显示日期,类似这样

public class DateValueFormatter: NSObject, IAxisValueFormatter {
    private let dateFormatter = DateFormatter()
    private let dates:[Date]
    
    init(dates: [Date]) {
        self.dates= dates
        super.init()
        dateFormatter.dateFormat = "dd MMM HH:mm"
    }
    
    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        if value >= 0 && value < objects.count{
            let date = dates[Int(value)]
            return dateFormatter.string(from: date)
        }   
        return ""
    }
}

如果您不想更改格式化的值,可以尝试将粒度设置为 86400(1 天内的总秒数)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM