2017-03-01 40 views
1

我想繪製一個餅圖並想顯示標籤,可以說是「紅色」和「藍色」。如何使用iOS圖表庫在PieChart中顯示字符串標籤?

紅色的有100 藍的值具有200

碼的值是這樣的:

var colorEntries: [ChartDataEntry] = [] 
colorEntries.append(ChartDataEntry(x: 0, y: 100)) // 100 Red entires 
colorEntries.append(ChartDataEntry(x: 1, y: 200)) // 200 Blue entries 

// pie chart 
let pieChartDataSet = PieChartDataSet(values: colorEntries.append, label: "Color Chart") 
let pieChartData = PieChartData(dataSet: pieChartDataSet) 
self.pieChart.data = pieChartData 

// I'm not very sure how these colors are mapped to the dataset. 
let colors: [UIColor] = [UIColor(red: CGFloat(1), green: 0, blue: 0, alpha: 1), 
         UIColor(red: 0, green: 0, blue: CGFloat(1), alpha: 1)] 
pieChartDataSet.colors = colors 

以下是圖表框架:https://github.com/danielgindi/Charts

有一些不錯的那裏的網絡教程。但他們已經過時了。 Charts框架的最新版本引入了重大變化(看起來相當不直觀)。

發行說明:https://github.com/danielgindi/Charts/releases/tag/v3.0.0

+0

你想在哪裏添加標籤? –

+0

在餅圖部分,就像默認數字標籤一樣。 – AlvinfromDiaspar

回答

2

所以基本上你通過你的數據點的列表和值(字符串爲您的餅圖切片的數組)(這是相關值的對應陣列創建ChartDataEntry的數組,然後你循環與部分的字符串)。

var dataEntries: [ChartDataEntry] = [] 
for i in 0..<dataPoints.count { 
    let entry = PieChartDataEntry(value: Double(values[i]), label: dataPoints[i], data: dataEntries[i] as AnyObject) 
    dataEntries.append(entry) 
} 

let pieChartDataSet = PieChartDataSet(values: dataEntries, label: nil) 
let pieChartData = PieChartData(dataSet: pieChartDataSet) 

self.data = pieChartData 
相關問題