如何使用 ios 图表设置 x 轴标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40323288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:40:13  来源:igfitidea点击:

How to set x-axis labels with ios charts

iosswiftswift3ios-charts

提问by matt

I've recently started using the ios charts library and am having trouble finding information on how to manipulate the x-axis with the swift3 update.

我最近开始使用 ios 图表库,但无法找到有关如何使用 swift3 更新操作 x 轴的信息。

What I want to do is label the x-axis with time values, e.g. 2:03:00, 2:03:01, 2:03:02, etc. In the tutorials I find online, this seems to be very easy; many of them use months of the year as the x-axis label.

我想要做的是用时间值标记 x 轴,例如 2:03:00、2:03:01、2:03:02 等。在我在网上找到的教程中,这似乎很容易;他们中的许多人使用一年中的几个月作为 x 轴标签。

However, in the swift3 update, charts now initializes values with x and y values, and I am not sure how to use labels in this version of the library. Does anyone know how to create labels in charts with swift3?

但是,在 swift3 更新中,图表现在使用 x 和 y 值初始化值,我不确定如何在此版本的库中使用标签。有谁知道如何使用 swift3 在​​图表中创建标签?

回答by Shrestha Ashesh

Proper Solution for X-Axis String labels in Swift 3+

Swift 3+ 中 X 轴字符串标签的正确解决方案

let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:months)
    barChartView.xAxis.granularity = 1

回答by Hamed

Create a class like this:

创建一个这样的类:

    import Foundation
    import Charts

    @objc(BarChartFormatter)
    class ChartFormatter:NSObject,IAxisValueFormatter{

    var months: [String]! = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    return months[Int(value)]
    }

}

Set the x-Axis like this:

像这样设置 x 轴:

    let xAxis=XAxis()
    let chartFormmater=ChartFormatter()

    for i in index{
        chartFormmater.stringForValue(Double(i), axis: xAxis)
    }

    xAxis.valueFormatter=chartFormmater
    chartView.xAxis.valueFormatter=xAxis.valueFormatter

回答by biloshkurskyi.ss

I propose to use more flexible and pretty solution. You should have a formatter class like this:

我建议使用更灵活和漂亮的解决方案。你应该有一个像这样的格式化程序类:

final class MonthNameFormater: NSObject, IAxisValueFormatter {
    func stringForValue( _ value: Double, axis _: AxisBase?) -> String {
        return Calendar.current.shortMonthSymbols[Int(value)]
    }
}

Set up this type to your barChartView:

将此类型设置为您的barChartView

barChartView.xAxis.valueFormatter = MonthNameFormater()
barChartView.xAxis.granularity = 1.0

As a result you will have: Jan, Feb, ..., DecIn addition if you want to change presentation kind you should change shortMonthSymbols with some other: monthSymbols, veryShortMonthSymbols, standaloneMonthSymbols

因此,您将拥有:Jan, Feb, ..., Dec此外,如果您想更改演示文稿类型,您应该将 shortMonthSymbols 更改为其他一些:monthSymbols, veryShortMonthSymbols, standaloneMonthSymbols

回答by MHCsk

Swift 4.2

斯威夫特 4.2

I'm having pretty good results using standard DateFormatter()I'm converting UNIX timestamp to format day.month

我使用标准得到了很好的结果DateFormatter()我正在将 UNIX 时间戳转换为格式 day.month

final class XAxisNameFormater: NSObject, IAxisValueFormatter {

    func stringForValue( _ value: Double, axis _: AxisBase?) -> String {

        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.dateFormat = "dd.MM"

        return formatter.string(from: Date(timeIntervalSince1970: value))
    }

}

Usage:

用法:

self.lineChartView.xAxis.valueFormatter = XAxisNameFormater()
self.lineChartView.xAxis.granularity = 1.0

回答by Bhavesh Patel

You can do this by creating your own formatter for the time. Below I mention sample code for that for your reference.

您可以通过创建自己的格式化程序来做到这一点。下面我提到了示例代码供您参考。

    public class DateValueFormatter: NSObject, AxisValueFormatter {
    private let dateFormatter = DateFormatter()

    override init() {
        super.init()
        dateFormatter.dateFormat = "HH:mm:ss"
    }

    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return dateFormatter.string(from: Date(timeIntervalSince1970: value))
    }
}

To set your value just use below code.

要设置您的值,只需使用以下代码。

self.yourChart.xAxis.valueFormatter = DateValueFormatter()
self.yourChart.xAxis.granularity = 1.0