简体   繁体   English

iOS Swift 图表:条形图背景色

[英]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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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