【问题标题】:How to use date for x-Axis in SwiftChart?如何在 SwiftChart 中为 x 轴使用日期?
【发布时间】:2020-10-31 09:36:39
【问题描述】:

我正在尝试使用 SwiftChart pod 在我的项目中绘制折线图。我从 API 获得响应为 ["activity_count":0 , "date": "2020-10-31"] 但图表适用于双值,所以我不知道如何绘制图表。

import UIKit
import SwiftChart
class AnalyticsViewController: UIViewController {
    @IBOutlet weak var graphContainer: UIView!
    var graphData = [(x:0, y: 0.0)]
    var graphpResponseData = [[String: Any]]()
    override func viewDidLoad() {
        super.viewDidLoad()
    graphpResponseData.append(["activity_count":0 , "date": "2020-10-31"])
    graphpResponseData.append(["activity_count":2 , "date": "2020-10-30"])
    graphpResponseData.append(["activity_count":1 , "date": "2020-10-29"])
    graphpResponseData.append(["activity_count":29 , "date": "2020-10-28"])
    graphpResponseData.append(["activity_count":1 , "date": "2020-10-27"])
    graphpResponseData.append(["activity_count":0 , "date": "2020-10-26"])
    graphpResponseData.append(["activity_count":0 , "date": "2020-10-25"])
    graphpResponseData.append(["activity_count":0 , "date": "2020-10-24"])
    graphpResponseData.append(["activity_count":0 , "date": "2020-10-23"])
    graphpResponseData.append(["activity_count":0 , "date": "2020-10-22"])
    
   ???? unable to proceed
    //let chart = Chart(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
            //let series = ChartSeries(data: graphData)
    //chart.add(series)
    //chart.xLabels = [1, 2, 3, 4]
    //chart.xLabelsFormatter = { String(Int(round($1))) + "Oct" }
    //graphContainer.addSubview(chart)
}


}

想要的输出

【问题讨论】:

    标签: swift swiftcharts


    【解决方案1】:

    我只需要解决这个问题。我将内存中每个日期的月份日期用作 x 轴,但事实证明这会导致图表出现意外行为并在月份更改时显示随机绘制的点。

    我还尝试使用 x 的时间戳(Double 类型,ChartSeries 的数据初始化程序会接受,但这也失败了。

    我找到的解决方案是对 x 轴上的每个点使用 1 的增量,并使用 [Date] 的数组,其索引与图表的 data 属性中数据点的顺序相对应(对于例如,称之为correspondingDates)。在chart.xLabelsFormatter 内部,我将使用correspondingDates[value] 访问这个数组。从那里,我使用了两个 dateFormatter 扩展来访问这些值。

    代码如下:

    chart.xLabelsFormatter = { (index,value) in        
     //Format for label
     let date = correspondingDates[Int(value)]
     let dayAbbr = DateFormatter.getDay(date: date)
     let monthAbbr = DateFormatter.getMonth(date: date)
     return monthAbbr + "-" + dayAbbr
    
    

    DateFormatter 的扩展:

    extension DateFormatter{
      
      /// Used specifically to get the Month Abrreviation (ie Feb), but can be used for formatting in other ways.
      /// - Parameter format: Can be 'LLL' for the month abbreviated to 3 letters, or something like 'D/MM/Y'
      /// - Returns: Returns the String version of the date, with precision at any level
      static func getMonth(format:String = "LLL",date: Date = Date())->String{
          let formatter = DateFormatter()
          let today = date // formatter.da(from: Calendar.current.dateComponents([.day], from: Date()))
      
          formatter.dateFormat = format
          let string = formatter.string(from: today)
          return string
      }
      
      
      /// Used specifically to get the Day Abrreviation (ie 05 or 27), but can be used for formatting in other ways.
      /// - Parameters:
      ///   - format: Can be 'LLL' for the month abbreviated to 3 letters, or something like 'D/MM/Y'
      ///   - date: optionally pass in your own date
      /// - Returns:  Returns the String version of the day, with precision at any level
      static func getDay(format:String = "dd",date: Date = Date())->String{
          let formatter = DateFormatter()
          let today = date // formatter.da(from: Calendar.current.dateComponents([.day], from: Date()))
      
          formatter.dateFormat = format
          let string = formatter.string(from: today)
          return string
      }
      
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多