[英]Change Bar Chart Design [iOS Charts]
我創建使用BARCHART BarChartView
從圖表 ,但我無法弄清楚如何改變桿設計。
我正在使用的設計顯示在圖像的右側,而我需要實現左側的設計。
@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")
任何幫助將不勝感激。
當前,無法以所需方式更改條形設計。
但是,有人通過更改網格線以繪制條形圖來嘗試類似的操作,但未添加。
請參閱: 添加了drawGridLinesOnTopEnabled布爾標志,以在條形圖的頂部繪制網格線 。
您可以在這里查看他的工作。
另一個手動的解決方法雖然不是最好的方法,但如果絕對需要,也可以使用,它是通過在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
上顯示的任何地方畫一條線。
因此,如果您決定顯示條形數據標簽,則線條也將覆蓋它。
我希望這可以對您有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.