本文介绍了iOS Charts X Axis标签未全部显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序管理一项具有四个季度周期的运动的得分。我的条形图将无法正确显示x轴标签。它仅显示Qtr 1和Qtr 3,而应显示每个条形对下的所有四个季度。

My app manages the score of a sport that has 4 quarter periods. My bar chart will not display the x-axis labels correctly. It only shows Qtr 1 and Qtr 3, whereas it should show all 4 quarters under each respective pair of bars.Screen shot of Bar Chart

    func setChart(dataPoints: [String], values1: [Double], values2: [Double]) {

    let legend = barChartView.legend
    legend.enabled = true
    legend.textColor = Style.labelTextColor

    let xaxis = barChartView.xAxis
    xaxis.drawGridLinesEnabled = true
    xaxis.labelPosition = .bottom
    xaxis.axisMinimum = 0
    xaxis.centerAxisLabelsEnabled = true
    xaxis.granularityEnabled = true
    xaxis.labelCount = 4
    xaxis.granularity = 2
    xaxis.labelTextColor = Style.labelTextColor

    let leftAxisFormatter = NumberFormatter()
    leftAxisFormatter.maximumFractionDigits = 1

    let yaxis = barChartView.leftAxis
    yaxis.spaceTop = 0.35
    yaxis.axisMinimum = 0
    yaxis.drawGridLinesEnabled = true
    yaxis.labelTextColor = Style.labelTextColor
    yaxis.gridColor = Style.labelTextColor
    yaxis.zeroLineColor = Style.labelTextColor
    yaxis.axisLineColor = Style.labelTextColor

    barChartView.rightAxis.enabled = false

    barChartView.noDataText = "You need to provide data for the chart."
    barChartView.chartDescription?.text = nil

    var dataEntries1: [BarChartDataEntry] = []; var dataEntries2: [BarChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry1 = BarChartDataEntry(x: Double(i), y: values1[i])
        dataEntries1.append(dataEntry1)

        let dataEntry2 = BarChartDataEntry(x: Double(i), y: values2[i])
        dataEntries2.append(dataEntry2)
    }

    let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: gCurrentMatch.teamA)
    let chartDataSet2 = BarChartDataSet(values: dataEntries2, label: gCurrentMatch.teamB)
    chartDataSet1.colors = [Style.chartColorA]; chartDataSet1.valueColors = [Style.labelTextColor]
    chartDataSet2.colors = [Style.chartColorB]; chartDataSet2.valueColors = [Style.labelTextColor]

    let dataSets: [BarChartDataSet] = [chartDataSet1, chartDataSet2]
    let chartData = BarChartData(dataSets: dataSets)

    let groupSpace = 0.3
    let barSpace = 0.05
    let barWidth = 1.0
    // (0.3 + 0.05) * 2 + 0.3 = 1.00 -> interval per "group"

    let groupCount = self.qtr.count

    chartData.barWidth = barWidth
    let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)

    barChartView.xAxis.axisMaximum = Double(0) + gg * Double(groupCount)
    barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)

    chartData.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace)
    barChartView.notifyDataSetChanged()
    barChartView.data = chartData
    //chart animation
    barChartView.animate(yAxisDuration: 0.5, easingOption: .linear)

}

dataPoints是一个包含 Qtr 1, Qtr 2等的字符串数组。

dataPoints is a string array containing "Qtr 1", "Qtr 2" etc...

我不知道为什么它不起作用。

I cannot work out why it is not working.

推荐答案

在xaxis格式中使用以下代码:

use the following code in your xaxis formatting :

xaxis.setLabelCount(4, force: true)

这篇关于iOS Charts X Axis标签未全部显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 14:01