简体   繁体   English

iOS图表不会将x轴在y轴上设为零

[英]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. 我无法获得iOS图表条形图,将条形图(条形图的底部)放在x轴上。 They seem to float just above the x-axis: 它们似乎漂浮在x轴上方:

在此输入图像描述

The bars in orange are set to 100 which is the max height of the y-axis for illustration. 橙色条设置为100,这是y轴的最大高度,用于说明。 The only thing that might be doing it is at about 3PM there is a blank space. 唯一可能的做法是在下午3点左右有一个空白区域。

I have rightAxis.axisMinValue = 0 set. 我有rightAxis.axisMinValue = 0设置。

barChartView.leftAxis.spaceBottom = 0.0

适合我,对你而言.rightAxis

在您的示例项目中,您忘记设置dataSet.axisDependency = .Right

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()
    }  
}

在此输入图像描述

Have you tried setting the right y-axis minimum to zero (rightAxisY.axisMinimum = 0)? 您是否尝试将右侧y轴最小值设置为零(rightAxisY.axisMinimum = 0)? That should set the y-axis minimum to the zero line and shift the entire chart downward. 这应该将y轴最小值设置为零线并将整个图表向下移动。

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. 您也可以尝试启用零线(rightAxisY.drawZeroLineEnabled = true)并消除数据中的零值,因为您通过启用零线将零设置为最小值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM