如何将iOS图表限制为视图大小 [英] How to constrain ios-chart to size of view

查看:52
本文介绍了如何将iOS图表限制为视图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT (简短版本):我当时正在使用带有动态数据的ios-charts显示一个图,并且该图将我的图切掉,因此该图的整个垂直部分无法正确显示.某些操作(例如从横向过渡到纵向或从纵向过渡到纵向)将导致图形正确显示.

EDIT Short version: I was displaying a graph using ios-charts with dynamic data, and it was chopping my graph off so the full vertical portion of the graph wasn't displayed properly. Certain actions (like transitioning from landscape to portrait or vice-versa) would cause the graph to be correctly displayed.

原始问题

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

I am displaying a line graph on iPhone 6 simulator running ios 9.3, using ios-charts version 2.2.4, and I'm running into a problem where the content gets vertically clipped... sometimes.

该图表是向下钻取图表,通过在另一个图中选择一个数据点来触发.对于某些数据点,如果我 first 首先选择它们,则钻取图表将被裁剪,如下所示:

The chart is a drilldown chart, triggered by selecting a data point in another plot. For some data points, if I select them first, the drilldown chart gets clipped, like so:

但是,如果我先选择另一个数据点,然后重新选择这个刚被裁剪的相同数据点,则图表将按预期的方式渲染或多或少:

However, if I select another data point first, then re-select this exact same datapoint that was just clipped, the chart renders more or less as expected:

我正在手动设置图表的最大Y值,并且打印最大Y值确认确实已设置,因此我认为问题不在于图表格式,而可能在于图表是否将自身约束到视图?换句话说,也许这是布局/情节提要问题,并且可以在视图甚至父视图上设置一个属性以使其正确呈现吗?

I am manually setting the maximum Y value of the chart, and printing the maximum Y value confirms that it is indeed being set, so I am left to believe that the problem lies not in the chart formatting, but possibly in the way the chart constraints itself to the view? In other words, maybe this is a layout / storyboard issue and there is a property I can set on the view or even the parent view to make this render correctly?

尽管如此,这是我用来生成图表的代码,以防万一我做任何明显错误的事情:

Nonetheless, here is the code I use to generate the chart in case I'm doing anything obviously wrong:

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

EDIT (故事板设置)被省略,因为它们的数量巨大,而且事实证明它们不相关

EDIT Storyboard settings omitted because they are enormous and it turns out not relevant

推荐答案

答案是在绘图函数的末尾添加 notifyDataSetChanged().

The answer was to add a notifyDataSetChanged() to the end of the plot function.

此文档我在MPAndroid图表(iOS的Android版本)上找到-图表),

Per this documentation I found on MPAndroid Chart (the Android version of ios-Charts),

notifyDataSetChanged():让图表知道其基础数据已更改,并执行所有必要的重新计算(偏移量,图例,最大值,最小值等).这是必需的,尤其是在动态添加数据时.

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.

这篇关于如何将iOS图表限制为视图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆