如何让y轴不从零开始?

时间:2015-12-11 01:23:00

标签: swift ios-charts

我知道有一个chart.leftAxis.startAtZeroEnabled = false选项,但它不适用于我。

我制作了一个线图来追踪Instagram追随者随着时间的推移而且我的数据点变化很小所以我需要调整y轴以便更改已启用,但它 enter image description here

以下是我对图表的代码:

let chart = LineChartView(frame: chart1View.bounds)
            let dayTrackerUnits = trackerCrawler.thisWeeksPoints()

            var xVals : [Int] = []
            var yVals : [ChartDataEntry] = []
            var minimum = 1000000000
            var maximum = 0
            for i in 0..<dayTrackerUnits.count {
                xVals.append(i)
                yVals.append(ChartDataEntry(value: Double(dayTrackerUnits[i].followerCount), xIndex: i))

                if dayTrackerUnits[i].followerCount < minimum {
                    minimum = dayTrackerUnits[i].followerCount
                }

                if dayTrackerUnits[i].followerCount > maximum {
                    maximum = dayTrackerUnits[i].followerCount
                }

            }

            print(yVals)

            let chartDataSet = LineChartDataSet(yVals: yVals, label: "Followers")
            chartDataSet.setColor(GlobalStyles.sharedStyles.instagramBlue())
            chartDataSet.lineWidth = 2.0
            chartDataSet.circleRadius = 5.0
            chartDataSet.circleColors = [GlobalStyles.sharedStyles.instagramBlue()]
            chartDataSet.drawValuesEnabled = false
            chartDataSet.drawVerticalHighlightIndicatorEnabled = true

            let data = LineChartData(xVals: xVals, dataSet: chartDataSet)
            chart.data = data
            chart.delegate = self
            chart.leftAxis.startAtZeroEnabled = false
            chart.rightAxis.startAtZeroEnabled = false
            print("min: \(minimum), max: \(maximum)")
            chart.rightAxis.enabled = false
            chart1View.addSubview(chart)
            left1Label.text = "Followers"

我想将yAxisMinimum设置为minimum,将yAxisMaximum设置为maximum,但图表不会显示我想要的内容。< / p>

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用customAxisMin/Max来限制其范围。

顺便说一句,你必须确保你的数据值不包含0.看起来你的数据有0,这就是为什么它总是从0开始。它有一个小算法来计算{的轴范围y轴渲染器中的{1}}。