如何将 ios-图表限制为视图大小

How to constrain ios-chart to size of view

编辑 简短版本: 我正在使用带有动态数据的 ios-charts 显示图表,它正在切断我的图表,因此图表的完整垂直部分无法正确显示。某些操作(例如从横向转换为纵向或反之亦然)会导致图表正确显示。


原题

我在 iPhone 6 模拟器 运行 ios 9.3 上显示折线图,使用 ios-charts 版本 2.2.4,我 运行 遇到内容被垂直剪切的问题... 有时

该图表是一个向下钻取图表,由 selecting 另一个图中的数据点触发。对于某些数据点,如果我先 select 它们 ,则向下钻取图表会被截断,如下所示:

但是,如果我先 select 另一个数据点,然后重新 select 刚刚被剪裁的完全相同的数据点 ,图表呈现更多或低于预期:

我正在手动设置图表的最大Y值,打印最大Y值确认确实设置了,所以我只能相信问题不在于图表格式,而可能在于图表将自身限制在视图中的方式?换句话说,也许这是一个布局/情节提要问题,并且有一个 属性 我可以在视图甚至父视图上设置以正确渲染?

不过,这是我用来生成图表的代码,以防我做错任何事情:

... // Part where I actually get the data omitted bc its boring
let chartDataSet = LineChartDataSet(yVals: dataEntries, label: "")
let chartData = LineChartData(xVals: xVals, dataSet: chartDataSet)
drilldownChart.data = chartData
formatChart(drilldownChart, yMax: [runningTotal, Double(goal.target)].maxElement()!)
chartDataSet.colors = [UIColor.blackColor()]
chartDataSet.circleColors = [UIColor.blackColor()]
chartDataSet.drawCirclesEnabled = false
chartDataSet.drawFilledEnabled = true
chartDataSet.fillColor = UIColor.blackColor()
chartDataSet.fillAlpha = 1.0
chartDataSet.valueColors = dataPointLabelColors
print(drilldownChart.chartYMax) // Still prints 66, y'know, just in case

// Format a chart by eliminating grid lines, decimals, right axis, etc.
func formatChart(chart: BarLineChartViewBase, yMax: Double) {
    let formatter = NSNumberFormatter()
    formatter.minimumFractionDigits = 0
    chart.xAxis.labelPosition = .Bottom
    chart.xAxis.gridColor = UIColor.clearColor()
    chart.leftAxis.axisMinValue = 0.0
    chart.leftAxis.valueFormatter = formatter
    chart.leftAxis.granularity = 1 // Sets a minimum granularity, but allows for higher granularities
    chart.leftAxis.axisMaxValue = [1.0, yMax * 1.1].maxElement()!
    chart.leftAxis.gridColor = UIColor.clearColor()
    chart.rightAxis.enabled = false
    chart.legend.enabled = false
    chart.data!.setValueFormatter(formatter)
    if (chart.data!.xValCount > 20) { chart.data!.setDrawValues(false) }
    chart.descriptionText = ""
    chart.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .Linear)
    chart.pinchZoomEnabled = false
    chart.doubleTapToZoomEnabled = false

    let limit = ChartLimitLine(limit: Double(goal!.target), label: "")
    limit.lineColor = UIColor.blackColor()
    limit.lineDashLengths = [4.0, 2.0]
    chart.leftAxis.addLimitLine(limit)
    print(chart.leftAxis.axisMaxValue) // prints 66
    print(chart.chartYMax) // Prints 66
}

编辑 省略了故事板设置,因为它们很大,结果证明不相关

答案是在 plot 函数的末尾添加一个 notifyDataSetChanged()

根据我在 MPAndroid 图表(ios-图表的 Android 版本)上找到的 this documentation

notifyDataSetChanged(): Lets the chart know it's [sic] underlying data has changed and performs all necessary recalculations (offsets, legend, maxima, minima, ...). This is needed especially when adding data dynamically.