2017-08-14 169 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