Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAxis entry count is greater than it should be in iOS Charts 3.0.1 in Swift 3

Tags:

I have two BarChartDataSets. One of them is always size 3 and the other is either 2 or 3. I tested out the code in version 3.0.0 and everything was working fine. When 3.0.1 came out, it broke my chart. I have the correct number of bars always, but I have six labels instead of 5 when the second dataset is only size 2. It has nothing to do with the stringForValue Delegate function. I set the X values using int's that are associated linearly with the Bar I want represented at that index, so each bar is equally spaced when working properly, but none of them are equally spaced when I have 6 labels and 5 bars.

enter image description here The one on the left shows the issue and the one on the right shows what it looks like when my BarChartDataSet is size 3. It is duplicating whatever the last value on the chart is and adding it as a 6th label on the left. In 3.0.0 the one on the left would have only had 5 labels.

I dug into their code and where they create the labels in XAxisRendererHorizontalBarChart.swift
right before they call

drawLabel()

I call

print("xAxis entries: \(xAxis.entries.count)")

which prints

xAxis entries: 6

to the console even though right before I call

let chartData = BarChartData(dataSets: [chartDataSet1, chartDataSet2])

I call

print("dataEntries1 count: \(dataEntries1.count), dataEntries2 count: (dataEntries2.count)")

which prints

dataEntries1 count: 3, dataEntries2 count: 2

like image 881
Sethmr Avatar asked Nov 28 '16 19:11

Sethmr


1 Answers

First, xAxis.entries and dataSet.entries are totally different.

xAxis.entries are the values that being displayed on the axis as labels, while dataSet.entries is the value displayed for the data, e.g. the dot value in line chart, or the bar value for bar chart.

for x axis, it calculate the label count by your data entries min/max value. In Chart 3.0, x axis behaves like y axis, so it calculates x axis entries like y axis, please take a look at computeAxisValues() for details.

So xAxis.entries and dataSet.entries don't have to be equal size.

If you want to set the x axis label count same as your bars count, you can call: setLabelCount(5(6), true):

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

Note, don't call the labelCount setter as it's different:

/// the number of label entries the 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
    }
}
like image 60
Wingzero Avatar answered Oct 14 '22 05:10

Wingzero