我使用BarChartView
的Charts创建了一个BarChart,但我无法弄清楚如何改变条形设计。
我正在使用的设计在图像的右侧看到,而我需要实现左侧设计。
@IBOutlet weak var charts: BarChartView!
charts.chartDescription?.enabled = false
charts.isUserInteractionEnabled = false
//charts.maxVisibleCount = 10
charts.drawBarShadowEnabled = false
charts.legend.enabled = false
let xAxis = charts.xAxis
xAxis.labelPosition = .bottom
xAxis.labelTextColor = .white
charts.xAxis.drawGridLinesEnabled = false
charts.rightAxis.drawGridLinesEnabled = false
charts.rightAxis.drawLabelsEnabled = false
charts.leftAxis.drawGridLinesEnabled = false
charts.leftAxis.drawLabelsEnabled = false
charts.leftAxis.axisMinimum = 0 ///
charts.rightAxis.axisMinimum = 0
charts.fitBars = true
charts.legend.textColor = .white
charts.setBarChartData(xValues: time, yValues: data, label: "Bar Chart")
任何帮助将不胜感激。
目前,无法以您想要的方式更改条形设计。
然而,有人通过更改网格线来绘制条形图但确实没有添加。 见:Added drawGridLinesOnTopEnabled boolean flag to draw grid lines on top of Bar Charts。 你可以查看他的作品here。
另一种手动解决方法,虽然不是最好的但是如果你绝对需要的话应该可以工作,就是在chartView
(或任何其他UIView
)的顶部放置一个透明的非透明线条视图
class HorizontalLines: UIView {
override func draw(_ rect: CGRect) {
//number of parts to divide the height of view by
let segments = 20
//number of lines ignoring the bottom most line
let numberOfLines = segments - 1
//draw the lines from left to right
for i in (1...numberOfLines).reversed() {
let multiplier = CGFloat(i)/CGFloat(segments)
let y = rect.size.height * multiplier
let startPoint = CGPoint(x:0, y:y)
let endPoint = CGPoint(x:rect.size.width, y:y)
let aPath = UIBezierPath()
aPath.move(to: startPoint)
aPath.addLine(to: endPoint)
aPath.close()
//line width
aPath.lineWidth = 1
aPath.stroke()
aPath.fill()
}
}
}
let lineView = HorizontalLines()
lineView.isUserInteractionEnabled = false
lineView.backgroundColor = .clear
lineView.translatesAutoresizingMaskIntoConstraints = false
chartView.addSubview(lineView)
lineView.leadingAnchor.constraint(equalTo: chartView.leadingAnchor).isActive = true
lineView.trailingAnchor.constraint(equalTo: chartView.trailingAnchor).isActive = true
lineView.topAnchor.constraint(equalTo: chartView.topAnchor).isActive = true
lineView.bottomAnchor.constraint(equalTo: chartView.bottomAnchor).isActive = true
请注意,这将在chartView
上显示的任何内容上划一条线。
因此,如果您决定显示条形数据标签,那么这些线条也将覆盖它。
我希望这能以某种方式帮助你:)