从 ios 个图表中删除零线

remove zero line from ios charts

我使用 (https://github.com/danielgindi/Charts) 创建了一个简单的小图表,我试图在其中删除线本身旁边的所有内容,但我似乎无法删除底部零轴。我已经查看了文档,但似乎无法删除它?

图表

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
    }

}

如果你只是想隐藏xAxis,那么我看到两种方法:

您可以禁用 xAxis xAxis.enabled = false

或者设置x轴线颜色为透明xAxis.axisLineColor = UIColor.clear

如果您想在禁用所有网格线时隐藏通过中心绘制的水平线和垂直线,并且只想要一条如上图所示的 X 轴线

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

这只会隐藏水平线和垂直线,同时让您可以自由地通过 X 轴或 Y 轴画线。