如何拉伸PieChartView父视图的整个宽度和长度

时间:2016-04-22 07:37:42

标签: ios swift ios-charts

我使用以下charts library并拥有以下自定义控件:

enter image description here

但是在图表加载后我得到了以下图片:

enter image description here

显然,PieChartView的尺寸大于输出图表。我的问题是如何使最终图表占据整个PieChartView?

3 个答案:

答案 0 :(得分:3)

找到解决方案。 配置PieChartDataSet时

let set = PieChartDataSet(...)
//configuring

并将该组的selectionShift设置为零(将禁用切片的选择)

set.selectionShift = 0

答案 1 :(得分:0)

如果有帮助,请尝试使用以下代码。

pieChartView.contentMode = .ScaleAspectFill

答案 2 :(得分:0)

你可以尝试

PieChartView.setExtraOffsets(left: -20, top: -20, right: -20, bottom: -20)

已编辑

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var pieChartView: PieChartView!
    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]

    setChart(months, values: unitsSold)

}


func setChart(dataPoints: [String], values: [Double]) {

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
    let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
    pieChartView.data = pieChartData

    pieChartView.setExtraOffsets(left: -50, top: -50, right: -50, bottom: -50)

    var colors: [UIColor] = []

    for i in 0..<dataPoints.count {
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))

        let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
        colors.append(color)
    }

    pieChartDataSet.colors = colors


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

Download code

使用 - pieChartView.setExtraOffsets(左:-50,顶部:-50,右:-50,底部:-50)

没有 - pieChartView.setExtraOffsets(左:-50,顶部:-50,右:-50,底部:-50)

请参阅随附的屏幕截图。 enter image description here

enter image description here