ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.51k stars 5.99k forks source link

Date formatter in value formatter for x-axis causing duplicate labels #4458

Open absenceofnunchi opened 4 years ago

absenceofnunchi commented 4 years ago

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.

Screen Shot 2020-08-27 at 5 45 24 PM

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: Screen Shot 2020-08-27 at 5 54 49 PM

kinber95 commented 2 years ago

@igibliss00 do you found a solution for your problem?