0

我正在尝试使用 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)
}


}

期望的输出在此处输入图像描述

4

1 回答 1

1

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

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

我找到的解决方案是对 x 轴上的每个点使用一个增量,并使用一个数组,[Date]其索引与图表data属性中数据点的顺序相对应(例如,调用 this 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
  }
  
}
于 2021-04-04T22:18:54.357 回答