1

I have negative values in my line chart using ios-charts. Positive values are displayed but negative values are clipped at 0. I have set the left and right axes not to start at 0 but no help.

    LineChart1.leftAxis.startAtZeroEnabled = false
    LineChart1.rightAxis.startAtZeroEnabled = false

Any ideas? TIA.

Code snippet below.

    override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.navigationController?.navigationBar.translucent = false

    self.title = "Line Chart 1"
    LineChart1.bounds = CGRectMake(-20, -20, 275, 250)
    LineChart1.data = getMyData()
    LineChart1.drawBordersEnabled = false
    LineChart1.legend.enabled = false
    LineChart1.leftAxis.startAtZeroEnabled = false
    LineChart1.rightAxis.startAtZeroEnabled = false
    LineChart1.xAxis.setLabelsToSkip(0)
    LineChart1.xAxis.drawGridLinesEnabled = false
    LineChart1.leftAxis.drawGridLinesEnabled = true
    LineChart1.rightAxis.drawLabelsEnabled = false
    LineChart1.animate(xAxisDuration: 0.3)
    LineChart1.animate(yAxisDuration: 0.3)

    LineChart1.setNeedsDisplay()
}

func getMyData() -> LineChartData {

    var xVals = ["2008","2009","2010","2011","2012"]

    var courses: [ChartDataEntry] = []
    courses.append(ChartDataEntry(value: 3, xIndex: 0))
    courses.append(ChartDataEntry(value: -40, xIndex: 1))
    courses.append(ChartDataEntry(value: 4, xIndex: 2))
    courses.append(ChartDataEntry(value: 4, xIndex: 3))
    courses.append(ChartDataEntry(value: 3, xIndex: 4))

    let bcds = LineChartDataSet(yVals: courses, label: "")
    bcds.valueTextColor = UIColor.blackColor()

    return LineChartData(xVals: xVals, dataSet: bcds)
}
3
  • Try setting the data after you call LineChart1.leftAxis.startAtZeroEnabled = false. Commented May 11, 2015 at 10:28
  • How can I show Negative X Axis ? Commented Mar 4, 2016 at 9:00
  • Maybe it is too late but I want to share my experience and maybe save someone's life. If you have several points for xAxis both negative and positive, you have to sort them first and then put them on the graph. Otherwise when you tap on a specific value at a certain point you will not get the correct coordinates for that specific point. Commented Oct 30, 2017 at 18:55

1 Answer 1

2

You should call notifyDataSetChanged() after setting startAtZeroEnabled - like in the sample app.

The chart does not know of a change in a property like startAtZeroEnabled on an inner component, so you have to tell it. And because min/max are calculated in notifyDataSetChanged according to startAtZeroEnabled, that's a case where you have to notify, and just setNeedsDisplay...

Sign up to request clarification or add additional context in comments.

2 Comments

What's a negative X axis? An X axis is an X axis... The values displayed on it are in your control...
@DeviPhone26 You are not becoming any clearer. You can post a new question with screenshots and clear explanations, or do the same on GitHub issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.