我是iOS项目中的图表框架,用于显示折线图。当至少一个数据大于0时,它工作正常,但当数据为0时,它显示负值。 Results fine when data is 1 Shows negative values when all data is 0
这是我正在使用的代码。我调用setData函数将数据传递到图表 `导入UIKit 导入图表 GraphCollectionViewCell类:UICollectionViewCell {
let xaxis = ["1","2","3","4","5","6","7"]
@IBOutlet weak var graphView: LineChartView!
@IBOutlet weak var sentCountLbl: UILabel!
@IBOutlet weak var opensCountLbl: UILabel!
@IBOutlet weak var opensLbl: UILabel!
override func awakeFromNib() {
graphView.isUserInteractionEnabled = false
self.graphView.gridBackgroundColor = UIColor.white
self.graphView.xAxis.labelPosition = .bottom
self.graphView.xAxis.drawGridLinesEnabled = false
self.graphView.leftAxis.gridLineWidth = 0.5
self.graphView.leftAxis.gridLineDashLengths = [1]
self.graphView.leftAxis.drawAxisLineEnabled = false
self.graphView.xAxis.drawAxisLineEnabled = false
self.graphView.rightAxis.enabled = false
self.graphView.xAxis.labelFont = UIFont.systemFont(ofSize: 12, weight: .semibold)
self.graphView.leftAxis.labelFont = UIFont.systemFont(ofSize: 12, weight: .semibold)
self.graphView.doubleTapToZoomEnabled = false
self.graphView.pinchZoomEnabled = false
self.graphView.chartDescription?.text = ""
self.graphView.xAxis.labelTextColor = UIColor.gray
self.graphView.leftAxis.labelTextColor = UIColor.gray
self.graphView.xAxis.avoidFirstLastClippingEnabled = false
self.graphView.xAxis.granularity = 1
self.graphView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xaxis)
self.graphView.xAxis.spaceMin = 0.5
self.graphView.xAxis.spaceMax = 0.5
self.graphView.legend.enabled = false
self.graphView.noDataText = "Nothing to show"
}
override func prepareForReuse() {
super.prepareForReuse()
}
//Set chart data
func setChartData(xData:[Double], yData:[Double]) {
graphView.animate(yAxisDuration: 1)
var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xData.count {
yVals1.append(ChartDataEntry(x:Double(i), y:yData[i] ))
}
// 2 - create a dataset with our array
let set1: LineChartDataSet = LineChartDataSet(values: yVals1, label:nil)
set1.axisDependency = .right
set1.setColor(UIColor(red:0.34, green:0.67, blue:0.18, alpha:1))
set1.setCircleColor(UIColor(red:0.34, green:0.67, blue:0.18, alpha:1))
set1.lineWidth = 2
set1.circleRadius = 5
set1.circleHoleColor = UIColor(red: 0.973, green: 0.973, blue: 0.973, alpha: 1)
set1.fillColor = UIColor(red:0.34, green:0.67, blue:0.18, alpha:1)
set1.highlightColor = UIColor.red
set1.drawCircleHoleEnabled = true
let data: LineChartData = LineChartData()
data.addDataSet(set1)
data.highlightEnabled = false
data.setValueTextColor(graphView.backgroundColor)
graphView.data = data
}
}`
var days:[Double] = [1,2,3,4,5,6,7] 变量百分比:[双精度] = [0,0,0,0,0,0,0] cell.setChartData(xData:days,yData:percentage)