Swift Charts xAxis标签对齐

时间:2017-08-29 20:49:58

标签: swift charts ios-charts

充分意识到这个问题已被多次询问,但我尝试了尽可能多的解决方案,但没有找到有意义的结果。这是日期与其数据分组不匹配的问题。这两个项目的数量相同:

enter image description here

正在根据数据库条目的年龄动态编译X日期值。代码如下:

var allDiaperEntries: [SubmittedEntry] = []
    var allDiaperDates: [Date] = []
    daysUnformatted = []
    days = []

    for diaper in realm.objects(SubmittedEntry.self).filter("diaperIsWet = true or diaperIsPoopy = true or diaperIsCombo = true") {
        allDiaperEntries.append(diaper)
    }

    for diaper in allDiaperEntries {
        allDiaperDates.append(diaper.submissionTime!)
    }

    allDiaperDates.sort(by: { $0 < $1 })


    let oldestDiaperEntry = allDiaperDates.first

    var dateToCheck = Date()

    let calendar = Calendar.current

    repeat {

        dateToCheck = calendar.date(byAdding: .day, value: -1, to: dateToCheck)!
        daysUnformatted.append(dateToCheck)

    } while !calendar.isDate(dateToCheck, inSameDayAs: oldestDiaperEntry!)

    daysUnformatted.insert(Date(), at: daysUnformatted.startIndex)


    let daysDateFormatter = DateFormatter()
    daysDateFormatter.timeZone = TimeZone.current
    for day in daysUnformatted {
        daysDateFormatter.dateFormat = "MMM d, yyyy"
        days.append(daysDateFormatter.string(from: day))
    }
    days.reverse()
}

然后我在图表中构建图表:

buildDiaperDays()

    barView.noDataText = "Charts will be available after submitting baby events."
    var dataEntriesWet: [BarChartDataEntry] = []
    var dataEntriesPoopy: [BarChartDataEntry] = []
    var dataEntriesCombo: [BarChartDataEntry] = []

    for i in 0..<days.count {
        let dataEntryWet = BarChartDataEntry(x: Double(i), y: diaperCountsWet[i], data: days[i] as AnyObject)
        dataEntriesWet.append(dataEntryWet)

        let dataEntryPoopy = BarChartDataEntry(x: Double(i), y: diaperCountsPoopy[i], data: days[i] as AnyObject)
        dataEntriesPoopy.append(dataEntryPoopy)

        let dataEntryCombo = BarChartDataEntry(x: Double(i), y: diaperCountsCombo[i], data: days[i] as AnyObject)
        dataEntriesCombo.append(dataEntryCombo)
    }

    let chartDataSetWet = BarChartDataSet(values: dataEntriesWet, label: "Wet Diapers")
    let chartDataSetPoopy = BarChartDataSet(values: dataEntriesPoopy, label: "Poopy Diapers")
    let chartDataSetCombo = BarChartDataSet(values: dataEntriesCombo, label: "Combo Diapers")

    let dataSets: [BarChartDataSet] = [chartDataSetWet, chartDataSetPoopy, chartDataSetCombo]

    let chartData = BarChartData(dataSets: dataSets)

    let groupSpace = 0.3
    let barSpace = 0.05
    let barWidth = 0.3

    let groupCount = self.days.count
    let startYear = 0

    barView.xAxis.valueFormatter = IndexAxisValueFormatter(values:days)
    barView.xAxis.granularity = 1
    barView.xAxis.labelCount = days.count
    barView.xAxis.labelRotationAngle = 270

    chartData.barWidth = barWidth;
    barView.xAxis.axisMinimum = Double(startYear)
    let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
    barView.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)

    chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)

    barView.notifyDataSetChanged()

    barView.data = chartData

    barView.backgroundColor = UIColor(red: 189/255, green: 195/255, blue: 199/255, alpha: 0)

    barView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .easeInSine)

感谢社区的帮助。

1 个答案:

答案 0 :(得分:1)

试试这个:

barView.xAxis.labelPosition = .bottom
barView.xAxis.granularityEnabled = true

需要更改以下值:

let groupSpace = 0.1
let barSpace = 0.05
let barWidth = 0.25
// (barWidth + barSpace) * (no.of.bars) + groupSpace = 1.00 -> interval per "group"