简体   繁体   中英

How to make y-axis not start at zero?

I know there is a chart.leftAxis.startAtZeroEnabled = false option but it's not working for me.

I'm making a line graph to track Instagram Followers over time and I have data points that vary very little so I need the y-axis adjusted so that the changes are enabled, but it 在此处输入图片说明

Here is the code I have for the graph:

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"

I'd like to set the yAxisMinimum to minimum and the yAxisMaximum to maximum but the graph won't display how I want.

Any suggestions?

You can just use customAxisMin/Max to limit its range.

BTW, you have to make sure your data values does not contains 0. It seems like your data has 0, that's why it always start from 0. It has a small algorithm to calculate the axis range in computeAxisValues in y axis renderer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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