Search code examples
iosiphoneswiftchartsios-charts

Show x axis values real in danielgindi/Charts ios


I need to create a line chart in ios, so i searched and found out that danielgindi/Charts is one of the best options to use.

I started using it and get the chart plotted but i need to have some changes in UI. I tried to give danielgindi LineChart the look which i wanted but i am not getting it.

I want the values provided by me for x axis should reflect as is but it is not reflecting?

Chart i want to draw :-Line chart needed

Chart i have created :-Line chart made using ios charts

Things need to change :-

  1. left axis values -> i tried to remove it by lineChart.rightAxis.enabled = false. It removes y axis lines (lines parallel to x axis). I want to have y axis lines.

  2. Dotted axis lines instead of full.

  3. X axis should show values correctly, Jan, jan, feb is shown instead of jan, feb , march and that too not correct.

  4. square black box needs to be removed.

This is the code :-

    override func viewDidLoad() {
        super.viewDidLoad()

        self.lineChart.delegate = self
        self.lineChart.chartDescription?.textColor = UIColor.white

        let months = ["Jan" , "Feb", "Mar"]
        let dollars1 = [1453.0,2352,5431]
        setChart(months, values: dollars1)
    }

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

        var dataEntries: [ChartDataEntry] = []

        for i in 0 ..< dataPoints.count {
            dataEntries.append(ChartDataEntry(x: Double(i), y: values[i]))
        }

        let lineChartDataSet = LineChartDataSet(values: dataEntries, label: nil)
        lineChartDataSet.axisDependency = .left
        lineChartDataSet.setColor(UIColor.black)
        lineChartDataSet.setCircleColor(UIColor.black) // our circle will be dark red
        lineChartDataSet.lineWidth = 1.0
        lineChartDataSet.circleRadius = 3.0 // the radius of the node circle
        lineChartDataSet.fillAlpha = 1
        lineChartDataSet.fillColor = UIColor.black
        lineChartDataSet.highlightColor = UIColor.white
        lineChartDataSet.drawCircleHoleEnabled = true

        var dataSets = [LineChartDataSet]()
        dataSets.append(lineChartDataSet)


        let lineChartData = LineChartData(dataSets: dataSets)
        lineChart.data = lineChartData
        lineChart.rightAxis.enabled = false
        lineChart.xAxis.drawGridLinesEnabled = false
        lineChart.xAxis.labelPosition = .bottom
        lineChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
    }

Solution

  • In swift 3: Answer 1:

    chartView.leftAxis.enabled = false
    

    Answer 4:

    chartView.legend.enabled = false