【问题标题】:iOS-Charts Float to Integer YAxisiOS-Charts 浮动到整数 YAxis
【发布时间】:2015-07-09 13:24:29
【问题描述】:

我正在使用 iOS 图表构建图表

我正在尝试将浮点数转换为 int,但 iOS 图表仅允许在数据条目中使用浮点数:

      let result = ChartDataEntry(value: Float(month), xIndex: i)

有人知道确保只使用整数的方法吗?

【问题讨论】:

    标签: ios swift ios-charts


    【解决方案1】:

    对于 Swift 3.0 和 Charts 3.0,您需要启用并设置轴的粒度。

    例子:

    barChart.leftAxis.granularityEnabled = true
    barChart.leftAxis.granularity = 1.0
    

    【讨论】:

      【解决方案2】:

      你只需要调整NSNumberFormatter...

      例子:

      yAxis.valueFormatter = NSNumberFormatter()
      yAxis.valueFormatter.minimumFractionDigits = 0
      

      NSNumberFormatter 是一个非常强大的类,你可以用它做更多的事情。 :-)

      【讨论】:

      • 感谢您的帮助。我试过这个,但遗憾的是它没有用。也找不到关于回购的任何信息:(
      • @HannahLouisaCarney 它确实有效。你试过什么?也许您设置了错误的 valueFormatter property?不同的组件具有单独的属性
      • 你是对的,对不起!我添加了: self.lineChartholder.leftAxis.valueFormatter = NSNumberFormatter() self.lineChartholder.leftAxis.valueFormatter?.minimumFractionDigits = 0
      • 这不太行。如果轴范围太小,例如5-8,有7条网格线,然后在轴上显示5、5、6、6、6、7、7。
      【解决方案3】:

      这对我有用。但是,当显示小数字时,y 轴标签会四舍五入,所以如果您正在绘制图表,比如 1 或 2,图表将显示 0 两次。 (见这里:Force BarChart Y axis labels to be integers?

      let numberFormatter = NSNumberFormatter()
      numberFormatter.generatesDecimalNumbers = false
      chartDataSet.valueFormatter = numberFormatter
      // Converting to Int for small numbers rounds weird
      //barChartView.rightAxis.valueFormatter = numberFormatter
      //barChartView.leftAxis.valueFormatter = numberFormatter
      

      【讨论】:

        【解决方案4】:

        你可以试试这个,它对我很有效

        let formatter = NSNumberFormatter()
        formatter.numberStyle = .NoStyle
        formatter.locale = NSLocale(localeIdentifier: "es_CL")
        BarChartView.leftAxis.valueFormatter = formatter
        

        【讨论】:

          【解决方案5】:

          Swift4.2 ; Xcode 10.0:

          let numberFormatter = NumberFormatter()
          numberFormatter.generatesDecimalNumbers = false
          barChart.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: numberFormatter)
          

          【讨论】:

            猜你喜欢
            • 2016-12-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-26
            • 2016-11-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多