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

感情迁移 提交于 2020-12-04 09:39:21

问题


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


回答1:


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];



来源:https://stackoverflow.com/questions/43096731/ios-charts-library-how-to-handle-x-axis-duplicate-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!