2

I have troubles with the library and i can't find a solution, hope you can help.

When I put data on the chart everything about drawing axis and shapes goes well but the labels on my xAxis multiplies itself and i don't know why, look i let you my code and a problem's picture, maybe it sounds weird but in order to show the problem i created a barChart with just one data.

func setChart(values: [Double]) {

    let formato:BarChartFormatter = BarChartFormatter()
    formato.setArray(self.listadoLabels)
    let xaxis:XAxis = XAxis()

    max = values.maxElement()

    barChartView.noDataText = "You need to provide data for the chart."

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<values.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y:values[i])
        dataEntries.append(dataEntry)

        formato.stringForValue(Double(i), axis: xaxis)
    }

    xaxis.valueFormatter = formato

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
    chartDataSet.colors = ChartColorTemplates.joyful()

    barChartView.fitBars = true //Para que las barras inicial y final se muestren completas
    barChartView.xAxis.valueFormatter = xaxis.valueFormatter
    barChartView.xAxis.labelPosition = .Bottom

    barChartView.leftAxis.axisMinimum = 0
    barChartView.leftAxis.axisMaximum = max + 2
    barChartView.rightAxis.enabled = false
    barChartView.drawValueAboveBarEnabled = false
    barChartView.descriptionText = ""
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)

    let chartData = BarChartData()
    chartData.addDataSet(chartDataSet)
    barChartView.data = chartData
}

Most of code is just styling, and i have one class for the xAxis format because you need one to put labels in the xAxis, this is the code:

public func stringForValue(value: Double, axis: AxisBase?) -> String {

    return  "Label1"
}

I'm just sending one value of 5512, and the result is, don't pay attention to segmentTab and button:

enter image description here

  • By the way, reading your code i see you use the "fitBars" to have the fist and last bar the same size, did you do anything else to achieve this? because fitters in my case is not working – Ponchotg Oct 09 '16 at 13:02

1 Answers1

4

I was in the same situation you were, i couldn't find an exact solution, but this one worked for me:

barChart?.xAxis.wordWrapEnabled = true
barChart?.xAxis.setLabelCount(5, force: true)

I also centered my bars to match the labels

let dataEntry = BarChartDataEntry(x: Double(i) + 0.5, y: Double(count))

In my case my chart can't have more than 5 bars so i used 5, but you could pass a variable with the number of bars and set the "LabelCount" to that number, when you zoom in it will still repeat the labels, but they won't be overlapped and won't look as bad.

I know it is not exactly the answer you were looking for but hope it helps

Ponchotg
  • 1,195
  • 1
  • 14
  • 25