Jason Leach
Jason Leach

Reputation: 4229

iOS-charts won't put the x-axis at zero on the y-axis

I can not get an iOS-charts bar chart to put the bars zero (the bottom of the bars) on the x-axis. They seem to float just above the x-axis:

enter image description here

The bars in orange are set to 100 which is the max height of the y-axis for illustration. The only thing that might be doing it is at about 3PM there is a blank space.

I have rightAxis.axisMinValue = 0 set.

Upvotes: 3

Views: 3133

Answers (4)

B-Rad
B-Rad

Reputation: 349

Have you tried setting the right y-axis minimum to zero (rightAxisY.axisMinimum = 0)? That should set the y-axis minimum to the zero line and shift the entire chart downward.

You might also try enabling the zero line (rightAxisY.drawZeroLineEnabled = true) and eliminate the zero value from your data since you are setting zero as the minimum by enabling the zero line.

Upvotes: 0

Wingzero
Wingzero

Reputation: 9754

In your sample project, you forgot to set dataSet.axisDependency = .Right

Upvotes: 4

AnthonyR
AnthonyR

Reputation: 3545

barChartView.leftAxis.spaceBottom = 0.0

works for me, for you it is .rightAxis

Upvotes: 3

Hasya
Hasya

Reputation: 9898

I have just work around, This works for me.

Just refer this code.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var barChartView: BarChartView!

    override func viewDidLoad() {
    super.viewDidLoad()

        let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
        let unitsSold = [20.0, 4.0, 6.0, 3.0, 0.0, 16.0]

        setChart(months, values: unitsSold)

    }


    func setChart(dataPoints: [String], values: [Double]) {

        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: "Units Sold")
        let chartData = BarChartData(xVals: dataPoints, dataSet: chartDataSet)
        barChartView.data = chartData

        chartDataSet.colors = ChartColorTemplates.colorful()
    }  
}

enter image description here

Upvotes: 0

Related Questions