0

screenshot

I have a problem with piecharts not showing in my custom tableview cell. The tableview cells are added through storyboard, each cell contains a small icon image, two labels and a UIView which I set in the view inspector as PieChartView.

The small icon image and the text in the two labels is showing fine, no problem there. For the piecharts however, I get no error message but simply none of the charts is displayed. The table shows up, is filled with the proper texts in the labels but the piechartview is empty except the middle hole of the piechart. When I click one of these, the piechartview is displayed - not totally correct (only one of the two pie slices is displayed, the other part is missing).

The touch event therefore makes the slice visible, but I want the piechart be visible for all cells upon running the cell for row function.

I have added the code for the tableviewcontroller and the tableviewcell. Would be great, if someone could point out my error. I have researched and tried a lot, among others the following stack overflow resources: Charts not plotting in tableViewCell iosChart not displaying LineChartView points in UITableViewCell Adding a SubView to UITableViewCell doesn't get displayed How to implement iOS Chart in a tableview cell?

The screenshot shows the situation after I click a few of the invisible piecharts. They become visible, also when I then scroll down the table some more (not all) of piecharts in random cells are visible, some are not.

Code for tableviewcontroller:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "easyAndDiffAllWordsCell", for: indexPath) as! BVBResultsDiffAndEasyAllWordsGraphTableViewCell

        let easyVoc = parsedInEasyVocStructures[indexPath.row]

        //get the current voc for the writings
        let currentVoc = BVBVocabularyManager.getVoc()

        cell.label1.text = currentVoc.kanji

        cell.label2.text = currentVoc.kanji2

        let image = UIImage(named: "plus")
        cell.plusMinusImage.image = image

        //set the percentages
        cell.percentageSolved = arrayOfSuccessPercentagesForPieChart
        cell.percentageNotSolved = arrayOfNegativeSuccessPercentagesForPieChart

        cell.setChart(forIndexNo:indexPath.row, dataPoints: months, valuesSolved: arrayOfSuccessPercentagesForPieChart, valuedNonSolved: arrayOfNegativeSuccessPercentagesForPieChart)

        cell.setNeedsDisplay()
        cell.pieChartView.clipsToBounds = true
        cell.pieChartView.layer.masksToBounds = true
        cell.pieChartView.contentMode = .scaleAspectFit

        return cell
    }

And for the tableViewCell:

class BVBResultsDiffAndEasyAllWordsGraphTableViewCell: UITableViewCell {

    @IBOutlet weak var kanjiL: UILabel!
    @IBOutlet weak var translationL: UILabel!
    @IBOutlet weak var pieChartView: PieChartView!

    @IBOutlet weak var plusMinusImage: UIImageView!

    var testconditions: Array<String>?
    var percentageSolved: Array<Int>? 
    var percentageNotSolved: Array<Int>? 

    var solvedPercentageDataEntry = PieChartDataEntry(value: 0)
    var nonSolvedPercentageDataEntry = PieChartDataEntry(value: 0)

    var percentageSolvedNonSolvedDataEntries = [PieChartDataEntry]()


    override func awakeFromNib() {
        super.awakeFromNib()

        solvedPercentageDataEntry.label = NSLocalizedString("solved", comment: "piechart label for the solved area")
        pieChartView.chartDescription?.text = ""
        pieChartView.legend.enabled = false
        pieChartView.setExtraOffsets(left: 2, top: 0, right: 2, bottom: 0)

        pieChartView.holeRadiusPercent = 2.8     
 }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state

        //not used
    }


    func setChart(forIndexNo: Int, dataPoints: [String], valuesSolved: [Int], valuedNonSolved: [Int]){

        var dataEntries: [PieChartDataEntry] = []

        solvedPercentageDataEntry = PieChartDataEntry(value: Double(valuesSolved[forIndexNo]), label: "")
        nonSolvedPercentageDataEntry = PieChartDataEntry(value:Double(valuedNonSolved[forIndexNo]), label: "")

        dataEntries = [solvedPercentageDataEntry, nonSolvedPercentageDataEntry]

        percentageSolvedNonSolvedDataEntries = [solvedPercentageDataEntry, nonSolvedPercentageDataEntry]

        let pieChartDataSet = PieChartDataSet(entries: percentageSolvedNonSolvedDataEntries, label: nil)

        pieChartDataSet.drawValuesEnabled = false 

        let pieChartData = PieChartData(dataSet: pieChartDataSet)

        let colors = [UIColor.themeColor(), UIColor.red]

        pieChartDataSet.colors = colors as! [NSUIColor]

        pieChartView.data = pieChartData

        pieChartView.notifyDataSetChanged()
    }

}

1 Answers1

0

I test your code. It works fine. And the middle hole, maybe it's the configuration problem. Try to add this

    pieChartDataSet.drawIconsEnabled = false
    pieChartDataSet.sliceSpace = 1
    pieChartDataSet.highlightColor = UIColor.white
    pieChartDataSet.entryLabelColor = UIColor.white
    pieChartDataSet.selectionShift = 0

    pieChartView.holeRadiusPercent = 0.5
    pieChartView.transparentCircleRadiusPercent = 0.0

Hope this helps

Tony Gilbert
  • 111
  • 5
  • great thanks that worked. I didn't add - delete line by line, so not sure which one did the magic but now all pie charts are showing as supposed! thanks a lot! – user2399094 Nov 12 '19 at 14:41