MilanPanchal
MilanPanchal

Reputation: 2953

iOS Charts Library - How to handle X-Axis duplicate values

I have used Charts library by @danielgindi to generate the graph for my iOS application. All seems works fine but we are facing issue with X-Axis data duplication.

Input Data for Graph in (X-Axis-Value, Y-Axis-Value)

("29/03/2017 00:00:00","2.7")

("29/03/2017 00:00:00","2.4")

("29/03/2017 00:10:00","1.3")

("29/03/2017 00:10:00","1.5")

("29/03/2017 00:20:00","1.8")

....

....

....

("29/03/2017 01:00:00","1.2")

("29/03/2017 09:00:00","2.7")

("29/03/2017 09:10:00","-10.8")

....

....

....

("29/03/2017 13:10:00","3.9")

("29/03/2017 13:20:00","-.8")

("29/03/2017 13:20:00","5.9"

Where X-Axis value is date time and Y Axis value is Double value associated to that date-time.

I have used following code for the formatting X-Axis

class ChartStringFormatter: NSObject, IAxisValueFormatter {

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

        let date = Date(timeIntervalSince1970: value)
        return date.toString(format: "hh:mm a")
    }
}

But when I zoom the graph it has multiple values for X-Axis. How should I resolved this issues?

Expected Output For X Axis Value:

Initially - [12:00 AM, 03:00 AM, 06:00 AM, 09:00 AM, 12:00 PM, 03:00 PM, 06:00 PM, 09:00 PM, 12:00 PM]

When Zoom - [12:00 AM, 01:00 AM, 02:00 AM, 03:00 AM ...... 12:00 PM]

Again Zoom - [12:00 AM, 12:30 AM, 01:00 AM, 01:30AM, 02:00 AM, 02:30 AM, 03:00 AM ...... 12:00 PM]

Again Again Zoom - [12:00 AM, 12:10 AM, 12:20 AM, 12:30 AM ...... 12:00 PM]

Sample GIF file:

iOS Charts Library Link: https://github.com/danielgindi/Charts

Upvotes: 1

Views: 1926

Answers (2)

Muhammad Abbas
Muhammad Abbas

Reputation: 496

Add the following two properties in your chart then it will solve your problem

chart.xAxis.granularityEnabled = true
chart.xAxis.granularity = 1

Remember not to use this property in your chart if you want to remove duplication

chart.xAxis.forceLabelsEnabled = true

Upvotes: 1

MKaro
MKaro

Reputation: 156

As mentioned in iOS-Charts library documentation:

When true, axis labels are controlled by the granularity property.

When false, axis values could possibly be repeated.

...

@property (nonatomic) BOOL granularityEnabled;

and:

The minimum interval between axis values.

This can be used to avoid label duplicating when zooming in.

default: 1.0

@property (nonatomic) double granularity;

So Im pretty sure that those lines will do the work for you:

[yourChart.xAxis setGranularityEnabled:YES];

[yourChart.xAxis setGranularity:1.0];

Upvotes: 3

Related Questions