iOS图表使Stacked BarChart具有2个数组

时间:2017-11-26 03:54:15

标签: ios swift ios-charts stacked-chart

好吧,我正在尝试制作Stacked Bar Charts。 (Swift,来自cocoapod的iOS-Charts。)在这些条件下......

  1. 我有1个字符串数组和2个双数组:

    1)monthArray = {"1","2","3".....,"12"} //for xAxis Label

    2)finalBorrowArray = {0,0,500.0,0,200.0...etc}

    3)finalLendArray = {0,0,500.0,800.0,...etc}

  2. 以下是一些Swift代码:

    func setChartData(count:Int, range:UINT32) {
    
          var dataEntries: [ChartDataEntry] = []
          var dataEntries2: [ChartDataEntry] = []
    
          for i in 0..<finalBorrowArray.count {
    
              let dataEntry = BarChartDataEntry(x: Double(i), yValues: 
              [finalBorrowArray[i]])
              dataEntries.append(dataEntry)
          }
    
          for i in 0..<finalLendArray.count {
    
              let dataEntry = BarChartDataEntry(x: Double(i), yValues: 
              [finalLendArray[i]])
             dataEntries2.append(dataEntry)
          }
    
          let yVals = (0..<count).map { (i) -> BarChartDataEntry in
    
              @@@@@@@@@@@@@@@PROBLEM@@@@@@@@@HELP@@@@@@@@@@@@@
              return ?
          }
    
          }
    
    let set = BarChartDataSet(values:yVals, label: "")
    set.drawIconsEnabled = false
    set.colors = [ChartColorTemplates.pastel()[0], ChartColorTemplates.pastel()[3]]
    
    set.stackLabels = ["(+)", "(-)"]
    
    let data = BarChartData(dataSet: set)
    data.setValueFont(.systemFont(ofSize: 10, weight: 20))
    data.setValueFormatter(DefaultValueFormatter(formatter: formatter))
    data.setValueTextColor(.black)
    
    barChart.fitBars = true
    barChart.data = data
    
  3. 所以我认为我必须yValBarChartDataEntry 让yVals。 (也许..?)

    但我不知道如何获得2 yAxis ...任何帮助或想法的椭圆?

    提前致谢..!

1 个答案:

答案 0 :(得分:0)

我自己解决了..

我使用另外BarChartDataEntry组合了2个数组,而不是yVals

以下是代码:

var finalEntries : [BarChartDataEntry] = []

    for i in 0..<finalLendArray.count {

        let Entry = BarChartDataEntry(x: Double(i), yValues: [finalBorrowArray[i], finalLendArray[i]])
        finalEntries.append(Entry)
    }

    let set = BarChartDataSet(values:finalEntries, label: "")