IOS图表条形图不定结果

时间:2018-05-25 14:37:44

标签: ios swift uitableview bar-chart ios-charts

我正在使用danielgindi / Charts库。在我的主视图控制器中,我有3个原型单元格的表视图。其中一个是条形图,第二个是折线图,第三个是饼图。还有3个dequeue可重用单元格方法和图表配置方法在其中。当我第一次执行应用程序时,我的所有条形图都没有任何问题。但是,当我添加新图表或滚动时(我不确定,但我认为其中一个导致奇怪的问题)条形图视图看起来很奇怪,如图所示。Weird one

Normal one

当我第一次执行程序时,它显示为第二个,但在一些滚动或添加新图表后,它首先看起来。每个数据集都覆盖x轴。以下是我的configureBarChart方法。

func configureBarChart(xTitle: IndexAxisValueFormatter, index: Int, chartData: BarChartData, chartType: String) {


barChartView.data?.setDrawValues(false)
barChartView.doubleTapToZoomEnabled = false
barChartView.chartDescription?.enabled = false
barChartView.animate(xAxisDuration: 1)
barChartView.xAxis.drawGridLinesEnabled = false        

barChartView.xAxis.valueFormatter = xTitle
barChartView.xAxis.labelCount = index


barChartView.rightAxis.enabled = false

barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.centerAxisLabelsEnabled = true
barChartView.xAxis.granularity = 1
let leftAxisFormatter = NumberFormatter()
leftAxisFormatter.maximumFractionDigits = 1

barChartView.leftAxis.spaceTop = 0.35
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.drawGridLinesEnabled = false


barChartView.rightAxis.enabled = false

var groupSpace = 0.0
var barSpace = 0.0
var barWidth = 0.0

if (chartData.dataSetCount == 1) {
groupSpace = 0.3
barSpace = 0.0
barWidth = 0.7
} else if (chartData.dataSetCount == 2) {
groupSpace = 0.3
barSpace = 0.05
barWidth = 0.3
} else if (chartData.dataSetCount == 3) {
groupSpace = 0.25
barSpace = 0.05
barWidth = 0.2
} else if (chartData.dataSetCount == 4) {
groupSpace = 0.2
barSpace = 0.05
barWidth = 0.15
}

let groupCount = index
let startYear = 0

chartData.barWidth = barWidth;
barChartView.xAxis.axisMinimum = Double(startYear)
let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)

if (chartType == "clustered") {
barChartView.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)
chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
} else {
barChartView.xAxis.axisMaximum = Double(startYear) + 1 * Double(groupCount)
}


barChartView.setScaleEnabled(false)

if xTitle.values.count >= 10 {
barChartView.setVisibleXRangeMaximum(12)
} else {
barChartView.setVisibleXRangeMaximum(Double(xTitle.values.count))   // When too many data comes I limit it so data's are not covering each other.
}
}

我也使用条形图来分组条形图,这就是为什么有一些barSpace和barWidth代码。改变图表行为的原因是什么?我该如何解决?

编辑:这是我的cellForRowAt方法并获取条形图数据

if MainGrafikVC.barChartDataArray[indexPath.row].dataSetCount != 0 {
                    cell.barChartView.data        = MainGrafikVC.barChartDataArray[indexPath.row]
                    cell.configureBarChart(xTitle: MainGrafikVC.xTitleArray[indexPath.row], index: MainGrafikVC.barChartDataArray[indexPath.row].getDataSetByIndex(0).entryCount, chartData: MainGrafikVC.barChartDataArray[indexPath.row], chartType: Servisler.servisListesi[MainGrafikVC.indexler[indexPath.row]].chartType)
            }

if x10.count > 0 {
var barChartEntry10 = [BarChartDataEntry]()
for i in 0..<x10.count {
let value = BarChartDataEntry(x: Double(i), y: Double(x10[i]) ?? 0.0)
barChartEntry10.append(value)
}
let bar10 = BarChartDataSet(values: barChartEntry10, label: "10")
bar10.colors = ChartColorTemplates.vordiplom()
                dataSets.append(bar10)
            }
let data = BarChartData(dataSets: dataSets)
MainGrafikVC.barChartDataArray.append(data)

我认为问题 setVisibleXRangeMaximum 有关,因为当我评论它时,一切看起来都一样。但是,我需要设置可见的x轴可以替代它吗?

0 个答案:

没有答案