如何拉伸PieChartView父View的整个宽度和长度
How to stretch PieChartView the entire width and length of the parent View
我使用以下 charts library 并具有以下自定义控件:
但是加载图表后我得到了下图:
很明显 PieChartView 的尺寸比输出图表大。我的问题是如何让最终图表占据整个 PieChartView?
试试这行代码,如果有帮助的话..
pieChartView.contentMode = .ScaleAspectFill
你可以试试
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.
}
}
有 - pieChartView.setExtraOffsets(左:-50,上:-50,右:-50,下:-50)
没有 - pieChartView.setExtraOffsets(左:-50,上:-50,右:-50,下:-50)
请参阅随附的屏幕截图。
找到解决办法。
配置 PieChartDataSet 时
let set = PieChartDataSet(...)
//configuring
并将集合的 selectionShift 设置为零(切片的选择将被禁用)
set.selectionShift = 0
我使用以下 charts library 并具有以下自定义控件:
但是加载图表后我得到了下图:
很明显 PieChartView 的尺寸比输出图表大。我的问题是如何让最终图表占据整个 PieChartView?
试试这行代码,如果有帮助的话..
pieChartView.contentMode = .ScaleAspectFill
你可以试试
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.
}
}
有 - pieChartView.setExtraOffsets(左:-50,上:-50,右:-50,下:-50)
没有 - pieChartView.setExtraOffsets(左:-50,上:-50,右:-50,下:-50)
请参阅随附的屏幕截图。
找到解决办法。 配置 PieChartDataSet 时
let set = PieChartDataSet(...)
//configuring
并将集合的 selectionShift 设置为零(切片的选择将被禁用)
set.selectionShift = 0