Charts: How to get back x-values for BarChart[Charts 3 migration problem]

Hello,

I’m trying to get started with Charts 3 but I’ve got some migration problems. That old Charts code created a Bar Chart with a history for right and wrong answered questions and writing the Day on the top over the belonging Bar.

img_0771

I’ve built it with the following code:

            let examHistoryChartFrame = CGRectMake(scrWidth*0.08, 24 + scrWidth*1.68 , scrWidth*0.84, scrWidth*0.84)
            let examHistoryView = BarChartView(frame: examHistoryChartFrame)

            examHistoryView.descriptionText = ""
            examHistoryView.userInteractionEnabled = false
            //  historyBarChartView.xAxis.valueFormatter = NSNumberFormatter()
            examHistoryView.rightAxis.enabled = false
            let formatter = NSNumberFormatter()
            formatter.minimumFractionDigits = 0
            examHistoryView.leftAxis.valueFormatter = formatter
            examHistoryView.drawHighlightArrowEnabled = true
            examHistoryView.drawValueAboveBarEnabled = false
            examHistoryView.xAxis.drawGridLinesEnabled = false

           //...getting the data in some way....

           var counter = 0
           let fi = getDataEntries(dataPoints, values: firstValues, counter: counter)
           counter += 1
           let sec = getDataEntries(dataPoints, values: secondValues, counter: counter)
           dataPoints = transformDataPoints(dataPoints)
           examHistoryView.data =  BarChartData(xVals: dataPoints, dataSets: [fi,sec])

The getDataEntries(…) function does the following:


    func getDataEntries(dataPoints: [String], values: [Double], counter: Int) -> BarChartDataSet{
        var dataEntries: [ChartDataEntry] = []
        for i in 0..<dataPoints.count {
            let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
            dataEntries.append(dataEntry)
        }
        let ret = BarChartDataSet(yVals: dataEntries)
        ret.drawValuesEnabled = false
        switch counter{
            case 0:
                ret.colors = [GREEN]
                ret.label = "Richtig beantwortet"
                break
            case 1:
                ret.colors = [RED]
                ret.label = "Falsch beantwortet"
                break
            default:
                break
        }
        return ret
    }

After the update I’ve tried everything to bring the Dates back to the Chart but I can’t make it.

Does anybody know which attribute I should manipulate for achieving my goal?

Thanks for your help! Patrick

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 22 (2 by maintainers)

Most upvoted comments

Note that in Charts 3.0.1 there’s an IndexAxisValueFormatter that you can pass an array of labels and have them behave like in Charts 2.0

Vielen dank 😃 … So it turns out that duplicate is because the values are not equal the number of datasets.

Edit: Setting the granularity causes it to increment by one. Weird things happening here. Why would you need to set this if the datasets count is known and formatter values count is also known.

Seems Chart 3.0 is designed for maximum customization and advanced usage but this makes basic usage not easy.

chartView.xAxis.granularity = 1

Oh no, that hint would have been a real time-saver for me @danielgindi haha

Am 21.11.2016 um 19:08 schrieb Daniel Marques <notifications@github.commailto:notifications@github.com>:

@acegreenhttps://github.com/acegreen Ah, ok, now I get it. Thanks!

@danielgindihttps://github.com/danielgindi Oh, now I’ll have to remove all those IAxisValueFormatter classes I just implemented, haha. Thanks! 😃

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/danielgindi/Charts/issues/1688#issuecomment-262018947, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AK1gNW6ZzcQMNhve8G1wO_oMBGFbnMgbks5rAd4fgaJpZM4KcIMy.

On a little Education App that also features learning statistics… I hope you can figure it out!