如何在iOS图表中自定义数据点标签?

问题描述 投票:4回答:4

我正在尝试使线图中的数据点标签显示自定义字符串而不是实际数字(使用iOS图表/图表库)。我想知道是否有类似IAxisFormatter的东西我用来格式化我的x和y轴标签。

我想知道是否有人知道在Swift中如何做到这一点?我似乎无法在网上找到任何例子。谢谢!

swift xcode charts ios-charts
4个回答
5
投票

您必须将IValueFormatter协议附加到ViewController并实现stringForValue(_:entry:dataSetIndex:viewPortHandler:)方法(1)。

然后将ViewController设置为图表数据集(2)的valueFormatter委托。

import UIKit
import Charts

class ViewController: UIViewController, IValueFormatter {

    @IBOutlet weak var lineChartView: LineChartView!

    // Some data
    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    let unitsSold = [20.0, 4.0, 3.0, 6.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

    // (1) Implementation the delegate method for changing data point labels. 
    func stringForValue(_ value: Double,
                        entry: ChartDataEntry,
                        dataSetIndex: Int,
                        implement delegate methodviewPortHandler: ViewPortHandler?) -> String{

        return "My cool label " + String(value)
    }

    func setChart(dataPoints: [String], values: [Double]){
        var dataEntries: [ChartDataEntry] = []

        // Prepare data for chart
        for i in 0..<dataPoints.count {
            let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
            dataEntries.append(dataEntry)
        }

        let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
        let lineChartData = LineChartData(dataSets: [lineChartDataSet])

        // (2) Set delegate for formatting datapoint labels
        lineChartData.dataSets[0].valueFormatter = self

        lineChartView.data = lineChartData
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setChart(dataPoints: months, values: unitsSold)
    }
}

2
投票

在我的情况下,我的数据集的值为y,x是索引

enter image description here

// this shows date string instead of index
let dates = ["11/01", "11/02", "11/03", "11/04"...etcs]
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:months)

设定轴值enter image description here


2
投票

内存泄漏警报!

回答这个答案:

You have to append IValueFormatter protocol to your ViewController and implement stringForValue(_:entry:dataSetIndex:viewPortHandler:) method (1).

Then set ViewController as valueFormatter delegate for charts data set (2).

我发现答案很有帮助。它帮助我让我的程序工作。但它导致内存泄漏。视图控制器具有对图形对象的强引用,由于值格式化程序委托,它可以强烈引用视图控制器:

        // (2) Set delegate for formatting datapoint labels
    lineChartData.dataSets[0].valueFormatter = self

Here's a graph of my memory consumption in my program thanks to that leak as I instantiate and exit my (running on MacOS) view controller 3 times

为了解决内存泄漏问题,我将3个日期格式化程序放在一个单独的“ChartsFormatter”文件中:

class ChartsFormatterBlank: IValueFormatter {
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
    // We do not want labels on each data point in our graph
    return ""
}
}
class ChartsFormatterPercent: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        // y-value percent
        return "\(Int(value*100))%"
    }
}
class ChartsFormatterDateShort: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        let date = Date(timeIntervalSinceReferenceDate: value)
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .short
        dateFormatter.timeStyle = .short
        return dateFormatter.string(from:date)
    }
}

以下是我在视图控制器中调用它们的方式:

let chartsFormatterBlank = ChartsFormatterBlank()
let chartsFormatterPercent = ChartsFormatterPercent()
let chartsFormatterDateShort = ChartsFormatterDateShort()

line1.valueFormatter = chartsFormatterBlank

lineChart.xAxis.valueFormatter = chartsFormatterDateShort
lineChart.leftAxis.valueFormatter = chartsFormatterPercent
lineChart.rightAxis.valueFormatter = chartsFormatterPercent

我宁愿设置一个属性,在数据点上没有标签,而不是我当前为每个数据点创建一个空字符串的系统。但我还没弄明白该怎么做。


0
投票

在设置中:

self.lineChartView.xAxis.valueFormatter = self

之后:

extension YourViewController: ChartViewDelegate, IValueFormatter, IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    let formatter = DateFormatter()
    formatter.dateFormat = "dd.MM"
    return formatter.string(from: Date(timeIntervalSince1970: value))
}

}

结果:result

© www.soinside.com 2019 - 2024. All rights reserved.