Search code examples
swiftios-charts

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
    }

}

Solution

  • 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