2

I have used this library on swift called: iOS Charts https://github.com/danielgindi/ios-charts

I have two datasets and I want to set the position of data labels in one of the dataset to the bottom of the line, so the numbers can be visible and no overlapping happens. How can I do this?

enter image description here

I setup the chart as follows:

private func configureChart() {
    lineChartView = LineChartView(frame: CGRect(x: 0, y: 60, width: self.view.frame.width, height: 200))

    lineChartView?.delegate = self

    lineChartView?.chartDescription?.enabled = false
    lineChartView?.dragEnabled = true
    lineChartView?.setScaleEnabled(false)
    lineChartView?.pinchZoomEnabled = false
    lineChartView?.rightAxis.enabled = false

    lineChartView?.xAxis.valueFormatter = self
    lineChartView?.xAxis.granularity = 1

    lineChartView?.legend.form = .line

    lineChartView?.animate(yAxisDuration: 0.3)

    if let lineChartView = lineChartView {
        dashboardHeaderView?.subviews.filter({ $0 is LineChartView }).forEach {
            $0.removeFromSuperview()
        }
        dashboardHeaderView?.addSubview(lineChartView)
    }
    setupLineChartData()
}

func setupLineChartData() {
    monthData = ReportModel.monthlyOveralInfo()
    let costSet = self.provideLineData(type: .totalCost)
    let incomeSet = self.provideLineData(type: .totalIncome)

    let lineChartData = LineChartData(dataSets: [incomeSet, costSet])
    lineChartView?.data = lineChartData
    lineChartView?.setVisibleXRangeMaximum(5)
    lineChartView?.moveViewToX(lineChartView?.chartXMax ?? 0)
}

private func provideLineData(type: SWMonthlyOverallType) -> LineChartDataSet {
    var mainColor: UIColor = .black
    var gradientFirstColor: UIColor = .clear
    var gradientSecondColor: UIColor = .black
    if type == .totalIncome {
        mainColor = .myAppGreen
        gradientFirstColor = .clear
        gradientSecondColor = .myAppGreen
    }

    let totalCosts = monthData.compactMap({
        $0.items.first(where: {$0.type == type})
    })

    var index: Double = -1
    let values: [ChartDataEntry] = totalCosts.compactMap({
        index += 1
        return ChartDataEntry(x: index, y: $0.value)
    })

    let chartDataSet = LineChartDataSet(values: values, label: type.rawValue)
    chartDataSet.resetColors()
    chartDataSet.drawIconsEnabled = false

    chartDataSet.setColor(mainColor)
    chartDataSet.setCircleColor(mainColor)
    chartDataSet.lineWidth = 1
    chartDataSet.circleRadius = 3
    chartDataSet.drawCircleHoleEnabled = true
    chartDataSet.valueFont = .systemFont(ofSize: 9)

    let gradientColors = [gradientFirstColor.cgColor,
                          gradientSecondColor.cgColor]
    let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)

    chartDataSet.fillAlpha = 0.5
    if let gradient = gradient {
        chartDataSet.fill = Fill(linearGradient: gradient, angle: 90)
    }
    chartDataSet.drawFilledEnabled = true

    return chartDataSet
}
Soheil Novinfard
  • 1,358
  • 1
  • 16
  • 43

1 Answers1

1

Just posting in case someone finds it to be useful:

chart.xAxis.labelPosition = .bottom
Lilya
  • 495
  • 6
  • 20