2017-05-25 42 views
1

如何從iOS圖表中刪除y值中的小數點?使用 林最新的iOS排行榜發佈與Swift3iOS圖表刪除yValues中的小數點

enter image description here

+0

你正在使用哪個庫? –

+1

您是否嘗試過下面的代碼來顯示您的數據集值? – CodeChanger

+0

即時通訊使用iOS的圖表https://github.com/danielgindi/Charts – johnny

回答

2

感謝每一個誰試圖幫助,這裏是此修復程序,添加以下代碼

let formatter = NumberFormatter() 
    formatter.numberStyle = .none 
    formatter.maximumFractionDigits = 0 
    formatter.multiplier = 1.0 
    chartData.setValueFormatter(DefaultValueFormatter(formatter: formatter)) 

到setBarChartData FUNC

func setBarChartData(xValues: [String], yValues: [Double], label: String) { 

    var dataEntries: [BarChartDataEntry] = [] 

    for i in 0..<yValues.count { 
     let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i]) 
     dataEntries.append(dataEntry) 
    } 

    let chartDataSet = BarChartDataSet(values: dataEntries, label: label) 
    let chartData = BarChartData(dataSet: chartDataSet) 


    let formatter = NumberFormatter() 
    formatter.numberStyle = .none 
    formatter.maximumFractionDigits = 0 
    formatter.multiplier = 1.0 
    chartData.setValueFormatter(DefaultValueFormatter(formatter: formatter)) 

    let chartFormatter = BarChartFormatter(labels: xValues) 
    let xAxis = XAxis() 
    xAxis.valueFormatter = chartFormatter 
    self.xAxis.valueFormatter = xAxis.valueFormatter 

    self.data = chartData 
} 
2

您需要設置值格式代表在DataSet像下面

的OBJ-C:

//DataSet 1 
LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithValues:values label:@"outstanding"]; 
set1.valueFormatter = self; 

使用下面的代表方法來格式化您的值:

#pragma mark - IChartValueFormatter 

- (NSString * _Nonnull)stringForValue:(double)value entry:(ChartDataEntry * _Nonnull)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler * _Nullable)viewPortHandler{ 

     //Format your value what you want here 
     return [NSString stringWithFormat:@"%0.f",value]; 
} 

確認協議:

@interface YourViewController()<ChartViewDelegate,IChartValueFormatter> 

對於斯威夫特,你需要創建BARCHART的擴展,並在其中使用下面的方法

斯威夫特:

extension BarChartView { 

    private class BarChartFormatter: NSObject, IAxisValueFormatter { 

     var labels: [String] = [] 

     func stringForValue(_ value: Double, axis: AxisBase?) -> String { 
      return labels[Int(value)] 
     } 

     init(labels: [String]) { 
      super.init() 
      self.labels = labels 
     } 
    } 

    func setBarChartData(xValues: [String], yValues: [Double], label: String) { 

     var dataEntries: [BarChartDataEntry] = [] 

     for i in 0..<yValues.count { 
      let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i]) 
      dataEntries.append(dataEntry) 
     } 

     let chartDataSet = BarChartDataSet(values: dataEntries, label: label) 
     let chartData = BarChartData(dataSet: chartDataSet) 

     let chartFormatter = BarChartFormatter(labels: xValues) 
     let xAxis = XAxis() 
     xAxis.valueFormatter = chartFormatter 
     self.xAxis.valueFormatter = xAxis.valueFormatter 

     self.data = chartData 
    } 
} 

致電上述擴展方法是這樣的:

func setChart(){ 
     let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
     let unitsSold = [20.0, 4.0, 3.0, 6.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0] 

     barChartView.setBarChartData(xValues: months, yValues: unitsSold, label: "Monthly Sales") 
    } 

希望你能得到線圖上的格式化值。

+0

謝謝,代碼是有道理的,我使用swift和im使用數據集的擴展,所以我試圖讓它與swift一起工作!會讓你更新。 – johnny

+0

多數民衆贊成在很好,我也更新了我的代碼與ObjC和Swift其他用戶參考。 – CodeChanger

+0

謝謝!這正是我正在做的,但即時獲取yValues的小數 – johnny

3

還可以使用:

let format = NumberFormatter() 
format.numberStyle = .none 
let formatter = DefaultValueFormatter(formatter: format) 
data.setValueFormatter(formatter)