今天,我终于使我的自动布局约束动画开始工作了,此后,我注意到,水平条形图的行为不再像以前那样。
我有一个水平条形图,其中的数据按降序填充(最上面应该在顶部),但是问题是,每当我尝试使用moveViewToX将视图移回到第一项(最大值)时,图表没有执行任何操作,它总是显示最后添加的条目,因此将其滚动到末尾。
我想从顶部开始,我的意思是从第一个条目开始。
我已经尝试过moveViewToX函数,但是它仍然没有效果。 这是设置数据的代码:
let sortedData = chartData.sorted(by: { $0.money < $1.money })
for i in 0..<sortedData.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(sortedData[i].money))
dataEntries.append(dataEntry)
labels2.append(sortedData[i].Name)
}
这是图表的设置:
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "kategoriak")
chartDataSet.colors = [UIColor.red]
let kerekitöFormatter = KerekitöNumberFormatter(showZeros: false, showMoney: true)
chartDataSet.valueFormatter = kerekitöFormatter
chartDataSet.valueFont = chartDataSet.valueFont.withSize(10)
chartDataSet.valueTextColor = .white
let horChartData = BarChartData(dataSet: chartDataSet)
horizontalChartView.xAxis.valueFormatter = BarChartXaxisFormatter(labels: labels2, truncMode: true)
horizontalChartView.data = horChartData
horizontalChartView.leftAxis.axisMinimum = 0
horizontalChartView.leftAxis.valueFormatter = YAxisValueFormatter()
horizontalChartView.legend.enabled = false
horizontalChartView.xAxis.labelPosition = .bottom
horizontalChartView.setVisibleXRangeMaximum(5)
horizontalChartView.moveViewToX(Double(sortedData.count))
horizontalChartView.drawValueAboveBarEnabled = false
horizontalChartView.xAxis.granularityEnabled = true
//horizontalChartView.setExtraOffsets (left: 0.0, top: 0.0, right:0.0, bottom: 0.0)
horizontalChartView.xAxis.granularity = 1
horizontalChartView.doubleTapToZoomEnabled = false
if #available(iOS 13.0, *) {
horizontalChartView.backgroundColor = .systemBackground
horizontalChartView.xAxis.labelTextColor = .label
horizontalChartView.leftAxis.labelTextColor = .label
horizontalChartView.gridBackgroundColor = .label
}
let rightAxis = horizontalChartView.rightAxis
rightAxis.drawGridLinesEnabled = false
rightAxis.enabled = false
我希望有人可以提供帮助。 期待您的回答。
答案 0 :(得分:0)
经过数小时的研究和尝试,我弄清楚了。出于某些奇怪的原因,如果您使用moveViewToX,它不会响应,但是当您使用moveViewTo(float xValue,float yValue,AxisDependency轴)时,它将起作用。
因此,简而言之,我在设置图表的末尾添加了这行代码。
horizontalChartView.moveViewTo(xValue: Double(sortedData.count), yValue: sortedData.first!.money, axis: .left)
希望这对以后有帮助!