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

时间:2017-04-05 19:23:12

标签: swift xcode charts ios-charts

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

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

5 个答案:

答案 0 :(得分: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)
    }
}

答案 1 :(得分: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 :(得分: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

相对于当前为每个数据点创建空字符串的系统,我宁愿将属性设置为在数据点上没有标签。但是我还没有弄清楚该怎么做。

答案 3 :(得分: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

答案 4 :(得分:0)

有点晚了,但是如果有人遇到同样的问题,我相信这个问题是指在图表中的数据点标记中放置一个字符串。

折线图中的每个条目都是一个ChartDataEntry,默认情况下,它会使用x和y的Doubles进行初始化。 您可以使用setValue将任何对象设置为该条目:

entry.setValue(value: AnyObject, forKey: "data")

这将允许您在使用的标记类的setLabel()方法中使用该对象。例如:

class BalloonMarker: Marker
{

        ...
    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
    {
        setLabel("\((entry.data as! AnyObject).date) \(String(entry.y))")
    }
}

在上面的示例中,我为数据点值添加了字符串日期前缀,这是我之前使用setValue()方法设置的对象的属性。

希望这会有所帮助。