2016-06-18 1 views
0

Daniel Gindiがios-chartsライブラリを使用して棒グラフを作成しようとしています。BarChartViewのサイズに棒グラフが適用されない

赤い背景を持つBarChartViewをプログラムで作成し、ハードコードされたデータで埋めます。ビューのサイズは正しく設定されていますが、棒グラフは適切に拡大縮小されず、ビューの最上部に到達すると切り捨てられます。

私の見解では、こののようになります。

enter image description here

このビューコントローラは、ストーリーボード方式instantiateViewControllerWithIdentifierを使用してscrollviewの内側にインスタンス化されます。しかし、私はこのビューがコントローラ初期ビューコントローラ作るとき、グラフが正しくスケーリングし、そして次のようになります。私のグラフのスケールが間違ってない理由

enter image description here

?私も提供します

enter image description here

また、私は100のように、大規模なものに誤っスケールグラフのleftAxis.axisMaxValueプロパティを設定した場合、グラフは次のようになりますので注意したいと思いますグラフを作成するために使用したコードから、プロパティとグラフのデータを設定するために使用した30以上の行を除いたものです。

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() 
} 

有用であれば追加情報を提供できます。

何が問題なのでしょうか?または次に試すものは?

EDIT:

私はscrollview内部ビュー・コントローラをインスタンス、私はそのようなその左境界がscrollviewの左境界から2*self.view.frame.widthでその位置にNSLayoutConstraintsを使用します。

グラフを持つビューコントローラがスクロールビューの一番左のフレームに表示されるように、その制約を0に設定すると、グラフが正しく表示されます。しかし、その制約を(1つの単位のように)変更した場合、グラフの縮尺が再び正しくなります。

func setUpQuestionFrame(newViewController: UIViewController){ 

    var frame = newViewController.view.frame 
    frame.origin.x = self.view.frame.size.width*2 
    newViewController.view.frame = frame 

    self.addChildViewController(newViewController) 
    self.scrollView.addSubview(newViewController.view) 
    newViewController.didMoveToParentViewController(self) 

    newViewController.view.translatesAutoresizingMaskIntoConstraints = false 

    let widthConstraintVCBAR = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: newViewController.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0) 
    view.addConstraint(widthConstraintVCBAR) 

    let heightConstraintVCBAR = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: newViewController.view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0) 
    view.addConstraint(heightConstraintVCBAR) 

    let horizontalConstraintVCBAR = NSLayoutConstraint(item: newViewController.view, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 5)//self.view.frame.width*2) 
    view.addConstraint(horizontalConstraintVCBAR) 

    let verticalConstraintVCBAR = NSLayoutConstraint(item: newViewController.view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0) 
    view.addConstraint(verticalConstraintVCBAR) 
} 

答えて

0

問題が解決された再構成するチャートを指示​​を追加することによって:

Iは、上記ストーリーボードの方法を使用してビュー・コントローラをインスタンス化直後、私は、そのコードを以下に示している方法を使用して位置します新しいデータを受信すると

this質問で使用されたのと同じ解決策でした。

関連する問題