2017-05-09 47 views
1

我在這裏發現了一個類似的問題,但答案並沒有幫助我。我認爲由於我的數據結構。iOS圖表3 - 用陰謀對齊X標籤(日期)

我有一個由各個數組組成的數組,每個數組都有自己的圖表。然後這個多行結構構成該行的繪圖點。

我的問題是值/行是正確的,但沒有正確與日期對齊。在下面的例子中。日期從5月3日開始,5月8日結束。請幫助

這裏是我的代碼

struct chartPoint { 
let date:String 
var total:Double 
let exercise:String 
} 
var sets:[[chartPoint]] = [] 

func setupLineChart() { 
    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "yyyy-MM-dd" 
    var dataSets:[LineChartDataSet] = [] 
    var color:[UIColor] = [UIColor.red,UIColor.blue, UIColor.green,UIColor.red,UIColor.red,UIColor.red,UIColor.red] 
    for i in sets { //Array of array of structs 
     let sort = i.sorted { // sort the internal array by date 
      item1, item2 in 
      let date1 = dateFormatter.date(from:item1.date) 
      let date2 = dateFormatter.date(from:item2.date) 
      return date1!.compare(date2!) == ComparisonResult.orderedAscending 
     } 
     var dataEntries: [ChartDataEntry] = [] 
     for stat in 0...(sort.count - 1) { 
      let date = dateFormatter.date(from:sort[stat].date) 
      let timeIntervalForDate: TimeInterval = date!.timeIntervalSince1970 
      let dataEntry = ChartDataEntry(x: Double(timeIntervalForDate), y: sort[stat].total) 
      dataEntries.append(dataEntry) 
      if stat == (sort.count - 1){ 
       let chartDataSet = LineChartDataSet(values: dataEntries, label: "\(sort[stat].exercise)") 
       chartDataSet.setCircleColor(color[stat]) 
       chartDataSet.setColor(color[stat], alpha: 1.0) 

       chartDataSet.drawValuesEnabled = true 
       dataSets.append(chartDataSet) 
       startChart(dataSets: dataSets) 
      } 
     } 
    } 

} 

func startChart(dataSets:[LineChartDataSet]){ 
    testLineChartView.animate(xAxisDuration: 0.7, yAxisDuration: 0.7) 
    testLineChartView.dragEnabled = true 
    testLineChartView.legend.form = .circle 
    testLineChartView.drawGridBackgroundEnabled = false 

    let xaxis = testLineChartView.xAxis 
    xaxis.valueFormatter = axisFormatDelegate 
    xaxis.labelCount = dataSets.count 
    xaxis.granularityEnabled = true 
    xaxis.granularity = 1.0 
    xaxis.centerAxisLabelsEnabled = true 
    xaxis.avoidFirstLastClippingEnabled = true 
    xaxis.drawLimitLinesBehindDataEnabled = true 

    let rightAxis = testLineChartView.rightAxis 
    rightAxis.enabled = false 

    let leftAxis = testLineChartView.leftAxis 
    leftAxis.drawZeroLineEnabled = true 
    leftAxis.drawGridLinesEnabled = true 
    axisFormatDelegate = self 
    testLineChartView.delegate = self 

    let chartData = LineChartData(dataSets: dataSets) 
    testLineChartView.data = chartData 
    testLineChartView.chartDescription?.text = "" 
} 

extension ChartViewController: IAxisValueFormatter { 
func stringForValue(_ value: Double, axis: AxisBase?) -> String { 
    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "dd MMM" 
    let date = Date(timeIntervalSince1970: value) 
    return dateFormatter.string(from: date) 
} 

}

enter image description here

回答

0

我使用的x軸的日期,以及在我的項目之一,我只是改變每個日期一個字符串並將一個字符串值數組傳遞給一個IndexAxisValueFormatter。

testLineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xvalues) 

如果這不是你正在尋找的,你能展示一個什麼樣的「sets」包括嗎?然後我將能夠運行你的代碼。

+0

這是不一樣的(讓x軸= testLineChartView.xAxis xaxis.valueFormatter = axisFormatDelegate)? – ThundercatChris

+0

我已更新我的代碼以顯示設置。它基本上是一個具有繪圖點陣列的數組,由結構數組組成。這樣一條線可以在一個數組內有幾個繪圖點,而另一條線可以有它自己的。我希望這是有道理的 – ThundercatChris

+0

我也嘗試使用日期字符串數組(我認爲這就是你所建議的),但他們又是錯位。我不認爲字符串是問題。它的抵消 – ThundercatChris