Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with order of x-values and Multi LineChart #2474

Closed
casim opened this issue May 25, 2017 · 3 comments
Closed

Issue with order of x-values and Multi LineChart #2474

casim opened this issue May 25, 2017 · 3 comments

Comments

@casim
Copy link

casim commented May 25, 2017

I'm still using swift 2 so I use Charts 2.3.1
I have to load some measurement data (Waist, Weight, Hip) that the user is adding. Something like this:

screen shot 2017-05-25 at 12 17 15

To display the data correctly with an ascending date order (x-value) I have to order the xValues and also partially order the yValues as well. If I don't order the yValue as well some data are not shown or the line chart is plotted wrong.

I'm slightly confused on why do I need to to do the second step for each yVals dataset given that I already provide an ordered xVals and the correct index.

Any help would be appreciated, I'm moving torward dynamicaly load different datasets so the code would be much easier to understand if I only have to order the xVals.

@casim
Copy link
Author

casim commented May 25, 2017

This is the code with the changes I made to fix it commented out

        typealias entry = (label: String, value: Double) 
        let labels = ["04/20","04/22", "04/23","04/24", "04/26", "05/02", "05/08", "05/15"]
        let weights:[entry] = [(label: "04/22", value: 84.5),
                               (label: "05/15", value: 85.6),
                               (label: "04/24", value: 85),
                               (label: "04/20", value: 84.2),
                               (label: "05/02", value: 86)]
        let waists:[entry] = [(label: "04/26", value: 86),
                               (label: "05/15", value: 86.5),
                               (label: "05/08", value: 86.5),
                               (label: "04/23", value: 86.2)]

        
        // 1 - creating an array of data entries
        var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
        for weight in weights {
            yVals1.append(ChartDataEntry(value: weight.value, xIndex: labels.indexOf(weight.label)!))
        }
        
        // FIX: append DataEntry based on label order
        //        for label in labels {
        //            let optEntry = weights.filter {$0.label == label}.first
        //            if let entry = optEntry {
        //                   yVals1.append(ChartDataEntry(value: entry.value, xIndex: labels.indexOf(entry.label)!))
        //            }
        //            
        //        }

        let set1: LineChartDataSet = LineChartDataSet(yVals: yVals1, label: "First Set")
        set1.axisDependency = .Left // Line will correlate with left axis values
        set1.setColor(UIColor.redColor().colorWithAlphaComponent(0.5))
        set1.setCircleColor(UIColor.redColor())
        set1.lineWidth = 2.0
        set1.circleRadius = 6.0
        set1.fillAlpha = 65 / 255.0
        set1.fillColor = UIColor.redColor()
        set1.highlightColor = UIColor.whiteColor()
        set1.drawCircleHoleEnabled = true
        
        var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
        for waist in waists {
            yVals2.append(ChartDataEntry(value: waist.value, xIndex: labels.indexOf(waist.label)!))
        }
        
        // FIX: append DataEntry based on label order
        //        for label in labels {
        //            let optEntry = waists.filter {$0.label == label}.first
        //            if let entry = optEntry {
        //                yVals2.append(ChartDataEntry(value: entry.value, xIndex: labels.indexOf(entry.label)!))
        //            }
        //            
        //        }

        let set2: LineChartDataSet = LineChartDataSet(yVals: yVals2, label: "Second Set")
        set2.axisDependency = .Left // Line will correlate with left axis values
        set2.setColor(UIColor.greenColor().colorWithAlphaComponent(0.5))
        set2.setCircleColor(UIColor.greenColor())
        set2.lineWidth = 2.0
        set2.circleRadius = 6.0
        set2.fillAlpha = 65 / 255.0
        set2.fillColor = UIColor.greenColor()
        set2.highlightColor = UIColor.whiteColor()
        set2.drawCircleHoleEnabled = true
        
        //3 - create an array to store our LineChartDataSets
        var dataSets : [LineChartDataSet] = [LineChartDataSet]()
        dataSets.append(set1)
        dataSets.append(set2)

        //4 - pass our months in for our x-axis label value along with our dataSets
        let data: LineChartData = LineChartData(xVals: labels, dataSets: dataSets)
        
        //5 - finally set our data
        let lineChart = LineChartView(frame: CGRect.init(x: 0, y: 50, width: 350, height: 200))
        lineChart.descriptionText = ""
        lineChart.descriptionTextColor = UIColor.blueColor()
        lineChart.drawGridBackgroundEnabled = true
        lineChart.gridBackgroundColor = UIColor.darkGrayColor()
        lineChart.noDataText = "No data provided"
        lineChart.delegate = self
        lineChart.data = data

@liuxuan30
Copy link
Member

liuxuan30 commented May 26, 2017

2.x is not maintained anymore. so you are on your own and fix any bugs you found.

@casim
Copy link
Author

casim commented May 26, 2017

@liuxuan30 if you have any pointers on where I should start looking, it would be appriated. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants