简体   繁体   English

如何将ios-chart约束为视图大小

[英]How to constrain ios-chart to size of view

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. 编辑简短版本:我使用带有动态数据的ios-charts显示图表,它正在关闭我的图表,因此图表的完整垂直部分未正确显示。 Certain actions (like transitioning from landscape to portrait or vice-versa) would cause the graph to be correctly displayed. 某些操作(如从横向过渡到纵向,反之亦然)会导致图形正确显示。


Original Question 原始问题

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 . 我正在运行ios 9.3的iPhone 6模拟器上显示一个线图,使用ios-charts版本2.2.4,我遇到了一个问题,其中内容被垂直剪切...... 有时候

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: 对于一些数据点,如果我选择他们,下钻图表将被剪掉,就像这样:

由于某种原因,图表被裁剪为31左右

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: 但是,如果我首先选择另一个数据点,然后重新选择刚刚剪切的完全相同的数据点 ,则图表会按预期呈现更多或更少:

图表未被剪裁并按预期延伸至~66

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? 我手动设置图表的最大Y值,并打印最大Y值确认它确实被设置,所以我不得不相信问题不在于图表格式化,而在于图表限制的方式本身的观点? 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 Storyboard settings omitted because they are enormous and it turns out not relevant 编辑故事板设置被省略,因为它们是巨大的,并且结果是不相关的

The answer was to add a notifyDataSetChanged() to the end of the plot function. 答案是在plot函数的末尾添加notifyDataSetChanged()

Per this documentation I found on MPAndroid Chart (the Android version of ios-Charts), 根据我在MPAndroid Chart(ios-Charts的Android版本)上找到的这个文档

notifyDataSetChanged(): Lets the chart know it's [sic] underlying data has changed and performs all necessary recalculations (offsets, legend, maxima, minima, ...). notifyDataSetChanged():让图表知道它的[sic]基础数据已经改变并执行所有必要的重新计算(偏移,图例,最大值,最小值,......)。 This is needed especially when adding data dynamically. 特别是在动态添加数据时需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM