Search code examples
iosswiftgraphchartsline

Swift: Charts Filled Line & more


I've come up this library on swift called: Charts https://github.com/danielgindi/ios-charts

I did my best to add some functionalities but for some other(listed under) I couldn't figure out how to do it. Any help would be appreciated.

What i'm trying to do: - A filled Line chart (done) - Remove Circle at coordinates. (done) - remove all axis bars (Y+X) (todo) - remove yAxis legend (todo) - on the line, display value every x times (todo)

What i've done:

Chart Graph

Extra - on touch display yValue above the touch - gradient fill based on yValue - Animate (done)

Here is my actual code:

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

    //print(dataPoints)
    //print(values)
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    //Coloring
    let red = Double(arc4random_uniform(256))
    let green = Double(arc4random_uniform(256))
    let blue = Double(arc4random_uniform(256))
    let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)


    let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Temperatures")
    let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
    //draw as line
    lineChartDataSet.drawCubicEnabled = true
    //fill graph
    lineChartDataSet.drawFilledEnabled = true
    //color graph
    lineChartDataSet.colors = [color]

    lineChartView.data = lineChartData
    lineChartView.xAxis.labelPosition = .Bottom
    lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .EaseInCubic)
    //remove coordinate circles
    lineChartDataSet.drawCirclesEnabled = false
    //remove xAxis line
    lineChartView.xAxis.drawGridLinesEnabled = false
    lineChartView.xAxis.drawAxisLineEnabled = false

    //remove description
    lineChartView.descriptionText = ""


}

I'm looking to: remove all axis bars (Y+X) remove yAxis legend on the line, display value every x times And to display the value when graph touched.

Thanks for your time.


Solution

  • To remove all axis bars and legends you could do this:

    This is a snippet from my code:

    popularityChartView.descriptionText = ""
        popularityChartView.noDataText = Constants.messages.NO_POPULARITY_CHART_DATA
        popularityChartView.noDataTextDescription = ""
        popularityChartView.backgroundColor = UIColor(red:236/255, green:236/255,blue:236/255,alpha:1)
    
        popularityChartView.xAxis.drawAxisLineEnabled = false
        popularityChartView.xAxis.drawLimitLinesBehindDataEnabled = false
        popularityChartView.xAxis.gridColor = UIColor(red:220/255, green:220/255,blue:220/255,alpha:1)
        popularityChartView.xAxis.gridLineWidth = 0.5
        popularityChartView.xAxis.drawGridLinesEnabled = true
        popularityChartView.xAxis.drawLabelsEnabled = false
    
        popularityChartView.leftAxis.removeAllLimitLines()
        popularityChartView.leftAxis.drawZeroLineEnabled = false
        popularityChartView.leftAxis.zeroLineWidth = 0
        popularityChartView.leftAxis.drawTopYLabelEntryEnabled = false
        popularityChartView.leftAxis.drawAxisLineEnabled = false
        popularityChartView.leftAxis.drawGridLinesEnabled = false
        popularityChartView.leftAxis.drawLabelsEnabled = false
        popularityChartView.leftAxis.drawLimitLinesBehindDataEnabled = false
    
        popularityChartView.rightAxis.removeAllLimitLines()
        popularityChartView.rightAxis.drawZeroLineEnabled = false
        popularityChartView.leftAxis.zeroLineWidth = 0
        popularityChartView.rightAxis.drawTopYLabelEntryEnabled = false
        popularityChartView.rightAxis.drawAxisLineEnabled = false
        popularityChartView.rightAxis.drawGridLinesEnabled = false
        popularityChartView.rightAxis.drawLabelsEnabled = false
        popularityChartView.rightAxis.drawLimitLinesBehindDataEnabled = false
    

    My Chart

    Hope it helps.