Dumitru Rogojinaru Dumitru Rogojinaru - 12 days ago 7
iOS Question

Set Maximum Zoom for iOS Charts

I am new to programming and swift, so I have a question...
How to set the maximum zoom in iOS charts? tried:

func setBarChart(dataPoints: [String], humidity: [Double], andDays: Int, forTime: String)
{
let formato = BarChartFormatter()
let xaxis = XAxis()
var humidityEntry: [BarChartDataEntry] = []

for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(x: Double(i), y: humidity[i])
humidityEntry.append(dataEntry)
formato.getNumberOfDays(Double(i), axis: xaxis, andDays: andDays)
}

xaxis.valueFormatter = formato
let barChartDataSet3 = BarChartDataSet(values: humidityEntry, label: "Humidity")
barChartDataSet3.setColor(UIColor.cyan)
barChartDataSet3.barBorderColor = UIColor.blue
barChartDataSet3.valueTextColor = UIColor.lightText

barChartView.leftAxis.labelTextColor = UIColor.white
barChartView.rightAxis.labelTextColor = UIColor.white
barChartView.leftAxis.labelFont = UIFont.systemFont(ofSize: 11)
barChartView.rightAxis.labelFont = UIFont.systemFont(ofSize: 11)
barChartView.scaleYEnabled = false
// barChartView.setVisibleXRange(minXRange: 0.1, maxXRange: 1.1)
// barChartView.autoScaleMinMaxEnabled = true
// barChartView.setVisibleXRange(minXRange: 0.0, maxXRange: 1.0)


switch forTime {
case "6months":
barChartView.setVisibleXRange(minXRange: 0.0, maxXRange: 3.1)
case "3months":
barChartView.setVisibleXRange(minXRange: 0.0, maxXRange: 2.1)
case "1month":
barChartView.setVisibleXRange(minXRange: 0.0, maxXRange: 1.5)
case "7days":
barChartView.setVisibleXRange(minXRange: 0.0, maxXRange: 1.0)
default:
break
}

if barChartView.xRange >= 1.0 && barChartView.xRange <= 0
{
barChartView.scaleXEnabled = false
}
// barChartView.setVisibleYRangeMaximum(2.0, axis: YAxis.)
// barChartView.
// if barChartView.scaleX > 1.3
// {
// barChartView.notifyDataSetChanged()
// barChartView.scaleXEnabled = false
// }
// barChartView.notifyDataSetChanged()
// barChartView.scaleXEnabled =
barChartView.highlightFullBarEnabled = false

var dataSets = [IChartDataSet]()
dataSets.append(barChartDataSet3)

let lineChartDataTemp = BarChartData(dataSets: dataSets)
barChartView.legend.font = UIFont.systemFont(ofSize: 14)
barChartView.legend.textColor = UIColor.white
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.data = lineChartDataTemp
barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

}


But nothing works, sometimes I can zoom to infinite, other time it just shows me weird zoomed-in graphs.

Answer

Hi Dumitru Rogojinaru,

barChartView.setVisibleXRange(minXRange: 1, maxXRange: 3)

This method and description is available in BarLineChartViewBase.swift in chart classes file.

/// Limits the maximum and minimum value count that can be visible by pinching and zooming.
/// e.g. minRange=10, maxRange=100 no less than 10 values and no more that 100 values can be viewed
/// at once without scrolling

public func setVisibleXRange(minXRange minXRange: CGFloat, maxXRange: CGFloat)
{
    let maxScale = _deltaX / minXRange
    let minScale = _deltaX / maxXRange
    _viewPortHandler.setMinMaxScaleX(minScaleX: minScale, maxScaleX: maxScale)
}

Please do refer - https://github.com/danielgindi/Charts/issues/1820

I hope this will help you.