iOS Swift图表:条形图背景颜色
我创建了一个完全可以正常工作的条形图。如何添加条形图胶囊背景颜色?不希望整个图表背景颜色,但我想为每个栏供您使用背景颜色,因此看起来像是填充和像bar聊天一样露出。
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
}
这是我所做的。
I create a bar chart that works completely fine. How can I add the bar chart capsule background color? Not want the entire chart background color but I want to need a background color for each bar so it will look like fill and unfill like effet to the bar chat.
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
}
This is what I have done yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用图表来绘制Barchart,则要应用Corne Radius,则必须更改图表框架的内部代码。
您可以这样做:
搜索(shift+命令+o)for barchartrenderer.swift(shift+命令+o)文件,然后将(每个)行替换
为下面的代码
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 (Shift+Command+O) for the BarChartRenderer.swift (Shift+Command+O) file and replace the (every)line
with the code below