问题
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