Barchart具有分组值:x轴中心对齐标签

问题描述 投票:0回答:1

我正在使用Charts库来生成条形图。这是我得到的设计,正在尝试重新创建:

enter image description here

这是我设法建立的:

enter image description here

距离越来越近,但显示栏时出现严重问题。例如,x轴标签需要显示在组的中心,而不是垂直线的正下方。由于某种原因,最后的灰色条根本没有显示。

代码:

import Charts
import UIKit

class DayAxisValueFormatter: NSObject, IAxisValueFormatter {
  public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    let day = Int(value)
    return ["Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu"][day]
  }
}

class ReportBarChart: UITableViewCell {
  @IBOutlet private var chartView: BarChartView!

  let groupSpace = 0.06
  let barSpace = 0.02
  let barWidth = 0.45

  override func awakeFromNib() {
    super.awakeFromNib()

    chartView.backgroundColor = .reportCard
    chartView.drawBarShadowEnabled = false
    chartView.drawValueAboveBarEnabled = false
    chartView.dragEnabled = false
    chartView.setScaleEnabled(false)
    chartView.pinchZoomEnabled = false

    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    xAxis.labelFont = .systemFont(ofSize: 11, weight: .semibold)
    xAxis.labelTextColor = .reportGrayText
    xAxis.granularity = 1
    xAxis.labelCount = 7
    xAxis.valueFormatter = DayAxisValueFormatter()

    let leftAxis = chartView.leftAxis
    leftAxis.enabled = false

    let rightAxis = chartView.rightAxis
    rightAxis.enabled = true
    rightAxis.labelFont = .systemFont(ofSize: 11, weight: .semibold)
    rightAxis.labelTextColor = .reportGrayText
    rightAxis.axisMinimum = 0
    rightAxis.labelCount = 3

    let legend = chartView.legend
    legend.font = .systemFont(ofSize: 11, weight: .semibold)
    legend.textColor = .white
    legend.form = .circle
    legend.xEntrySpace = 16
  }

  func configure() {
    let currentValues = [
      BarChartDataEntry(x: 0, y: 1),
      BarChartDataEntry(x: 1, y: 2),
      BarChartDataEntry(x: 2, y: 3),
      BarChartDataEntry(x: 3, y: 4),
      BarChartDataEntry(x: 4, y: 4),
      BarChartDataEntry(x: 5, y: 1),
      BarChartDataEntry(x: 6, y: 6),
    ]

    let currentValuesSet = BarChartDataSet(entries: currentValues, label: "This week")
    currentValuesSet.setColor(UIColor.reportOrange)
    currentValuesSet.drawValuesEnabled = false

    let previousValues = [
      BarChartDataEntry(x: 0, y: 4),
      BarChartDataEntry(x: 1, y: 3),
      BarChartDataEntry(x: 2, y: 2),
      BarChartDataEntry(x: 3, y: 1),
      BarChartDataEntry(x: 4, y: 3),
      BarChartDataEntry(x: 5, y: 2),
      BarChartDataEntry(x: 6, y: 5),
    ]

    let previousValuesSet = BarChartDataSet(entries: previousValues, label: "Last week")
    previousValuesSet.setColor(UIColor.reportGrayChart)
    previousValuesSet.drawValuesEnabled = false

    let data = BarChartData(dataSets: [currentValuesSet, previousValuesSet])
    data.highlightEnabled = false

    data.barWidth = barWidth
    data.groupBars(fromX: 0, groupSpace: groupSpace, barSpace: barSpace)

    chartView.data = data
  }
}
ios swift ios-charts
1个回答
0
投票

好的,找到答案😅

xAxis.axisMinimum = 0
xAxis.axisMaximum = 7
xAxis.centerAxisLabelsEnabled = true
© www.soinside.com 2019 - 2024. All rights reserved.