[Solved] Swift – Create charts(bar, line, pie, column) programatically [closed]


After you look into documentation for charts a bit more, you would probably for example if you make a line chart LineChartView() you can have different update functions to change how it looks, for example here is one of mine:

func lineChartUpdate(dataPoints: [String], values: [Double]) {

    //Graph data management
    var lineChartEntry = [ChartDataEntry]()

    for i in 0..<prices.count {

        //Graph marker from extension

        if prices != [] {

            let value = ChartDataEntry(x: Double(i), y: values[i])
            lineChartEntry.append(value)

            let line1 = LineChartDataSet(values: lineChartEntry, label: "Price")

            line1.setColor(.white)
            line1.drawVerticalHighlightIndicatorEnabled = false
            line1.drawHorizontalHighlightIndicatorEnabled = false
            line1.mode = .cubicBezier
            line1.lineWidth = 2.0
            line1.drawValuesEnabled = true
            line1.valueTextColor = UIColor.white
            line1.drawCirclesEnabled = false

            chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:self.days)
            chartView.xAxis.granularity = 1
            chartView.leftAxis.drawGridLinesEnabled = false
            chartView.xAxis.drawGridLinesEnabled = false
            //Expanded

            chartView.rightAxis.enabled = false
            chartView.leftAxis.enabled = false
            chartView.xAxis.enabled = false

            chartView.rightAxis.drawGridLinesEnabled = false
            chartView.legend.enabled = false

            chartView.dragEnabled = false
            chartView.pinchZoomEnabled = false
            chartView.drawMarkers = false
            chartView.doubleTapToZoomEnabled = false

                self.chartView.isUserInteractionEnabled = true

            //Graph Data.

                let data = LineChartData()
                data.addDataSet(line1)
                self.chartView.data = data
            self.chartLoaded = true 

        }

    }

}

If you want one view to be able to act as a Bar chart and a line chart, it may just be easier to have a View for each type in the same place, and you can change for example a Bar chart view to any type of bar chart you want and update it to change as much as you want reusing the view, but when you need a line chart just hide that view and use the line chart instead.

1

solved Swift – Create charts(bar, line, pie, column) programatically [closed]