Charts3.0 - 如果超过8个值,则无法显示所有xAxis标签 - Swift

时间:2016-12-06 04:06:48

标签: swift charts

我创建了一个条形图,其中包含xAxis的9个类别标签: [" Foods"," House Loan"," Insurance"," Petrol"," Saving",&#34 ; Train"," Others"," Books&杂志","停车场"]

但是在图表中它只显示: 食品,保险,储蓄,其他和停车场

任何人都知道如何解决它?

这里是设置xAxis的源代码

func setChart(dataPoints: [String], values: [Double]) {       
     var dataEntries: [BarChartDataEntry] = []

     for i in 0..<dataPoints.count {

         let value = values[i] as Double
         let dataEntry = BarChartDataEntry(x: Double(i), y:value)
         dataEntries.append(dataEntry)
     }

     let chartDataSet = BarChartDataSet(values: dataEntries, label: "Total Amount")

     var colors: [UIColor] = []
     for _ in 0..<dataPoints.count {
         let red = Double(arc4random_uniform(256))
         let green = Double(arc4random_uniform(256))
         let blue = Double(arc4random_uniform(256))

         let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
         colors.append(color)
     }

     chartDataSet.colors = colors
     chartDataSet.colors = ChartColorTemplates.joyful()
     barChartView.chartDescription?.text = ""

     barChartView.xAxis.labelPosition = .bottom
     barChartView.xAxis.drawGridLinesEnabled = false
     barChartView.xAxis.granularityEnabled = true
     barChartView.xAxis.granularity = 1.0
     barChartView.xAxis.decimals = 0

     let chartData = BarChartData()
     chartData.addDataSet(chartDataSet)
     barChartView.data = chartData
     barChartView.xAxis.valueFormatter = self

     let format = NumberFormatter()
     format.numberStyle = .decimal
     let formatter = DefaultValueFormatter(formatter: format)
     chartData.setValueFormatter(formatter)
     barChartView.animate(xAxisDuration: TimeInterval(1))
}

enter image description here

1 个答案:

答案 0 :(得分:1)

只需设置标签计数即可。 barChartView.xAxis.labelCount = dataPoints.count