Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 239
Dumitru Rogojinaru Avatar asked Nov 29 '16 08:11

Dumitru Rogojinaru


2 Answers

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.

like image 156
Hasya Avatar answered Oct 08 '22 20:10

Hasya


Btw found also a method that can assign only the maximum scale factor:

   switch forTime {
    case "6months":
        barChartView.viewPortHandler.setMaximumScaleX(26.5)
    case "3months":
        barChartView.viewPortHandler.setMaximumScaleX(13.0)
    case "1month":
        barChartView.viewPortHandler.setMaximumScaleX(4.5)
    case "7days":
        barChartView.viewPortHandler.setMaximumScaleX(1.0)
    default:
        break
    }

If somebody need that.

like image 32
Dumitru Rogojinaru Avatar answered Oct 08 '22 18:10

Dumitru Rogojinaru