gpt4 book ai didi

ios - 如何将 ios-chart 限制为 View 大小

转载 作者:行者123 更新时间:2023-11-28 08:39:19 26 4
gpt4 key购买 nike

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


原始问题

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

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

Chart is clipped around 31 for some reason

但是,如果我先选择另一个数据点,然后重新选择刚刚被裁剪的完全相同的数据点,图表或多或少会按预期呈现:

Chart is not clipped and extends to ~66 as expected

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

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

... // 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 设置被省略,因为它们很大而且结果不相关

最佳答案

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

根据 this documentation我在 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, ...). This is needed especially when adding data dynamically.

关于ios - 如何将 ios-chart 限制为 View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com