允许 PieChartView 在 Swift 中隐藏小切片的标签
Allow PieChartView to hide labels for tiny slices in Swift
我正在使用图表 iOS 框架制作图表。但当切片很小时,该值将覆盖。我怎样才能隐藏它?这个问题和这个GitHub link类似,但是我不明白它是如何工作的。我是否只需要在我的视图控制器中添加代码或将 PieChartRenderer.swift 文件拖到我的项目中?
有人可以向我解释如何使用拉取请求或一些开放的 public 功能...
抱歉,我是 iOS 框架的新手。
这是我的代码。
@IBOutlet weak var myChart: PieChartView!
var valueColors = [UIColor]()
var dataEntries = [PieChartDataEntry]()
var record = [Record]()
var category = [String]()
var categoryTotal : [Double] = []
var categoryArray : [String] = []
func setDataCount() {
valueAndColor()
let set = PieChartDataSet(values: dataEntries, label: nil)
set.colors = valueColors
set.valueLinePart1OffsetPercentage = 0.8
set.valueLinePart1Length = 0.2
set.valueLinePart2Length = 0.4
set.xValuePosition = .outsideSlice
set.yValuePosition = .outsideSlice
set.selectionShift = 0.0
let data = PieChartData(dataSet: set)
let Formatter:ChartFormatter = ChartFormatter()
data.setValueFormatter(Formatter)
data.setValueFont(.systemFont(ofSize: 11, weight: .light))
data.setValueTextColor(.black)
myChart.data = data
myChart.highlightValues(nil)
}
func setup(pieChartView chartView: PieChartView) {
chartView.usePercentValuesEnabled = true
chartView.drawSlicesUnderHoleEnabled = true
chartView.holeRadiusPercent = 0.58
chartView.chartDescription?.enabled = false
chartView.drawCenterTextEnabled = true
chartView.centerAttributedText = attributedString;
chartView.drawHoleEnabled = true
chartView.rotationAngle = 0
chartView.rotationEnabled = true
chartView.highlightPerTapEnabled = true
}
func valueAndColor(){
for i in 0..<categoryArray.count{
let dataEntry = PieChartDataEntry(value: categoryTotal[i], label: categoryArray[i % categoryArray.count])
dataEntries.append(dataEntry)
if categoryArray[i] == "吃喝" {
valueColors.append(UIColor.yellow)
}else if categoryArray[i] == "交通"{
valueColors.append(UIColor.red)
}...
}
创建一个自定义格式化程序,我将minNumber设置为10.0,当值小于minNumber时返回空字符串,否则返回值。
public class ChartFormatter: NSObject, IValueFormatter{
public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
let total = UserDefaults.standard.double(forKey: "totalValue")
var valueToUse = value/total * 100
valueToUse = Double(round(10*valueToUse)/10)
print("valueToUse: \(valueToUse)")
let minNumber = 10.0
if(valueToUse<minNumber) {
return ""
}
else {
return String(valueToUse) + "%"
}
}
}
然后确保设置 totalValue 变量,将其存储在 UserDefaults 中(以便可以在格式化程序中访问它)并为图表设置格式化程序
var totalValue = 0.0
let units = [10.0, 4.0, 6.0, 3.0, 12.0, 16.0]
for a in units {
totalValue += a
}
UserDefaults.standard.set(totalValue, forKey: "totalValue")
let formatter:ChartFormatter = ChartFormatter()
data.setValueFormatter(formatter)
结果:
较新版本的 Charts 库添加了此功能并使其变得简单 属性 以在 PieChartView
:
的实例上设置
pieChart.sliceTextDrawingThreshold = 20
sliceTextDrawingThreshold
属性 设置绘制标签所需的最小角度。
我正在使用图表 iOS 框架制作图表。但当切片很小时,该值将覆盖。我怎样才能隐藏它?这个问题和这个GitHub link类似,但是我不明白它是如何工作的。我是否只需要在我的视图控制器中添加代码或将 PieChartRenderer.swift 文件拖到我的项目中?
有人可以向我解释如何使用拉取请求或一些开放的 public 功能...
抱歉,我是 iOS 框架的新手。
这是我的代码。
@IBOutlet weak var myChart: PieChartView!
var valueColors = [UIColor]()
var dataEntries = [PieChartDataEntry]()
var record = [Record]()
var category = [String]()
var categoryTotal : [Double] = []
var categoryArray : [String] = []
func setDataCount() {
valueAndColor()
let set = PieChartDataSet(values: dataEntries, label: nil)
set.colors = valueColors
set.valueLinePart1OffsetPercentage = 0.8
set.valueLinePart1Length = 0.2
set.valueLinePart2Length = 0.4
set.xValuePosition = .outsideSlice
set.yValuePosition = .outsideSlice
set.selectionShift = 0.0
let data = PieChartData(dataSet: set)
let Formatter:ChartFormatter = ChartFormatter()
data.setValueFormatter(Formatter)
data.setValueFont(.systemFont(ofSize: 11, weight: .light))
data.setValueTextColor(.black)
myChart.data = data
myChart.highlightValues(nil)
}
func setup(pieChartView chartView: PieChartView) {
chartView.usePercentValuesEnabled = true
chartView.drawSlicesUnderHoleEnabled = true
chartView.holeRadiusPercent = 0.58
chartView.chartDescription?.enabled = false
chartView.drawCenterTextEnabled = true
chartView.centerAttributedText = attributedString;
chartView.drawHoleEnabled = true
chartView.rotationAngle = 0
chartView.rotationEnabled = true
chartView.highlightPerTapEnabled = true
}
func valueAndColor(){
for i in 0..<categoryArray.count{
let dataEntry = PieChartDataEntry(value: categoryTotal[i], label: categoryArray[i % categoryArray.count])
dataEntries.append(dataEntry)
if categoryArray[i] == "吃喝" {
valueColors.append(UIColor.yellow)
}else if categoryArray[i] == "交通"{
valueColors.append(UIColor.red)
}...
}
创建一个自定义格式化程序,我将minNumber设置为10.0,当值小于minNumber时返回空字符串,否则返回值。
public class ChartFormatter: NSObject, IValueFormatter{
public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
let total = UserDefaults.standard.double(forKey: "totalValue")
var valueToUse = value/total * 100
valueToUse = Double(round(10*valueToUse)/10)
print("valueToUse: \(valueToUse)")
let minNumber = 10.0
if(valueToUse<minNumber) {
return ""
}
else {
return String(valueToUse) + "%"
}
}
}
然后确保设置 totalValue 变量,将其存储在 UserDefaults 中(以便可以在格式化程序中访问它)并为图表设置格式化程序
var totalValue = 0.0
let units = [10.0, 4.0, 6.0, 3.0, 12.0, 16.0]
for a in units {
totalValue += a
}
UserDefaults.standard.set(totalValue, forKey: "totalValue")
let formatter:ChartFormatter = ChartFormatter()
data.setValueFormatter(formatter)
结果:
较新版本的 Charts 库添加了此功能并使其变得简单 属性 以在 PieChartView
:
pieChart.sliceTextDrawingThreshold = 20
sliceTextDrawingThreshold
属性 设置绘制标签所需的最小角度。