我目前使用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