2016-10-02 61 views
0

我使用迅速iOSCharts通过@danielgindihttps://github.com/danielgindi/Charts问题与iOS图表库

因此,这里是我的数据集

let dateArray = [26,27,28,29,30,01,02] 
     let values = [11.0,5.0,0.0,0.0,4.0,1.0,0.0] 

     var dataEntries: [BarChartDataEntry] = [] 

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

     let data = BarChartData() 
     let ds1 = BarChartDataSet(values: dataEntries, label: "Hello") 
     ds1.colors = [NSUIColor.red] 
     data.addDataSet(ds1) 




     self.barChartView.data = data 

     self.barChartView.fitBars = true 

     self.barChartView.gridBackgroundColor = NSUIColor.white 

     self.barChartView.chartDescription?.text = "" 

什么,我希望是这样的 enter image description here

但我得到的是这个

enter image description here

因此,大家可以看到的问题是低于

1)从图形上面一个格开始,你会看到在底部 2空间)x轴的标签是不同的,我不知道如何设置。 3)我想显示x轴和y轴标签为int而不是double。

让的DataEntry = BarChartDataEntry(X::双(i)中,Y:值[I])

x值是

回答

0

你的第二个问题,在X标签的问题,是在这条线引起被正好被设置为i(double类型),当我怀疑你的意思是它设置为Double(dateArray[i]),像这样:

让dateArray = [26,27,28,29,30,01,02] 让值= [11.0,5.0,0.0,0.0,4.0,1.0,0.0]

var dataEntries: [BarChartDataEntry] = [] 

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

此代码可以与使用的zipmap短路:

let dataEntries = zip(dateArray, values).map(BarChartDataEntry.init(x:y:)) 

此代码将的dateArray每日期配对与来自value的值。然后,通过传递给BarChartDataEntry.init(x:y:)初始值设定项来转换这些对。这种技术很好,因为它不需要dataEntries是可变的。

+0

嘿谢谢,你能帮我解决第一个问题。为什么有差距 – Ranjit

+0

barChartView.legend.enabled = false – Alex

+0

哎@亚历克斯,我试过,但它不工作 – Ranjit

1

退房在ChartsDemo项目BarchartViewController的主版本,它实现了您的两个问题:

1)左轴对齐,请参阅:

https://github.com/danielgindi/Charts/blob/master/ChartsDemo/Classes/Demos/BarChartViewController.m#L76

具体来说,要设置axisMinimum在左轴,例如它会是:

self.barChartView.leftAxis.axisMinimum = 0.0 

2)X轴值forma拟合。

这一个有点棘手。您可以借用ChartsDemo中的DayAxisValueFormatter,但这是Objective-C代码,所以您需要熟悉将Objective-C添加到项目中。

你也可以得到你问什么了通过使您的视图控制器IAxisValueFormatter和实施这样的:

class MyViewController: UIViewController, IAxisValueFormatter { 

    var dateArray: [Int] = [26,27,28,29,30,01,02] //member variable 

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

    //edit: forgot to say this earlier, you need to do this somewhere in your bar graph setup code 
    self.barChwrtView.xAxis.formatter = self 
} 

这是给你保持dateArray和值同步每当更新图表。

0

尝试leftAxis.axisMinValue = 0.0