Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 182 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set x-axis labels with ios charts

#1
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.

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.

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?
Reply

#2
**Swift 5.5**

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

final class XAxisNameFormatter: NSObject, AxisValueFormatter {

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
Reply

#3
I had problems creating the class as the previous answers

> 'No type or protocol named 'IChartAxisValueFormatter'

I removed the `NSObject` inheritance and left only `IAxisValueFormatter` like here:

final class XAxisNameFormater: IAxisValueFormatter {.....
Reply

#4
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

Reply

#5
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.xAxis.valueFormatter = MonthNameFormater()
barChartView.xAxis.granularity = 1.0

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

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

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

#7
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:

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

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

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


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through