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

HorizontalBarChartView text for xValues -- charts 3.0 #1646

Closed
0xCaponte opened this issue Oct 12, 2016 · 7 comments
Closed

HorizontalBarChartView text for xValues -- charts 3.0 #1646

0xCaponte opened this issue Oct 12, 2016 · 7 comments

Comments

@0xCaponte
Copy link

I am working with an horizontal bar chart, with version 2.2.5 my original code was:

    var dataEntries: [BarChartDataEntry] = []

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

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Affected")

   var formatedResults = [String]()

    for i in 0..<dataPoints.count {

        let label = dataPoints[i].company! as String
        formatedResults.append(label)
    }

    //Coloring the bars and creating the labels
    var colors = [UIColor]()

    for i in 0..<dataPoints.count {

        if (i % 2 == 0){
            colors.append(Colors.blue)
        }else{
            colors.append(Colors.gray)
        }
    }

    chartDataSet.colors = colors

    // Adds the data to the chart
    let chartData = BarChartData(xVals: formatedResults, dataSet: chartDataSet)
    barChartView.data = chartData

But with the version 3.0 I can not create my BarChartData that way, so I do not know how to set the names I want as the xValues.

I am getting just 0/2/4... as the text, always even numbers every other bar and I have no idea how to get it fixed.

My current code:
var dataEntries: [BarChartDataEntry] = []

    for i in 0..<dataPoints.count {

        let dataEntry = BarChartDataEntry(x : Double(i), y : values[i])
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Affected")

    var formatedResults = [String]()

    for i in 0..<dataPoints.count {

        let label =  dataPoints[i].company! as String
        formatedResults.append(label)
    }

    //Coloring the bars and creating the labels
    var colors = [UIColor]()

    for i in 0..<dataPoints.count {

        if (i % 2 == 0){
            colors.append(Colors.blue)
        }else{
            colors.append(Colors.gray)
        }
    }

    chartDataSet.colors = colors
    chartDataSet.drawValuesEnabled = false // Hides the values on the bars

    // Adds the data to the chart
    let chartData = BarChartData(dataSet: chartDataSet)
    chartData.barWidth = 0.5

    barChartView.data = chartData

Current Result:

horizontalbarchartview

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 12, 2016

In chart 3.0, x axis does not work any more as it used to be. The x axis is redesigned and works more like y axis.

I tried with ChartsDemo horizontal bar chart with an trick _chartView.xAxis.labelCount = count; in - (void)setDataCount:(int)count range:(double)range and it shows every label then.

What I know for the moment is, you can try change labelCount, because by default how many x axis labels are displayed is calculated by computeAxisValues(). You can also take a look at it and override it with your desired behaviors, like display every label.

But do be aware that xAxis.labelCount = count is not equivalent to open func setLabelCount(_ count: Int, force: Bool), because forceLabelsEnabled is false in labelCount setter:

    /// the number of label entries the y-axis should have
    /// max = 25,
    /// min = 2,
    /// default = 6,
    /// be aware that this number is not fixed and can only be approximated
    open var labelCount: Int
    {
        get
        {
            return _labelCount
        }
        set
        {
            _labelCount = newValue

            if _labelCount > 25
            {
                _labelCount = 25
            }
            if _labelCount < 2
            {
                _labelCount = 2
            }

            forceLabelsEnabled = false
        }
    }
    open func setLabelCount(_ count: Int, force: Bool)
    {
        self.labelCount = count
        forceLabelsEnabled = force
    }

Short answer is when enable forceLabelsEnabled, it will not apply a small algorithm to calculate nice numbers but just do a brute calcualation like interval = Double(range) / Double(labelCount - 1) For the details please check computeAxisValues().

@danielgindi Is there any other convenient way to configure this? I don't see there is property that controls inertval but only granularity or labelCount

@0xCaponte
Copy link
Author

0xCaponte commented Oct 12, 2016

I am lookind at what you mentiones @liuxuan30 , but I wanted to point the issue in another directions. My original chart looked like this:

screen shot 2016-10-11 at 8 39 04 pm

Is it possible to get that look, those company text labels on the new version?

@kientux
Copy link

kientux commented Oct 12, 2016

@caat91 To show those company text labels, you must use function stringForXValue of protocol ChartXAxisValueFormatter. But the problem here is that you will get Company 2, Company 4, Company 6... only because of auto-calculating axis labels feature. I will try what @liuxuan30 suggested and feedback later.

@0xCaponte
Copy link
Author

0xCaponte commented Oct 12, 2016

@kientux your suggestion did the trick for me regarding the text labels, I just extended de IAxisValueFormatter protocol and added the stringForXValue methods.

I was able to follow @liuxuan30 idea and got the following result but is there a way to align the labels with the bars?

Also, Is this an issue than you are planning to address or is this just the new behaviour of the barchart and there are no plans of modifying it?

screen shot 2016-10-12 at 1 53 52 pm

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 13, 2016

well you have to debug a little, I don't see this in ChartsDemo. Need more info why not centered..
Just use shorter text and maybe check ChartsDemo.
Bottom line: Chart 3.0 x axis code differs from 2.x, you should not reuse your data portion of code but write new code to setup your chart data.

@Paradolle
Copy link

I meet the same issue using Charts 3.0 (in VerticalBarChartView as well), could you teach me how to write the extension in IAxisValueFormatter and added the 'stringForXValue' please? Thanks a lot

@danielgindi
Copy link
Collaborator

Please see the demos. Another way is to just init a DefaultAxisValueFormatter with a block.

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

5 participants