【问题标题】:X axis values are getting overlapped - iOS ChartsX 轴值重叠 - iOS 图表
【发布时间】:2019-09-12 06:14:28
【问题描述】:

这是图表视图的截图。

我正在使用 ios https://github.com/danielgindi/Charts

x 轴上的值(日期)正在重叠。

 axisMinValue = 50.0
 axixMaxvalue = 300.0

unitsSold = [65.0,88.0, 114.0, 111.0, 88.0 ]
months = ["Sep 12, 2019”,"Sep 12, 2019”,"Sep 12, 2019”,"Sep 11, 2019”,"Sep 11, 2019"]

这是我写的代码,

    if unitsSold.count < 1{

         cell.chartViewOutlet.noDataText = "No data available!"
    }
    else{

        dataEntries = []
        for i in 0 ..< unitsSold.count {
            let dataEntry = ChartDataEntry(x: Double(i), y: unitsSold[i]) 
            dataEntries.append(dataEntry) // here we add it to the data set
        }

        let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Consumed")

        lineChartDataSet.setCircleColor(UIColor.clear) // hide the outer circle color
        lineChartDataSet.circleRadius = 0.0
        lineChartDataSet.lineWidth = 2.0 //1.0
        lineChartDataSet.valueTextColor = UIColor.clear //hide the values on the curve line


        lineChartDataSet.colors = [NSUIColor.blue] //Sets the line colour to blue
        lineChartDataSet.mode = .cubicBezier
        lineChartDataSet.cubicIntensity = 0.2

        var dataSets = [LineChartDataSet]() 
        dataSets.append(lineChartDataSet)  

        let lineChartData = LineChartData(dataSets: dataSets)
        cell.chartViewOutlet.data = lineChartData 

        cell.chartViewOutlet.chartDescription?.text = "" 

        cell.chartViewOutlet.xAxis.enabled = true 
        cell.chartViewOutlet.leftAxis.enabled = true 
        cell.chartViewOutlet.rightAxis.enabled = false 
        cell.chartViewOutlet.animate(xAxisDuration: 1.5) 
        cell.chartViewOutlet.drawGridBackgroundEnabled = true 




        cell.chartViewOutlet.xAxis.drawGridLinesEnabled = true 
        cell.chartViewOutlet.xAxis.drawAxisLineEnabled = true 

        cell.chartViewOutlet.xAxis.labelPosition = .bottom 
        cell.chartViewOutlet.xAxis.drawLabelsEnabled = true 


        cell.chartViewOutlet.leftAxis.drawGridLinesEnabled = true 

        cell.chartViewOutlet.legend.enabled = false //show.hide legend - below graph


        let customFormater = CustomFormatter1()
        customFormater.labels =  months
        cell.chartViewOutlet.xAxis.valueFormatter = customFormater

// 在 x 轴上显示值

final class CustomFormatter1: IAxisValueFormatter {

var labels: [String] = []

func stringForValue(_ value: Double, axis: AxisBase?) -> String {

    let count = self.labels.count

    guard let axis = axis, count > 0 else {
        return ""
    }

    let factor = axis.axisMaximum / Double(count)

    let index = Int((value / factor).rounded())

    if index >= 0 && index < count {
        return self.labels[index]
    }

    return ""
}

我们如何解决这个重叠的问题?或者有什么办法可以根据屏幕或月份长度自动调整此值?

【问题讨论】:

  • 一种方法是将xAxis标签旋转为cell.chartViewOutlet.xAxis.labelRotationAngle = -45

标签: ios swift charts


【解决方案1】:

尝试使用以下并尝试,它会 100 % 工作

cell.chartViewOutlet.xAxis.labelRotationAngle = -45

【讨论】:

  • 真的!!!这不是解决方案...
猜你喜欢
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多