How to stretch PieChartView the entire width and length of the parent View

家住魔仙堡 提交于 2019-12-31 02:46:07

问题


I use the following charts library and have the following custom control:

But after chart loaded i got the following picture:

Its obvious that PieChartView has greater sizes than output chart. My question is how to make final chart to occupy entire PieChartView?


回答1:


Found a solution. When configuring PieChartDataSet

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

and set selectionShift of the set to the zero (selection of the slices will be disabled)

set.selectionShift = 0



回答2:


Try this line of code if it helps..

pieChartView.contentMode = .ScaleAspectFill



回答3:


You can try

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

Edited

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

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

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

Please refer attached screen shots.



来源:https://stackoverflow.com/questions/36787773/how-to-stretch-piechartview-the-entire-width-and-length-of-the-parent-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!