Convert bar chart to a grouped bar chart with danielgindi/ios-charts and Swift

后端 未结 3 1565
难免孤独
难免孤独 2020-12-29 13:20

i have created the simple bar chart with the library at https://github.com/danielgindi/ios-charts still can\'t figure out how to make this a grouped bar chart. I\'ve added t

3条回答
  •  北海茫月
    2020-12-29 13:34

    let months = ["Jan", "Feb", "Mar", "Apr", "May"]
        let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0]
        let unitsBought = [10.0, 14.0, 60.0, 13.0, 2.0]
    
         override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view, typically from a nib.
                barChartView.delegate = self
                barChartView.noDataText = "You need to provide data for the chart."
                barChartView.chartDescription?.text = "sales vs bought "
    
    
                //legend
                let legend = barChartView.legend
                legend.enabled = true
                legend.horizontalAlignment = .right
                legend.verticalAlignment = .top
                legend.orientation = .vertical
                legend.drawInside = true
                legend.yOffset = 10.0;
                legend.xOffset = 10.0;
                legend.yEntrySpace = 0.0;
    
    
                let xaxis = barChartView.xAxis
                xaxis.valueFormatter = axisFormatDelegate
                xaxis.drawGridLinesEnabled = true
                xaxis.labelPosition = .bottom
                xaxis.centerAxisLabelsEnabled = true
                xaxis.valueFormatter = IndexAxisValueFormatter(values:self.months)
                xaxis.granularity = 1
    
    
                let leftAxisFormatter = NumberFormatter()
                leftAxisFormatter.maximumFractionDigits = 1
    
                let yaxis = barChartView.leftAxis
                yaxis.spaceTop = 0.35
                yaxis.axisMinimum = 0
                yaxis.drawGridLinesEnabled = false
    
                barChartView.rightAxis.enabled = false
               //axisFormatDelegate = self
    
                setChart()
            }
    
     func setChart() {
            barChartView.noDataText = "You need to provide data for the chart."
            var dataEntries: [BarChartDataEntry] = []
            var dataEntries1: [BarChartDataEntry] = []
    
            for i in 0.. interval per "group" 
    
            let groupCount = self.months.count
            let startYear = 0
    
    
            chartData.barWidth = barWidth;
            barChartView.xAxis.axisMinimum = Double(startYear)
            let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
            print("Groupspace: \(gg)")
            barChartView.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)
    
            chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
            //chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
            barChartView.notifyDataSetChanged()
    
            barChartView.data = chartData
    
    
    
    
    
    
            //background color
            barChartView.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 1)
    
            //chart animation
            barChartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .linear)
    
    
        }
    

提交回复
热议问题