Date formatter in value formatter for x-axis causing duplicate labels
- Dominant language
- Swift
- Stars
- 28k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
## What did you do?
Created a custom value formatter for x-axis in a multiline chart.
## What did you expect to happen?
I expected the identical x-axis labels on the x-axis to be combined if two or more lines shared the same label, resulting in non-duplicate, chronological labels. i.e. 08/24, 08/25, 0/8/26, 08/27
## What happened instead?
Each data set has its own x-axis labels independently of other data sets, resulting in duplicate labels.

## Charts Environment
**Charts version/Branch/Commit Number:** 3.5.0
**Xcode version:** 11.5
**Swift version:** 5.x
**Platform(s) running Charts:** iOS
**macOS version running Xcode:** 10.15.6
## Demo Project
**Chart setup**
```
let lineChartView = LineChartView()
lineChartView.data = loadMetricsData()
lineChartView.rightAxis.enabled = false
lineChartView.pinchZoomEnabled = true
lineChartView.dragEnabled = true
lineChartView.setScaleEnabled(true)
lineChartView.drawBordersEnabled = false
lineChartView.delegate = self
let yAxis = lineChartView.leftAxis
yAxis.labelFont = .boldSystemFont(ofSize: 12)
yAxis.setLabelCount(6, force: false)
yAxis.labelTextColor = UIColor(red: 0, green: 0, blue: 255/255, alpha: 1.0)
yAxis.axisLineColor = UIColor(white: 0.2, alpha: 0.4)
yAxis.labelPosition = .outsideChart
yAxis.gridColor = UIColor(white: 0.8, alpha: 0.4)
let xAxis = lineChartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .boldSystemFont(ofSize: 10)
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.drawLimitLinesBehindDataEnabled = true
xAxis.drawAxisLineEnabled = false
xAxis.avoidFirstLastClippingEnabled = true
xAxis.granularityEnabled = true
xAxis.granularity = 1
xAxis.valueFormatter = ChartXAxisFormatter(usingMetrics: metricsArr)
```
**Value Formatter**
```
class ChartXAxisFormatter: NSObject, IAxisValueFormatter {
private var metricsArr: [Metric]?
private var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("MM/dd")
return dateFormatter
}()
convenience init(usingMetrics metricsArr: [Metric]) {
self.init()
self.metricsArr = metricsArr
}
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let index = Int(value)
guard let metricsArr = metricsArr, index < metricsArr.count else {
return "?"
}
let date = metricsArr[index].date
return dateFormatter.string(from: date)
}
}
```
When I log `dateFormatter.string(from: date)`, it shows two "8/24"'s and two "8/25"'s.
If I do without the custom value formatter, the labels on the x-axis are non-duplicate:

Contributor guide
Assessment
This issue has not been assessed yet.