为iOS图表设置最大缩放

时间:2016-11-29 08:57:11

标签: ios swift ios-charts

我是编程和快速的新手,所以我有一个问题...... 如何在iOS图表中设置最大缩放?尝试:

 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)

}

但没有任何作用,有时候我可以放大到无限,有时它只会向我展示奇怪的放大图。

2 个答案:

答案 0 :(得分:3)

嗨Dumitru Rogojinaru,

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

此方法和说明可在图表类文件中的BarLineChartViewBase.swift中找到。

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

请参考 - https://github.com/danielgindi/Charts/issues/1820

我希望这会对你有所帮助。

答案 1 :(得分:2)

Btw还发现了一种只能分配最大比例因子的方法:

   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
    }

如果有人需要那个。