Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legend has only one label #361

Closed
wow-such-amazing opened this issue Sep 14, 2015 · 4 comments
Closed

Legend has only one label #361

wow-such-amazing opened this issue Sep 14, 2015 · 4 comments

Comments

@wow-such-amazing
Copy link

piechart
Hello.
I have pie chart. When it loads first time, then i can see my 6 labels in legend. But pieChart.legend.labels.count is equal 2 and labels are: "" and "myLabel".
Then i update my chart. The pie is perfect, but in the legend there is only one label exist.
pieChart.legend.labels.count is equal 2 and labels are: "" and "myLabel".
So, why in the first time i see 6 labels, but the real amount of labels is 2?

override func viewDidLoad() {
        super.viewDidLoad()

        pieChart.delegate = self

        setChart(segmentsName, pieValues: segmentsSumm, values: lineChartY, datePoints: lineChartDates)

        pieChart.alpha = 1

        var l = pieChart.legend
        l.position = ChartLegend.ChartLegendPosition.RightOfChart
        l.xEntrySpace = 7.0
        l.yEntrySpace = 0.0
        l.yOffset = 0.0
        pieChart.usePercentValuesEnabled = true
        pieChart.centerText = String(stringInterpolationSegment: totalIncome)
        pieChart.drawSliceTextEnabled = false
        pieChart.noDataText = "Нет данных"
        pieChart.animate(xAxisDuration: 2.0)

        lineChart.noDataText = "No Data"
        lineChart.xAxis.labelPosition = .Bottom
        lineChart.animate(yAxisDuration: 2.0)
        lineChart.rightAxis.enabled = false

        println("count = \(pieChart.legend.labels)") // it always equals 2

    }
func setChart(dataPoints: [String], pieValues: [Double], values: [Double], datePoints: [String]) {
        var pieDataEntries: [BarChartDataEntry] = []
        var lineDataEntries: [BarChartDataEntry] = []

        for i in 0..<dataPoints.count {
            let dataEntry = BarChartDataEntry(value: pieValues[i], xIndex: i)
            pieDataEntries.append(dataEntry)
        }

        let pieChartDataSet = PieChartDataSet(yVals: pieDataEntries, label: "")

        let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)

        pieChart.data = pieChartData

        var colors: [UIColor] = []

        for i in 0..<dataPoints.count {
            let red = Double(arc4random_uniform(256))
            let green = Double(arc4random_uniform(256))
            let blue = Double(arc4random_uniform(256))

            let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
            colors.append(color)
        }
        pieChartDataSet.colors = colors


        for i in 0..<datePoints.count {
            let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
            lineDataEntries.append(dataEntry)
        }

        let chartDataSet = LineChartDataSet(yVals: lineDataEntries, label: "Units Sold")
        chartDataSet.drawCubicEnabled = true
        chartDataSet.cubicIntensity = 0.2
        chartDataSet.drawCirclesEnabled = false
        chartDataSet.lineWidth = 2.0
        chartDataSet.circleRadius = 5.0
        let chartData = LineChartData(xVals: lineChartDates, dataSet: chartDataSet)
        lineChart.data = chartData
    }

When i press the button on the pop controller the following code runs:

var tmpController :UIViewController! = self.presentingViewController
        self.dismissViewControllerAnimated(false, completion: {()->Void in
            tmpController.viewDidLoad()
        })
@wow-such-amazing
Copy link
Author

any ideas?

@liuxuan30
Copy link
Member

@crabman448
Legends will be created computed by computeLegend. Every time you change dataSet, it will trigger notifyDataSetChanged, and compute legend again. You could debug from here.

    public override func notifyDataSetChanged()
    {
        if (_dataNotSet)
        {
            return
        }

        calcMinMax()

        if (_legend !== nil)
        {
            _legendRenderer.computeLegend(_data)
        }

        calculateOffsets()

        setNeedsDisplay()
    }

@wow-such-amazing
Copy link
Author

@liuxuan30
Thanks a lot! I just added swift pieChart.notifyDataSetChanged() to viedDidLoad method and everything started to work fine
Thank you

@liuxuan30
Copy link
Member

@crabman448 hey usually you don't need to call this, when you change the dataSet, it will call it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants