iOS图表-X轴上的条形图重复标签

时间:2019-06-20 09:01:06

标签: ios swift charts

我制作了一个分为6组的条形图,并且设法设法将它们精确地定位在每个组的下方。现在,问题在于每个标签内部的值都在重复。 我的值数组包含6个不同的值,我的标签计数为7。labelCount必须为7,才能使图表正确居中。而且,我也确实将此值设置为6,以为当值格式化程序选择值并且标签仍在重复时,存在索引问题。

我已经尝试将粒度更改为各种值,并且它无法正常工作。我还摆弄了将粒度切换为true或false的方法,但是它也不起作用。

Click here for screen shot

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]

lazy var barChart: BarChartView = {
    let chart = BarChartView()
    chart.translatesAutoresizingMaskIntoConstraints = false
    var inComing = [BarChartDataEntry]()
    var outgoing = [BarChartDataEntry]()
    for i in 0..<6 {
        let entry = BarChartDataEntry(x: Double(i), y: Double.random(in: 10..<100))
        inComing.append(entry)
    }
    for i in 6..<12 {
        let entry = BarChartDataEntry(x: Double(i), y: Double.random(in: 10..<100))
        outgoing.append(entry)
    }
    let set1 = BarChartDataSet(entries: inComing, label: "Incoming")
    let set2 = BarChartDataSet(entries: outgoing, label: "Outgoing")
    let incomingColor = UIColor(red: 51 / 255, green: 51 / 255, blue: 51 / 255, alpha: 1)
    set1.colors = [incomingColor]
    let outgoingColor = UIColor(red: 153 / 255, green: 0, blue: 0, alpha: 1)
    set2.colors = [outgoingColor]
    let data = BarChartData(dataSets: [set1, set2])
    let barWidth = 0.004
    let barSpace = 0.007
    let groupSpace = 0.032
    let groupCount = 6
    data.barWidth = barWidth
    let gg = data.groupWidth(groupSpace: groupSpace, barSpace: barSpace)

    chart.data = data
    chart.xAxis.axisMinimum = 0
    chart.xAxis.axisMaximum = gg * Double(groupCount)

    set1.drawValuesEnabled = false
    set2.drawValuesEnabled = false
    chart.xAxis.drawGridLinesEnabled = false
    chart.xAxis.drawAxisLineEnabled = true
    chart.drawBordersEnabled = true

    let formater = IndexAxisValueFormatter.with(values: months)
    chart.xAxis.centerAxisLabelsEnabled = true
    chart.xAxis.setLabelCount(7, force: true)

    chart.xAxis.granularityEnabled = true
    chart.xAxis.axisMaxLabels = 6
    chart.xAxis.axisMinLabels  = 0
    chart.xAxis.labelPosition = .bottom
    chart.xAxis.valueFormatter = formater
    chart.xAxis.drawLimitLinesBehindDataEnabled = false
    chart.leftAxis.drawGridLinesEnabled = false
    chart.leftAxis.drawAxisLineEnabled = false
    chart.rightAxis.drawGridLinesEnabled = false
    chart.rightAxis.drawAxisLineEnabled = false
    chart.rightAxis.drawLabelsEnabled = false

    data.groupBars(fromX: 0, groupSpace: groupSpace, barSpace: barSpace)
    chart.drawGridBackgroundEnabled = false
    chart.legend.enabled = false
    chart.notifyDataSetChanged()
    return chart
}()

0 个答案:

没有答案