【问题标题】:Barchart with grouped values: center-align x-axis labels具有分组值的条形图:中心对齐 x 轴标签
【发布时间】:2020-03-18 21:29:19
【问题描述】:

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

这是我设法构建的:

它正在接近,但我在显示条形图时遇到了严重问题。例如,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】:

    好的,找到答案了?

    xAxis.axisMinimum = 0
    xAxis.axisMaximum = 7
    xAxis.centerAxisLabelsEnabled = true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多