我目前使用Charts - by Daniel Gindi,并按照他的演示中,我已经实现了一个饼图,像这样:iOS图表:显示PieChart内实际值的百分比?
这是实现使用下面的代码:
func createPieChart(dataPoints: [String: Double])
{
for index in 0..<sortedDates.count
{
let year = sortedDates[index]
guard let costs = dataPoints[year] else {
return
}
let dataEntry = PieChartDataEntry(value: costs, label: year)
dataEntries.append(dataEntry)
}
chartDataSet = PieChartDataSet(values: dataEntries, label: "")
chartData = PieChartData(dataSet: chartDataSet)
chartDataSet.sliceSpace = 2.0
chartDataSet.yValuePosition = PieChartDataSet.ValuePosition.outsideSlice
...
pieChartView.usePercentValuesEnabled = true
pieChartView.drawSlicesUnderHoleEnabled = false
pieChartView.holeRadiusPercent = 0.40
pieChartView.transparentCircleRadiusPercent = 0.43
pieChartView.drawHoleEnabled = true
pieChartView.rotationAngle = 0.0
pieChartView.rotationEnabled = true
pieChartView.highlightPerTapEnabled = false
let pieChartLegend = pieChartView.legend
pieChartLegend.horizontalAlignment = Legend.HorizontalAlignment.right
pieChartLegend.verticalAlignment = Legend.VerticalAlignment.top
pieChartLegend.orientation = Legend.Orientation.vertical
pieChartLegend.drawInside = false
pieChartLegend.yOffset = 10.0
pieChartView.legend.enabled = true
pieChartView.data = chartData
}
我” m试图实现的是在饼图片内显示实际的costs
值,但也包括片外的百分比。百分比使用chartDataSet.yValuePosition = PieChartDataSet.ValuePosition.outsideSlice
显示。
检查的代码,我注意到,在PieChartRenderer
,有就是有功能open override func drawValues(context: CGContext)
并且在它内部的代码计算每一圆形切片的valueText
百分比:
let valueText = formatter.stringForValue(
value,
entry: e,
dataSetIndex: i,
viewPortHandler: viewPortHandler)
然后使用if
检查,它借鉴内或片外的百分比,这在我的情况是外:
else if drawYOutside
{
ChartUtils.drawText(
context: context,
text: valueText,
point: CGPoint(x: 0, y: labelPoint.y + lineHeight/2.0),
align: align,
attributes: [NSFontAttributeName: valueFont, NSForegroundColorAttributeName: valueTextColor]
)
}
不过,我不能找到一种方法,实际包括costs
值TH在传入PieChartDataEntry(value: costs, label: year)
。
我被告知派生饼图并重写我发现的方法,但我不确定如何做到这一点。
例如,costs
每个索引在dataPoints
的值是100.0
为标签2017
和50.0
用于标签2016
双精度值。我想将这两个值都包含在关联的饼图片段中。
任何有此图书馆经验的人都可以帮助我吗?
谢谢!
您需要绘制图表或旁边的百分比值内你的价值? –
图表里面的年份是 – Pangu