今天,我终于让我的自动布局约束动画工作,之后我注意到,水平条形图的行为不像以前那样。
我有一个水平条形图,数据按降序填充(最多应该在顶部),但问题是,每当我尝试使用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
希望有人能帮帮我,期待您的回答。
经过几个小时的研究和尝试,我想通了。由于一些奇怪的原因,如果你使用moveViewToX,它不响应,但当你使用moveViewTo(float xValue, float yValue, AxisDependency axis),它的工作。
所以,简而言之,我在设置图表的最后加了这行代码。
horizontalChartView.moveViewTo(xValue: Double(sortedData.count), yValue: sortedData.first!.money, axis: .left)
希望对你以后的工作有所帮助!