ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.62k stars 6k forks source link

Not seeing colors in PieChart #2097

Open bernhardwaidacher opened 7 years ago

bernhardwaidacher commented 7 years ago

I'm not seeing any colors in my PieChart. This is the code:

  func showPieChart(){
    pieChartView.isHidden = false
    tableView.isHidden = true
    searchbar.isHidden = true

    let transactions = manager.transactions.value
    let grouped = transactions.categorise({$0.category})
    var entries:[PieChartDataEntry] = []

    grouped.forEach{c, ts in
      var sum = 0.0
      ts.forEach{t in
        sum += t.expense ? Double(t.value * -1) : Double(t.value)
      }
      let entry =  PieChartDataEntry(value: sum, label: c.name)
      entries.append(entry)
    }

    entries.sort(by: {$0.0.value > $0.1.value})
    entries = Array(entries.prefix(10))

    let data = PieChartData()
    let ds1 = PieChartDataSet(values: entries, label: "")

    ds1.colors = ChartColorTemplates.material()

    data.addDataSet(ds1)

    let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
    paragraphStyle.lineBreakMode = .byTruncatingTail
    paragraphStyle.alignment = .center
    let centerText: NSMutableAttributedString = NSMutableAttributedString(string: "Top 10 spending categories".localizedCapitalized)

    self.pieChartView.centerAttributedText = centerText

    self.pieChartView.data = data
    self.pieChartView.holeColor = UIColor.clear

    self.pieChartView.animate(xAxisDuration: 0.0, yAxisDuration: 1.0)
  }

Attached is a screenshot. bildschirmfoto 2017-01-19 um 21 01 02

guidove commented 7 years ago

Same problem here. Did you find a solution by any chance? Tried several things, but no effect. The colors are registered with the dataset though (see legend). Makes me think that it might have something to do with the pie slices drawing area or something. I'll keep on digging, but if you found a solution or workaround could you let me know? Thanks & regards, Guido

bernhardwaidacher commented 7 years ago

No I haven't found a solution. I also think it's some size issue. I'll have a look at this later and let you know

guidove commented 7 years ago

Found it. (For me at least.)

I was passing negative values to the dataset, which makes them not draw (see PieChartRenderer.swift line 151).

Also I found (for me, legend drawn at the right of the pie) that if you set yourPieChart.legend.drawInside = false then the pie chart is really really small. Had to play with legend.maxSizePercent and legend.xOffset and graph.extraLeft/Right/Top/BottomOffset to get it looking OK.

bernhardwaidacher commented 7 years ago

Oh! Guess that will be the same for me, as I pass negative values too! Thank you

ghost commented 7 years ago

As someone who has also run into this issue, I'd love it if either: