3

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)
    }
DevB2F
  • 4,674
  • 4
  • 36
  • 60
Sudhanshu Gupta
  • 213
  • 4
  • 15

2 Answers2

1

Each date needs to be stored as a string and added to the xvalues array. This is how I have solved this:

 override func viewDidLoad() {
    super.viewDidLoad()

    var xvalues: [String] = [String]()
    xvalues.append("3 Sept")
    xvalues.append("16 Sept")
    xvalues.append("15 Oct")
    xvalues.append("15 Oct")
    xvalues.append("15 Oct")

    let xAxis = lineChart.xAxis
    xAxis.labelPosition = .bothSided
    xAxis.axisMinimum = 0.0
    xAxis.granularity = 1.0

    lineChart.leftAxis.enabled = false
    lineChart.rightAxis.enabled = false
    lineChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: xvalues)

    let data = generateLineData()
    lineChart.data = data


}

func generateLineData() -> LineChartData {
    let data: LineChartData = LineChartData()
    var entries: [ChartDataEntry] = []
    var dataArr: [Int] = []
    dataArr.append(10)
    dataArr.append(5)
    dataArr.append(8)
    dataArr.append(12)
    dataArr.append(10)

    for index in 0..<5 {
        entries.append(ChartDataEntry(x: Double(index),  y: Double(dataArr[index]) ) )
    }

    let set: LineChartDataSet = LineChartDataSet(values: entries, label: "My label")
    set.setCircleColor(UIColor.blue)
    set.lineWidth = 1
    set.circleRadius = 5
    set.drawCircleHoleEnabled = false
    set.valueTextColor = UIColor.blue
    set.valueFont = UIFont(name: "Verdana", size: 12.0)!
    set.drawFilledEnabled = true
    set.mode = Charts.LineChartDataSet.Mode.linear
    set.axisDependency = Charts.YAxis.AxisDependency.left

    data.addDataSet(set)

    return data
}
DevB2F
  • 4,674
  • 4
  • 36
  • 60
  • i tried your things. I faced an issue that data points if big gets out of the left and right axis. Second is there is way to remove x axis lines and place y axis lines. Basically left axis should start at an offset from the left most part of the view – Sudhanshu Gupta May 18 '17 at 13:01
  • If the string is too long, then you can use this: "3 \n Sept" to split it to two lines. I recommend creating a new question for the other issue you mentioned. But if the answer helped you, can you please accept the answer. – DevB2F May 18 '17 at 18:02
1

In swift 3: Answer 1:

chartView.leftAxis.enabled = false

Answer 4:

chartView.legend.enabled = false
Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21