每个栏的ios图表标签

时间:2017-01-29 09:02:48

标签: ios swift ios-charts

我使用ios图表来显示条形图。看起来像这样:

enter image description here

我想要的是红色框中的标签(对于所有的条形图,我只是不想经常重复这个......:P)。这意味着每个酒吧都有自己的标题"标签,以便用户看到每个值的类别名称。

另一个问题是:我添加的条形越多,条形越小。我可以以某种方式将BarChartView放入滚动视图,将条形的宽度设置为静态值,例如,还有20个值,用户只是向右滚动以查看更多值吗?

编辑代码:

  func entries(for chart:ChartType, content:ChartContent, max:Int = Int.max)->[ChartDataEntry]{
    let transactions = manager.transactions.value
    let grouped = content == .category ? transactions.categorise({$1.category}) : transactions.categorise({$1.payee})
    var entries:[ChartDataEntry] = []
    let formatter = NumberFormatter()
    formatter.locale = Locale.current
    formatter.numberStyle = .currency

    for i in 0..<grouped.count {
      let c = Array(grouped.keys)[i]
      let ts = grouped[c]!
      var sum = 0.0
      ts.forEach{k,t in
        if t.expense{
           sum +=  Double(t.value)
        }
      }
      if chart == .pie{
        let entry =  PieChartDataEntry(value: sum, label: "\(c)")
        if entries.count < max{
          entries.append(entry)
        }else{
          entries.sort(by: {$0.0.y > $0.1.y})
          break
        }

      }else if chart == .bar{
        let entry =  BarChartDataEntry(x: Double(i), y: sum, data: c as AnyObject?)
        if entries.count < max{
          entries.append(entry)
        }else{
          entries.sort(by: {$0.0.y > $0.1.y})
          break
        }
      }

    }


    return entries
  }

  func showBarChart(content: ChartContent){
    // Do any additional setup after loading the view.

    let data = BarChartData()
    let content = entries(for: .bar, content: content)
    let ds1 = BarChartDataSet(values: content, label: "")

    let xAxis=XAxis()
    let chartFormmater = ChartFormatter()
    chartFormmater.content = content as! [BarChartDataEntry]

    for i in 0..<content.count{

      chartFormmater.stringForValue(Double(i), axis: xAxis)
    }

    xAxis.valueFormatter=chartFormmater
    barChartView.xAxis.valueFormatter=xAxis.valueFormatter
    barChartView.xAxis.labelPosition = .bottom


    ds1.colors = ChartColorTemplates.material()

    data.addDataSet(ds1)

    self.barChartView.data = data

    self.barChartView.gridBackgroundColor = NSUIColor.white



    self.barChartView.animate(xAxisDuration: 0.0, yAxisDuration: 1.0)
  }
@objc(BarChartFormatter)
class ChartFormatter:NSObject,IAxisValueFormatter{

  var content:[BarChartDataEntry]!

  func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    return content[Int(value)].data as! String
  }

}

新截图: enter image description here

4 个答案:

答案 0 :(得分:7)

你可以在下面的代码中实现,以解决这两个问题。

在下面的函数中,我传递了两个参数,即 dataPoints: x-Axis Labels,以及值:栏的值。它还解决了你的滚动问题。

func setChart(dataPoints: [String], values: [Float])
{
    barChartView.noDataText = "You need to provide data for the chart."

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

    let chartDataSet:BarChartDataSet!

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "BarChart Data")
    let chartData = BarChartData(dataSet: chartDataSet)
    barChartView.data = chartData
    barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
    barChartView.xAxis.granularityEnabled = true
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.xAxis.labelPosition = .bottom
    barChartView.xAxis.labelCount = 30
    barChartView.xAxis.granularity = 2
    barChartView.leftAxis.enabled = true
    //barChartView.leftAxis.labelPosition = .outsideChart
    //barChartView.leftAxis.decimals = 0
    let minn = Double(values.min()!) - 0.1
    barChartView.leftAxis.axisMinimum = Double(values.min()! - 0.1)
    //barChartView.leftAxis.granularityEnabled = true
    //barChartView.leftAxis.granularity = 1.0
    //barChartView.leftAxis.labelCount = 5
    barChartView.leftAxis.axisMaximum = Double(values.max()! + 0.05)
    barChartView.data?.setDrawValues(false)
    barChartView.pinchZoomEnabled = true
    barChartView.scaleYEnabled = true
    barChartView.scaleXEnabled = true
    barChartView.highlighter = nil
    barChartView.doubleTapToZoomEnabled = true
    barChartView.chartDescription?.text = ""
    barChartView.rightAxis.enabled = false


    barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuart)
    chartDataSet.colors = [UIColor(red: 0/255, green: 76/255, blue: 153/255, alpha: 1)]

}

OR

如何设置x轴标签?

从Swift 3.0.1开始,我们也可以这样做:

let labels = ["Value 1", "Value 2", "Value 3"]

barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:labels)
//Also, you probably we want to add:

barChartView.xAxis.granularity = 1

希望它对你有所帮助。

答案 1 :(得分:2)

您可以在自己的课程中实施IAxisValueFormatter

为此你只需要实现这个方法:

func stringForValue(_ value: Double, axis: AxisBase?) -> String

示例:

或您关注RayWenderlich Tutorial "Using Realm and Charts with Swift 3 in iOS 10"

您必须设置一些代理人,但这是自定义格式化程序的关键代码段:

// MARK: axisFormatDelegate
extension ViewController: IAxisValueFormatter {

  func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = “HH:mm.ss”
    return dateFormatter.string(from: Date(timeIntervalSince1970: value))
  }
}

答案 2 :(得分:1)

要补充之前的答案,如果您想要显示所有值并且您有超过25个值,请记住在AxisBase.swift中修改LabelCount的硬编码限制,如下所示:

    open var labelCount: Int
    {
        get
        {
            return _labelCount
        }
        set
        {
            _labelCount = newValue
            // WHY?
            // This limits the Amount of Labels We can Display: (Removing) -> Comment OUT
//            
//            if _labelCount > 25
//            {
//                _labelCount = 25
//            }
//            if _labelCount < 2
//            {
//                _labelCount = 2
//            }
//            
            forceLabelsEnabled = false
        }
    }

我希望这有助于: - )

答案 3 :(得分:0)

回答你的第一个和第二个问题。我建议你做的是有一个侧滚动集合视图。在每个集合视图单元格中,您可以包含标题栏和值。这可以帮助你所有人重复尽可能多的图形,就像你要求你想要一个滚动视图。