BarChartView

PHOTO EMBED

Tue Jan 03 2023 06:16:48 GMT+0000 (Coordinated Universal Time)

Saved by @hasnat #ios #swift #barchart #bar #chart

  @IBOutlet weak var barChartView: BarChartView!

 var productTypes = ["Aluminum Cans", "Plastic Bottles", "Paper Waste"]
 var productsCountArray = [Int]()

// viewDidLoad

   setBarChartView()   

// MARK: Chart View

extension FootPrintViewController {
 
    func setBarChartView() {
        productsCountArray.append(HomeViewController.aluminumItemRecycled)
        productsCountArray.append(HomeViewController.plasticBottleRecycled)
        productsCountArray.append(HomeViewController.otherItemsCount)
        
        
        barChartView.gridBackgroundColor = UIColor.clear
//        productsCountArray = [10, 12, 9]
        
        barChartView.drawValueAboveBarEnabled = true
        barChartView.animate(yAxisDuration: 1)
        
        barChartView.xAxis.granularity = 1
        barChartView.pinchZoomEnabled = true
        barChartView.drawBarShadowEnabled = false
        barChartView.drawBordersEnabled = false
        barChartView.doubleTapToZoomEnabled = false
        barChartView.drawGridBackgroundEnabled = true
        
        setBarChartData()
    }
    
    func setBarChartData() {
        barChartView.noDataText = "No Data available for Chart"
        
        barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:productTypes)

        barChartView.leftAxis.granularity = 1
        barChartView.rightAxis.granularity = 1

        var dataEntries: [BarChartDataEntry] = []
        for i in 0..<productTypes.count {
           let dataEntry = BarChartDataEntry(x: Double(i), y: Double(productsCountArray[i]))
           dataEntries.append(dataEntry)
        }
        
        
        let chartDataSet = BarChartDataSet(entries: dataEntries, label: "Bar Chart")
        chartDataSet.colors = [.systemGreen, .red , .systemBlue]
        
        
        
        let chartData = BarChartData(dataSet: chartDataSet)
        barChartView.data = chartData
    }
}
content_copyCOPY