2017-08-14 271 views
0

我目前使用Charts - by Daniel Gindi,並按照他的演示中,我已經實現了一個餅圖,像這樣:iOS圖表:顯示PieChart內實際值的百分比?

enter image description here

這是實現使用下面的代碼:

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爲標籤201750.0用於標籤2016雙精度值。我想將這兩個值都包含在關聯的餅圖片段中。

任何有此圖書館經驗的人都可以幫助我嗎?

謝謝!

+0

您需要繪製圖表或旁邊的百分比值內你的價值? –

+0

圖表裏面的年份是 – Pangu

回答

1

創建從PieChartRenderer繼承的類,並從父類覆蓋drawValues像這樣:

public class CustomClass: PieChartRenderer 
{ 
    override public func drawValues(context: CGContext) 
    { 
     // In this body, copy ALL code from parent class drawValues function 

     ... 
     let decimalFormatter = NumberFormatter() 
     decimalFormatter.minimumFractionDigits = 2 
     decimalFormatter.maximumFractionDigits = 2 
     decimalFormatter.positivePrefix = "$" 

     ... 

     for i in 0 ..< dataSets.count 
     { 
      ... 

      for j in 0 ..< dataSet.entryCount 
      { 
       ... 

       guard let e = dataSet.entryForIndex(j) else { continue } 
       let pe = e as? PieChartDataEntry 

       // e.y = the actual value in the costs array, not the percentage 
       let twoDecimalValue = NSDecimalNumber(value: e.y) 
       let totalCost = decimalFormatter.string(from: twoDecimalValue) 

       ... 

       if drawXInside || drawYInside 
       { 
        ... 

        if drawXInside && drawYInside 
        { 
         ... 
        } 
        else if drawXInside 
        { 
         if j < data.entryCount && pe?.label != nil 
         { 
          ChartUtils.drawText(
           context: context, 
           text: pe!.label!, 
           point: CGPoint(x: x, 
               y: y), 
           align: .center, 
           attributes: [NSFontAttributeName: yearValueFont, 
              NSForegroundColorAttributeName: yearTextColor ?? percentageTextColor] 
          ) 

          ChartUtils.drawText(
           context: context, 
           text: totalCost!, 
           point: CGPoint(x: x, 
               y: y + lineHeight), 
           align: .center, 
           attributes: [NSFontAttributeName: yearCostsValueFont, 
              NSForegroundColorAttributeName: yearCostsTextColor] 
          ) 
         } 
        } 
       } 
      } 
     } 
    } 
} 

else if drawXInside,第一現有drawText功能繪製label值,在我的情況, 。添加另一個drawText函數並傳入totalCost值,該值表示數組成本中的實際值值。

然後createPieChart函數中:

func createPieChart(dataPoints: [String: Double]) 
{ 
    ... 

    pieChartView.data = chartData 

    let customClass = CustomClass(chart: pieChartView, 
            animator: pieChartView.chartAnimator, 
            viewPortHandler: pieChartView.viewPortHandler) 
    pieChartView.renderer = customRenderer 
} 

結果:

enter image description here

+0

可以提供你如何創建此圖表的示例@Pangu –

+0

該示例在答案中提供了'創建一個繼承自PieChartRenderer的類並覆蓋從父類中抽取值,如下所示:' – Pangu

+0

我試過但沒有爲我工作@Pangu –

-1

您需要yValuePosition屬性更改爲insideSlice

pieChartDataSet.yValuePosition = PieChartDataSet.ValuePosition.insideSlice 

默認情況下,它的內部,而是指定爲出側,其顯示的圖表之外。

編輯:

按照評論請檢查下面的代碼顯示在餅圖

func setPieChartData(lables: [String]!, values:[Double]!) { 

     var valuesArray: [PieChartDataEntry] = [] 
     var colors: [UIColor] = [] 
     let valNSArray = values as NSArray 

     valNSArray.enumerateObjects({ object, index, stop in 
      //your code 
      colors.append(self.getRandomColor()) 


      valuesArray.append(PieChartDataEntry(value: object as! Double, label: lables[index])) 

     }) 

     let pieChartDataSet = PieChartDataSet(values: valuesArray, label: "Pie Data Chart") 
     pieChartDataSet.colors = colors 

     let piechartData = PieChartData(dataSets:[pieChartDataSet]) 
     pieChartDataSet.yValuePosition = PieChartDataSet.ValuePosition.insideSlice 
     self.data = piechartData 



} 

enter image description here

希望這將有助於值而不是百分比。

+0

請再次仔細閱讀問題... – Pangu