Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combined Chart (line- and bar chart) using iOS-Charts

I am trying to combine a line- and bar chart in swift (iOS-charts). Both the x- and the y-axis seem to be scaled properly, but the data is not showing on the chart. What am I missing?enter image description here

import UIKit
import Charts

class CombinedChartsTest: UIViewController {


@IBOutlet weak var combinedChart: CombinedChartView!


let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [2.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 17.0, 2.0, 4.0, 5.0, 4.0]

override func viewDidLoad() {
    super.viewDidLoad()

    setChart(months, yValuesLineChart:  unitsSold, yValuesBarChart: unitsSold)
}

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double]) {
    combinedChart.noDataText = "Please provide data for the chart."

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

    for i in 0..<xValues.count {

        yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
        yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] - 1, xIndex: i))

    }

    let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
    let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")

    let data: CombinedChartData = CombinedChartData(xVals: xValues, dataSets: [lineChartSet,barChartSet])

    combinedChart.data = data

}
}
like image 940
Nils Avatar asked Mar 22 '16 09:03

Nils


2 Answers

I finally managed to figure out what was missing.

import UIKit
import Charts

class CombinedChartsTest: UIViewController {


@IBOutlet weak var combinedChartView: CombinedChartView!


let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [2.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 17.0, 2.0, 4.0, 5.0, 4.0]

override func viewDidLoad() {
    super.viewDidLoad()

    setChart(months, yValuesLineChart:  unitsSold, yValuesBarChart: unitsSold)
}

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double]) {
    combinedChartView.noDataText = "Please provide data for the chart."

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

    for i in 0..<xValues.count {

        yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
        yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] - 1, xIndex: i))

    }

    let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
    let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")


    let data: CombinedChartData = CombinedChartData(xVals: xValues)
    data.barData = BarChartData(xVals: xValues, dataSets: [barChartSet])
    data.lineData = LineChartData(xVals: xValues, dataSets: [lineChartSet])

    combinedChartView.data = data

}
}
like image 144
Nils Avatar answered Nov 03 '22 01:11

Nils


I used like this

`

func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double] , combinedChartView:CombinedChartView) {
    combinedChartView.noDataText = "Please provide data for the chart."

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    var yVals2 : [BarChartDataEntry] = [BarChartDataEntry]()

    for i in 0..<xValues.count {

        yVals1.append(ChartDataEntry(value: yValuesLineChart[i], xIndex: i))
        yVals2.append(BarChartDataEntry(value: yValuesBarChart[i] , xIndex: i))

    }


    let lineChartSet = LineChartDataSet(yVals: yVals1, label: "Line Data")
    lineChartSet.colors = [UIColor(red: 168.0/255.0, green: 164.0/255.0, blue: 164.0/255.0, alpha: 1.0)]

    // lineChartSet.mode = .CubicBezier
    lineChartSet.lineWidth = 5.0
    lineChartSet.drawCirclesEnabled = false
    //lineChartSet.drawFilledEnabled = true
    /*lineChartSet.colors = [UIColor(red: 0, green: 0, blue: 1, alpha: 0.0)]
     lineChartSet.fillColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1.0)
     lineChartSet.fillAlpha = 0.8*/



    let barChartSet: BarChartDataSet = BarChartDataSet(yVals: yVals2, label: "Bar Data")
    // barChartSet.colors = ChartColorTemplates.colorful()
    barChartSet.barSpace = 0.65

    barChartSet.barShadowColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0)// If alpha is > 0, then grey bars will appear.



    let data: CombinedChartData = CombinedChartData(xVals: xValues)
    data.barData = BarChartData(xVals: xValues, dataSets: [barChartSet])
    data.lineData = LineChartData(xVals: xValues, dataSets: [lineChartSet])
    //barChartView.leftAxis.startAtZeroEnabled = true
    combinedChartView.leftAxis.axisMinValue = 0

    combinedChartView.data = data

    combinedChartView.leftAxis.drawLabelsEnabled = false
    combinedChartView.rightAxis.drawLabelsEnabled = false
    //barChartView.leftAxis.startAtZeroEnabled = true
    combinedChartView.leftAxis.axisMinValue = 0
    combinedChartView.descriptionText = ""
    combinedChartView.legend.enabled = false
    combinedChartView.backgroundColor = UIColor.clearColor()
    //  combinedChartView.barSpace = 0.65


    combinedChartView.leftAxis.gridColor = UIColor(red: 179.0/255.0, green: 179.0/255.0, blue: 179.0/255.0, alpha: 0.2)
    combinedChartView.rightAxis.gridColor = UIColor(red: 179.0/255.0, green: 179.0/255.0, blue: 179.0/255.0, alpha: 0.2)

}

`

like image 42
IOS Singh Avatar answered Nov 03 '22 00:11

IOS Singh