iOS Swift图表:条形图背景颜色

发布于 2025-01-29 10:35:18 字数 3855 浏览 1 评论 0原文

我创建了一个完全可以正常工作的条形图。如何添加条形图胶囊背景颜色?不希望整个图表背景颜色,但我想为每个栏供您使用背景颜色,因此看起来像是填充和像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'm looking for.
enter image description here

This is what I have done yet.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷乱花海 2025-02-05 10:35:18

如果您使用图表来绘制Barchart,则要应用Corne Radius,则必须更改图表框架的内部代码。

您可以这样做:
搜索(shift+命令+o)for barchartrenderer.swift(shift+命令+o)文件,然后将(每个)行替换

context.fill(barRect)

为下面的代码

let bezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: 3.0)
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)

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

context.fill(barRect)

with the code below

let bezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: 3.0)
context.addPath(bezierPath.cgPath)
context.drawPath(using: .fill)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文