Daniel Gindiがios-chartsライブラリを使用して棒グラフを作成しようとしています。BarChartViewのサイズに棒グラフが適用されない
赤い背景を持つBarChartViewをプログラムで作成し、ハードコードされたデータで埋めます。ビューのサイズは正しく設定されていますが、棒グラフは適切に拡大縮小されず、ビューの最上部に到達すると切り捨てられます。
私の見解では、こののようになります。
このビューコントローラは、ストーリーボード方式instantiateViewControllerWithIdentifier
を使用してscrollviewの内側にインスタンス化されます。しかし、私はこのビューがコントローラ初期ビューコントローラ作るとき、グラフが正しくスケーリングし、そして次のようになります。私のグラフのスケールが間違ってない理由
?私も提供します
:
また、私は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)
}