简体   繁体   中英

iOS Swift Chart : Bar chart background colour

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.
在此处输入图像描述

This is what I have done yet.

在此处输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM