-1

So I went to use ios-Charts...

I have two data sets.

One is a time value(elapsed time for Y-axis) and one is a date value for the (x-axis)

I'm guessing that I want to use a line chart.

I'm just new to the library.

Below is a sample of data I will have:

Swims = [["Dec 1,2017",241.1],["Feb 4,2018",237.23],["Feb 21,2018",233.1],["Mar 23,2018",222.1],["Apr 1,2018",240.23],["Apr 15,2018",199.34]]

Dates will be on the X and duration in (min:sec.decimal format) on the Y axis.

Just trying to make heads or tails of how data is plotted on the ios-Charts

BostonMacOSX
  • 1,369
  • 2
  • 17
  • 38
  • Have you tryied to add this data in Charts library ? here https://github.com/danielgindi/Charts you will get all charts related question and solution that how to bind data please check it once. – CodeChanger May 21 '18 at 07:17
  • I have looked at the examples. There is no example using Xarray date data which I'm not sure how to do with the new ValueFormatter system which has been implement. All of the examples have not been updated. – BostonMacOSX May 21 '18 at 13:51
  • ok nop will help you on this but for that can you tell me your data is in above formate only means array of array or array of Dictionary ? – CodeChanger May 22 '18 at 06:28

1 Answers1

0

You need to use an array of ChartDataEntry to represent your data. ChartDataEntry takes a x and y, each being a Double.

Option 1 - Time-based x-axis

If you would like to represent data points that are horizontally spaced according to dates, you'll need to Convert your dates to TimeInterval, e.g. using Date.timeIntervalSince1970. The timeInterval from this conversion will become the x value of the data point.

There's actually an example of this in the demo project of the Charts library - LineChartTimeViewController.swift:

let now = Date().timeIntervalSince1970 // Here's the date to interval conversion
let hourSeconds: TimeInterval = 3600

let from = now - (Double(count) / 2) * hourSeconds
let to = now + (Double(count) / 2) * hourSeconds

let values = stride(from: from, to: to, by: hourSeconds).map { (x) -> ChartDataEntry in
    let y = arc4random_uniform(range) + 50
    return ChartDataEntry(x: x, y: Double(y))
}

Option 2 - Index-based x-axis

If you're fine with the data points being equally spaced, then just create the ChartDataEntry array using the indices of your original array for the x values. For example:

for (index, point) in Swim.enumerated() {
    let entry = ChartDataEntry(x: index, y: point[1])
    data.append(entry)
}

Formatting the x-axis

For either option, you'll need to implement the IAxisValueFormatter protocol in order to format the x axis labels

For option 1, you can use the value directly, converting it back to a date, then formatting it to your desired string

For option 2, the value will represent the index of the datapoint in the original array, you access it via something like:

let date = Swim[value][0]
Ryan Dias
  • 270
  • 2
  • 11