2017-10-07 1 views
1

Ich verwende charts framework zum Zeichnen von Diagrammen. Ich muss einige Strings links von jedem Balken hinzufügen. In meinem Code gibt es immer zwei Balken, einen für Income und einen für Expense. Ich möchte diese Saite abseits von jedem Takt zeigen.Hinzufügen von String-Labels (xAxis-Labels) zum horizontalen Balken im Diagramm-Framework

Horizontal bar

Unten können Sie meine Codes sehen:

  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 

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


      // 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 

Und meine Ansicht:

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 
    }() 

ich unten Code erraten sollten diese Etiketten hinzufügen, ist es jedoch nicht

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

aktualisieren

orangefarbenes Rechteck sehen. Ich brauche diese Etiketten.

enter image description here

Antwort

1

Charts Framework ist ein großartiges Werkzeug zum Zeichnen von Diagrammen. Du solltest es perfekt lernen. Ich benutzte einen alten Code in unserem Projekt (Kopieren/Einfügen), so dass es Probleme beim Zeichnen von xAxis-Labels in meinem Diagramm gab.

Gerade Kommentar unterhalb der Linie:

horizontalBarChartView.xAxis.drawLabelsEnabled = false 
0

Wenn Sie erstellen Ihre BarChartDataSet können Sie das Etikett übergeben.

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

Hoffnung, das hilft.

+0

Nein Versuchen, finden Sie in meinem Update. Ich brauche diese Etiketten. –

1

ich eine Erweiterung verwende für dieses

extension BarChartView { 

    private class BarChartFormatter: NSObject, IAxisValueFormatter { 

     var labels: [String] = [] 

     func stringForValue(_ value: Double, axis: AxisBase?) -> String { 
      return labels[Int(value)] 
     } 

     init(labels: [String]) { 
      super.init() 
      self.labels = labels 
     } 
    } 

    func setBarChartData(xValues: [String], yValues: [Double], label: String) { 

     var dataEntries: [BarChartDataEntry] = [] 

     for i in 0..<yValues.count { 
      let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i]) 
      dataEntries.append(dataEntry) 
     } 

     let chartDataSet = BarChartDataSet(values: dataEntries, label: label) 
     let chartData = BarChartData(dataSet: chartDataSet) 

     let chartFormatter = BarChartFormatter(labels: xValues) 
     let xAxis = XAxis() 
     xAxis.valueFormatter = chartFormatter 
     self.xAxis.valueFormatter = xAxis.valueFormatter 

     self.data = chartData 
    } 
} 

Sie dies im Code wie dieser

chartView.setBarChartData(xValues: xEntries, yValues: yEntries, label: "Income") 

verwenden können, wo,

//You need to add values dynamically 
var yEntries: [Double] = [1000, 200, 500] 
var xEntries: [String] = ["Income1", "Income2", "Income3"] 

Ich hoffe, das funktioniert.

0
self.ChartObj.xAxis.valueFormatter = DefaultAxisValueFormatter(block: {(index, _) in 
      print(index) 
      return YourArray.object(at: Int(index)) as! String 
     }) 
self.ChartObj.xAxis.setLabelCount(YourArray.count, force: true) 

diesem Code

Verwandte Themen