samedi 18 juin 2016

Bar Chart not scaling to size of BarChartView


I am using the ios-charts library by Daniel Gindi to try and create a bar chart.

I programatically create a BarChartView with a red background, and fill it with hard-coded data. The view is sized correctly, however the bar chart doesn't scale properly and is cut off when it reaches the top of the view.

My view look like this:

enter image description here

This view controller is instantiated using the storyboard method instantiateViewControllerWithIdentifier. However when I make this view controller the initial view controller, the chart scales correctly, and looks like this:

enter image description here

Why does my chart scale incorrectly?

I would also like to note that if I set the leftAxis.axisMaxValue property of the incorrectly-scaled graph to something large, like 100, the graph looks like this:

enter image description here

I will also provide the code I used to create the graph, minus the 30+ lines I used to set properties and the data of the graph.

override func viewDidLoad(){
    var chart : UIView?
    let gBFrame = self.graphBackground.frame
    let frame = CGRect(origin: gBFrame.origin, size: CGSize(width: gBFrame.width, height: gBFrame.height-25))
    chart = BarChartView(frame: frame)
    self.view.addSubview(chart!)
    constrainChart()
}

func constrainChart(){

    if type == "Bar Chart"{            
        chart!.translatesAutoresizingMaskIntoConstraints = false
        let leftConstraint = NSLayoutConstraint(item: self.chart!, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.graphBackground, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0)
        let rightConstraint = NSLayoutConstraint(item: self.chart!, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.graphBackground, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
        let topConstraint = NSLayoutConstraint(item: self.chart!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.graphBackground, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
        let bottomConstraint = NSLayoutConstraint(item: self.chart!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.graphBackground, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -25)
        self.view.addConstraints([leftConstraint,rightConstraint,topConstraint,bottomConstraint])
    } else if type == "Horizontal Bar Chart"{
    } else if type == "Pie Chart"{
    } else {

    }
    chart?.layoutIfNeeded()
}

I can provide any additional information if it is helpful.

Any ideas on what could be the problem? Or what to try next?


Aucun commentaire:

Enregistrer un commentaire