ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.51k stars 5.99k forks source link

BarChart xAxis label offsets #1874

Open shayneptorres opened 7 years ago

shayneptorres commented 7 years ago

Hello again, first off I want to say thank you for responding to my last issue so quickly. I do have another question though. When creating a bar charts, with labels on the bottom, the very first label is offset and not directly under the respective bar. screen shot 2016-11-22 at 3 25 27 pm I took a look at the demo and I matched all my settings accordingly: chartView.isUserInteractionEnabled = false chartView.chartDescription?.text = "" chartView.drawBarShadowEnabled = false chartView.rightAxis.drawGridLinesEnabled = false chartView.rightAxis.drawAxisLineEnabled = false chartView.rightAxis.drawLabelsEnabled = false chartView.xAxis.avoidFirstLastClippingEnabled = true chartView.xAxis.centerAxisLabelsEnabled = false chartView.xAxis.drawLimitLinesBehindDataEnabled = false chartView.xAxis.labelPosition = .bottom chartView.xAxis.labelCount = 3 chartView.xAxis.axisMinimum = -0.5 chartView.xAxis.axisMaximum = 3 chartView.xAxis.drawGridLinesEnabled = false chartView.fitBars = true chartView.xAxis.granularity = 1 Im not sure what other properties to try. I would appreciate the guidance. Thanks again

shayneptorres commented 7 years ago

Here is the entire function I call to setup the graph `func setUpChart(delegate: IAxisValueFormatter){

    var dataEntries = [BarChartDataEntry]()
    var barChartDataSet = BarChartDataSet()

    for i in 0..<items.count {
        let de = BarChartDataEntry(x: Double(i), yValues: [itemsValues[i]])
        dataEntries.append(de)
    }

    barChartDataSet = BarChartDataSet(values: dataEntries , label: "Monthly Income")

    chartView.isUserInteractionEnabled = false
    chartView.chartDescription?.text = ""
    chartView.drawBarShadowEnabled = false
    chartView.rightAxis.drawGridLinesEnabled = false
    chartView.rightAxis.drawAxisLineEnabled = false
    chartView.rightAxis.drawLabelsEnabled = false
    chartView.xAxis.avoidFirstLastClippingEnabled = true
    chartView.xAxis.centerAxisLabelsEnabled = false
    chartView.xAxis.drawLimitLinesBehindDataEnabled = false
    chartView.xAxis.labelPosition = .bottom
    chartView.xAxis.labelCount = 3
    chartView.xAxis.axisMinimum = -0.5
    chartView.xAxis.axisMaximum = 3
    chartView.xAxis.drawGridLinesEnabled = false
    chartView.fitBars = true
    chartView.xAxis.granularity = 1
    chartView.xAxis.valueFormatter = delegate

    barChartDataSet.colors = ChartColorTemplates.colorful()

    self.chartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .easeInOutCirc)

    let bcData = BarChartData(dataSet: barChartDataSet)

    bcData.barWidth = 0.5
    self.chartView.data = bcData

}`
liuxuan30 commented 7 years ago

Disable avoidFirstLastClippingEnabled. Enabling it will have the effect. It looks like a bug.

Rnorback commented 7 years ago

Had a similar problem with line charts, the first label on the xAxis is off.

screen shot 2016-11-29 at 3 28 53 pm

The only way I found to fix it is to use xAxis.centerAxisLabelsEnabled = true but I'm guessing my highlighting will be off.

Here is my xAxis code. Any help is welcome.

 func setupXAxisFormatting() {
        let xAxis = lineChartView.xAxis
        xAxis.enabled = true
        xAxis.drawGridLinesEnabled = false
        xAxis.drawLabelsEnabled = true
        xAxis.drawAxisLineEnabled = false
        xAxis.centerAxisLabelsEnabled = true //would rather fix another way

        xAxis.labelPosition = .bottom
        xAxis.labelTextColor = UIColor.gray
        xAxis.labelFont = UIFont(name: Constants.Fonts.ProximaNovaBold, size: 15.0)!

        xAxis.avoidFirstLastClippingEnabled = true
        xAxis.spaceMin = 5
        xAxis.valueFormatter = axisFormatDelegate
    }
liuxuan30 commented 7 years ago

@Rnorback I don't think it's the same issue for "the first label on the xAxis is off"? turn off avoidFirstLastClippingEnabled should move '11/23' to left by some space.