codechicksrock
codechicksrock

Reputation: 301

Array to data Swift

I am using "SwiftChart" library. Im uploading x and y axis to the chart.

1.) I do not know how to upload the values properly to the array

self.chartArray.insert(grChartStruct(x: xFloat, y: bearishFloatY), at: 0)

2.) I neeed to tranform that array to data (as shown below)

series = ChartSeries(data: chartArray[indexPath])

I do not want to upload these like this because im going to have to change the data

let data = [(x: 0.0, y: 0)

My code which needs fixing.

struct grChartStruct {
        let x : Float
        let y : Float

    }

       var chartArray = [grChartStruct]()


            let bearishFloat = 1.1
            let bullishFloat = 0.0
            var xFloat = 0.0

            self.chartArray.insert(grChartStruct(x: xFloat, y: bearishFloatY), at: 0)

         series = ChartSeries(data: chartArray[indexPath])

The class(library) im using

open class ChartSeries {
    open var data: Array<(x: Float, y: Float)>
    open var area: Bool = false
    open var line: Bool = true
    open var color: UIColor = ChartColors.blueColor() {
        didSet {
            colors = (above: color, below: color, 0)
        }
    }
    open var colors: (above: UIColor, below: UIColor, zeroLevel: Float) = (above: ChartColors.blueColor(), below: ChartColors.redColor(), 0)

    public init(_ data: Array<Float>) {
        self.data = []

        data.enumerated().forEach { (x, y) in
            let point: (x: Float, y: Float) = (x: Float(x), y: y)
            self.data.append(point)
        }
    }

    public init(data: Array<(x: Float, y: Float)>) {
        self.data = data
    }

    public init(data: Array<(x: Double, y: Double)>) {
        self.data = data.map ({ (Float($0.x), Float($0.y))})
    }
}   

Upvotes: 0

Views: 420

Answers (1)

Dave Weston
Dave Weston

Reputation: 6635

The type of data that ChartSeries is expecting is an array of tuples where each tuple is two Floats. So, in this case, you don't need a grChartStruct type. This should compile:

   var chartArray = [(x: Float, y: Float)]()

        let bearishFloat = 1.1
        let bullishFloat = 0.0
        var xFloat = 0.0

        chartArray.append((xFloat, bearishFloatY))

     series = ChartSeries(data: chartArray)

However, I'm concerned by the reference to indexPath in the last line of your code snippet. Are you displaying a number of different charts in a UITableView?

Upvotes: 1

Related Questions