0

I am using charts framework for drawing charts. I need to add some strings in left of every bars. In my code always there are two bars, one for Income and one for Expense. I want to show these string aside of every bar. enter image description here

Below you can see my codes:

        let ys1 = [model.totalIncome, abs(model.totalExpense)]

    var yse1 = ys1.enumerated().map { x, y -> BarChartDataEntry in
    return BarChartDataEntry(x: Double(x), y: y)
    }

    let count = 2
    let data = BarChartData()
    let ds1 = BarChartDataSet(values: yse1, label: "")
    ds1.colors = [UIColor.red, UIColor.blue]

    // Number formatting of Values
    ds1.valueFormatter = YAxisValueFormatter()
    ds1.valueFont = UIFont(fontType: .H6)!
    ds1.valueTextColor = UIColor.dark_text

    // Data set
    data.addDataSet(ds1)
    horizontalBarChartView.data = data

    // General setting
    horizontalBarChartView.xAxis.drawLabelsEnabled = false
    horizontalBarChartView.setScaleEnabled(false)

    // Grid
    horizontalBarChartView.xAxis.drawGridLinesEnabled = false
    horizontalBarChartView.leftAxis.gridLineDashLengths = [15.0, 15.0]
    horizontalBarChartView.leftAxis.gridColor = UIColor.palette_28
    horizontalBarChartView.rightAxis.drawGridLinesEnabled = false


    // Disable number formatting of leftAxis and rightAxis
    horizontalBarChartView.leftAxis.enabled = false
    horizontalBarChartView.rightAxis.enabled = false


    // setViewPortOffsets
    let digitCount = Int(data.yMax).digitCount
    let leftOffcet: CGFloat = digitCount > 2 ? CGFloat((digitCount - 1) * 10) : 10.0
    horizontalBarChartView.setViewPortOffsets(left: leftOffcet, top: 0, right: 0, bottom: 50)

    horizontalBarChartView.leftAxis.axisMinimum = 0

And my view:

   lazy var horizontalBarChartView: HorizontalBarChartView = {
        let chartView = HorizontalBarChartView()
        chartView.contentMode = .scaleAspectFit
        chartView.drawBordersEnabled = false
        chartView.drawGridBackgroundEnabled = false
        chartView.gridBackgroundColor = UIColor.clear
        chartView.legend.enabled = false
        chartView.chartDescription = nil
        chartView.highlighter = nil
        chartView.clipsToBounds = true
        chartView.translatesAutoresizingMaskIntoConstraints = false
        return chartView
    }()

I guess below code should add these labels, however, it is not working

horizontalBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["Income","Expense"])
horizontalBarChartView.xAxis.granularityEnabled = true
horizontalBarChartView.xAxis.granularity = 1

Update

See orange rectangle. I need these labels.

enter image description here

7
  • Hello, Amir. Sorry, but this question does not belong on this site - it does not ask us to recommend any software. Since you have some code, ask it on S.o, where you are already active Oct 9 '17 at 12:53
  • Hello Mawg. Sorry for this question. Someone recommended to ask here :( I'll remove it @Mawg Oct 9 '17 at 14:02
  • 1
    Thanks dear @Mawg for your kinds & helping :) Oct 9 '17 at 14:51
  • 1
    If I know the answer, why not :] I hope to help others... Oct 9 '17 at 14:59
  • 1
    Welcome to Software Recommendations! Please note this site is about recommending software, not assets or resources like howtos, manuals/tutorials, multi-media content, code fragments, etc.
    – Izzy
    Oct 9 '17 at 15:01
1

Charts framework is a great tool for drawing charts. You should study it perfectly. I used an old code in our project (copy/paste), so it caused some problems for drawing xAxis labels in my chart.

Just comment below line:

horizontalBarChartView.xAxis.drawLabelsEnabled = false
4
  • Is that the answer to your question? Oct 9 '17 at 12:51
  • @Mawg Yes, this is the answer Oct 9 '17 at 14:02
  • The you can accept it, to help others in future (but you have to wait 2 days after porting to accept your own answer) Oct 9 '17 at 14:13
  • Yes, I accepted it (after 6 hours) Oct 9 '17 at 14:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.