gpt4 book ai didi

设置 setVisibleXRangeMaximum 时,iOS-Charts X 轴值无限重复

转载 作者:行者123 更新时间:2023-11-28 07:23:23 27 4
gpt4 key购买 nike

我正在尝试绘制一个图表,其中 x 轴是 TimeInterval,Y 轴是功耗。将有太多数据无法显示,因为每天都会有数据。因此,我想一次显示 5 个 x 值。我通过在为图表设置数据后设置 self.chart.setVisibleXRangeMaximum(5) 来实现这一点。虽然显示了 5 个 x 轴值,但这些值会无限期重复。就像第一天是 10/10/2016 之后的所有 x 轴值对于无限滚动 都是相同的(见下面的屏幕截图)。其他一些 SO 答案说启用设置粒度将解决这个问题,但即使在启用它之后它也会显示第一点的重复不确定值。我该如何解决这个问题?

我目前使用的代码(使用虚拟数据进行测试)

         func setupChart(){
self.chart = BarChartView()
self.chart.translatesAutoresizingMaskIntoConstraints = false

self.chart.chartDescription?.enabled = false

self.chart.dragEnabled = true
self.chart.setScaleEnabled(true)
self.chart.pinchZoomEnabled = true


self.chart.delegate = self

self.chart.drawBarShadowEnabled = false
self.chart.drawValueAboveBarEnabled = true



let xAxis = self.chart.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .systemFont(ofSize: 10)
xAxis.granularityEnabled = true
xAxis.granularity = 1
xAxis.labelCount = 5
xAxis.drawGridLinesEnabled = false
xAxis.valueFormatter = DateValueFormatter() //DayAxisValueFormatter(chart: self.chart)


let leftAxisFormatter = NumberFormatter()
leftAxisFormatter.minimumFractionDigits = 0
leftAxisFormatter.maximumFractionDigits = 1
leftAxisFormatter.negativeSuffix = " KWH"
leftAxisFormatter.positiveSuffix = " KWH"

let leftAxis = self.chart.leftAxis
leftAxis.labelFont = .systemFont(ofSize: 10)
leftAxis.drawGridLinesEnabled = false
leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: leftAxisFormatter)
leftAxis.labelPosition = .outsideChart
leftAxis.spaceTop = 0.15
leftAxis.labelCount = 8
leftAxis.axisMinimum = 0 // FIXME: HUH?? this replaces startAtZero = YES


let l = self.chart.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.drawInside = false
l.form = .circle
l.formSize = 9
l.font = UIFont(name: "HelveticaNeue-Light", size: 11)!
l.xEntrySpace = 4

let marker = XYMarkerView(color: UIColor(white: 180/250, alpha: 1),
font: .systemFont(ofSize: 12),
textColor: .white,
insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8),
xAxisValueFormatter: self.chart.xAxis.valueFormatter!)
marker.chartView = self.chart
marker.minimumSize = CGSize(width: 80, height: 40)
self.chart.marker = marker

self.chart.rightAxis.enabled = false

self.addSubview(self.chart)


let upperConstraintChart = NSLayoutConstraint(item: self.chart, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.titleLabel!, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 20)
let leftConstraintChart = NSLayoutConstraint(item: self.chart, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 10)
let rightConstraintChart = NSLayoutConstraint(item: self.chart, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.right, multiplier: 1, constant: -10)
let bottomConstraintChart = NSLayoutConstraint(item: self.chart, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -10)

NSLayoutConstraint.activate([upperConstraintChart,leftConstraintChart,rightConstraintChart,bottomConstraintChart])

self.setupDataForChart()
}

func setupDataForChart(){
var set1: BarChartDataSet! = nil
var values:[BarChartDataEntry] = []
for i in 10..<30 {
// Specify date components
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
let someDateTime = formatter.date(from: "\(i)/10/2016")
print("anuran x value \(someDateTime!.timeIntervalSince1970)")
values.append(BarChartDataEntry(x: someDateTime!.timeIntervalSince1970, y: Double(20)))
}
set1 = BarChartDataSet(values: values, label: "")
var colors:[UIColor] = []
for i in 0..<values.count {
var color = UIColor(netHex: "FFC300")
if i == values.count - 1 {
color = UIColor.cyan
}
colors.append(color)
}
set1.colors = colors
//set1.drawValuesEnabled = true

let data = BarChartData(dataSet: set1)
data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 10)!)
data.barWidth = 0.9

self.chart.legend.setCustom(entries: [LegendEntry(label: "Earlier", form: Legend.Form.circle, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: 10, formLineDashLengths: [CGFloat.nan], formColor: UIColor(netHex: "FFC300")),
LegendEntry(label: "Today", form: Legend.Form.circle, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: 10, formLineDashLengths: [CGFloat.nan], formColor: UIColor.cyan)])

self.chart.data = data
self.chart.setVisibleXRangeMaximum(5)
}

我得到的结果如下:

result

它无限滚动,我不知道 11/10/2016 什么时候到来。这有什么问题吗?

最佳答案

原来您不能为 BarChartDataEntry 的 X 值设置任何值。所以你应该做的是将索引 0 放入你用来保存模型的数组的长度,并在你的 IAxisValueFormatter 子类中保存时间戳值。因此,当您覆盖 stringForValue 时,从存储的属性中获取时间戳。所以你的子类应该是这样的:

import Foundation
import Charts

public class DateValueFormatter: NSObject, IAxisValueFormatter {
private let dateFormatter = DateFormatter()
var values:[TimeInterval]!

override init() {
super.init()
dateFormatter.dateFormat = "dd/MM/yy"
}

public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return dateFormatter.string(from: Date(timeIntervalSince1970: self.values![Int(value)]))
}

}

所以最重要的是你应该维护一个额外的数组来保存时间戳值,并将正常的 for loop index 作为 BarChartDataEntry

的 X 值

关于设置 setVisibleXRangeMaximum 时,iOS-Charts X 轴值无限重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57326384/

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