[英]iOS Swift Chart : Bar chart background colour
我創建了一個完全正常的條形圖。 如何添加條形圖膠囊背景顏色? 不想要整個圖表背景顏色,但我希望每個條形圖都需要一個背景色,這樣它看起來就像填充和不填充一樣對條形聊天的影響。
var BAR_WIDTH_CONSTANT: CGFloat = 3
var BAR_DISTANCE: CGFloat = 14
var BAR_MAX_HEIGHT: CGFloat = 50
let MAX_VALUE: CGFloat = 60
let MIN_VALUE: CGFloat = 0
var unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0]
var months = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
var graphType = 1
var maxValue = 30.0
func setChart(arrX:[String]) {
let customFormater = BarChartFormatter()
customFormater.months = self.months
viewChart.xAxis.valueFormatter = customFormater
self.viewChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
self.viewChart.setVisibleXRangeMaximum(Double(arrX.count))
self.viewChart.fitScreen()
var dataEntries = [BarChartDataEntry]()
for i in 0..<months.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(unitsSold[i]), data: months as AnyObject?)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "")
chartDataSet.valueTextColor = .clear
chartDataSet.valueFont = themeFont(size: 9, fontname: .Poppins_Black)
chartDataSet.colors = [.appThemeColor, UIColor(named: "DarkGreen")!]
//Remove 0 value from graphs
let noZeroFormatter = NumberFormatter()
noZeroFormatter.zeroSymbol = ""
chartDataSet.valueFormatter = DefaultValueFormatter(formatter: noZeroFormatter)
chartDataSet.barShadowColor = .clear
let chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = 0.6
// if graphType == 1 || graphType == 3 {
// chartData.barWidth = 0.4
// }
viewChart.data = chartData
viewChart.animate(xAxisDuration: 1, yAxisDuration: 1, easingOption: .linear)
}
func setupChart() {
viewChart.delegate = self
viewChart.chartDescription.enabled = false
viewChart.dragEnabled = true
viewChart.doubleTapToZoomEnabled = false
viewChart.pinchZoomEnabled = false
viewChart.xAxis.valueFormatter = self
viewChart.legend.enabled = false
viewChart.setScaleEnabled(false)
viewChart.isUserInteractionEnabled = false
let leftAxisFormatter = NumberFormatter()
leftAxisFormatter.maximumFractionDigits = 0
let xAxis = viewChart.xAxis
xAxis.drawGridLinesEnabled = true
xAxis.labelPosition = .topInside
xAxis.labelRotationAngle = 0
xAxis.labelFont = themeFont(size: 12, fontname: .Poppins_Light)
xAxis.labelTextColor = .darkGray
xAxis.granularity = 1
xAxis.axisLineWidth = 0
xAxis.labelCount = 12
xAxis.granularityEnabled = true
xAxis.valueFormatter = self
xAxis.labelRotationAngle = 0
xAxis.gridColor = .clear
let yAxis = viewChart.leftAxis
yAxis.drawGridLinesEnabled = false
yAxis.axisMinimum = 0
yAxis.axisMaximum = Double(maxValue)
yAxis.axisLineWidth = 0
yAxis.labelTextColor = .clear
yAxis.drawLabelsEnabled = true
yAxis.labelPosition = .outsideChart
let dAxis = viewChart.rightAxis
dAxis.drawGridLinesEnabled = false
dAxis.axisMinimum = 0
dAxis.axisLineWidth = 0
dAxis.labelTextColor = .clear
dAxis.drawLabelsEnabled = true
dAxis.labelPosition = .outsideChart
}
這就是我所做的。
if you are drwaing your BarChart using Charts, then to apply corner
radius you should have to change the internal code of the Charts
Framework.
You can do it as:
Search for the BarChartRenderer.swift file and replace the (every)line
context.fill(barRect)
with the code below
let bezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: 3.0)
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.