Peter Pik
Peter Pik

Reputation: 11193

remove zero line from ios charts

I've created a simple small chart using (https://github.com/danielgindi/Charts), where i'm trying to remove everything beside the line itself, but i can't seem to remove the bottom zero axis. i've looked through the documentation, but cannot seem to remove it?

enter image description here

chart

class CellChartView: UIView {

    var lineChart: LineChartView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.lineChart = LineChartView()
        lineChart.backgroundColor = Color.lightTheme.value
        lineChart.translatesAutoresizingMaskIntoConstraints = false
        self.lineChart.chartDescription?.text = ""
        self.lineChart.isUserInteractionEnabled = false

        self.lineChart.legend.enabled = false
        self.lineChart.minOffset = 0

        self.lineChart.drawBordersEnabled = false
        self.lineChart.drawGridBackgroundEnabled = false
        self.lineChart.autoScaleMinMaxEnabled = true

        self.lineChart.rightAxis.enabled = false

        self.lineChart.leftAxis.enabled = false
        self.lineChart.leftAxis.drawAxisLineEnabled = false
        self.lineChart.leftAxis.axisLineColor = UIColor.green

        self.lineChart.xAxis.drawLabelsEnabled = false
        self.lineChart.xAxis.drawGridLinesEnabled = false
        self.lineChart.xAxis.labelPosition = .bottom
        self.lineChart.xAxis.drawLimitLinesBehindDataEnabled = false
        self.lineChart.xAxis.enabled = false

        self.lineChart.xAxis.axisLineColor = UIColor.clear



        self.addSubview(self.lineChart)

        lineChart.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        lineChart.topAnchor.constraint(equalTo: self.topAnchor, constant: -1).isActive = true
        lineChart.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
        lineChart.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setChartData(values: [Double], dates: [String]) {

        var yValues : [ChartDataEntry] = [ChartDataEntry]()
        for i in 0 ..< dates.count {
            yValues.append(ChartDataEntry(x: Double(i + 1), y: values[i]))
        }

        let data = LineChartData()
        let ds = LineChartDataSet(values: yValues, label: "Date")
        ds.drawCirclesEnabled = false
        ds.lineWidth = 1
        ds.drawValuesEnabled = false

        if (values.first ?? 0.0 > values.last ?? 0.0) {
            ds.setColor(Color.redColor.value)
        } else {
            ds.setColor(Color.greenColor.value)
        }


        data.addDataSet(ds)

        data.setDrawValues(false)
        self.lineChart.data = data
    }

}

Upvotes: 1

Views: 3678

Answers (3)

Muhammad Essa
Muhammad Essa

Reputation: 329

If you just want to hide background gridlines

Just do

    self.chartView?.leftAxis.drawGridLinesEnabled = false
    self.charView?.xAxis.drawGridLinesEnabled = false

It will remove background grids for both axis.enter image description here like that

Upvotes: 0

Nikhil Pandey
Nikhil Pandey

Reputation: 272

If you want to hide the horizontal and vertical lines being drawn through the center when you have disabled all grid lines and just want an X-Axis line as shown above in your drawing

lineChartDataSet.highlightColor = NSUIColor.clear
myLineChartView.xAxis.drawAxisLineEnabled = true
myLineChartView.xAxis.drawGridLinesEnabled = false
myLineChartView.xAxis.drawLabelsEnabled = false
myLineChartView.leftAxisAxis.drawAxisLineEnabled = false

This will just hide the horizontal and vertical lines while giving you the freedom to draw a line through X-Axis or Y-Axis.

Upvotes: 0

AlexSmet
AlexSmet

Reputation: 2161

If you just want to hide the xAxis, then I see two ways:

You could disable the xAxis xAxis.enabled = false

Or set the xAxis line color to transparent xAxis.axisLineColor = UIColor.clear

Upvotes: 4

Related Questions