iOS图表3.1.1 - X轴日期值不准确且数据未正确绘制

时间:2018-05-29 20:55:30

标签: ios json swift frameworks ios-charts

所以我正在使用第三方框架,名为' Charts'处理在我的应用程序中显示重量数据。但它看起来很奇怪,这里有一张照片,然后是我的代码。

my weight chart

有些值是正确的,但我的4个重量值中只有2个被显示,而且下面的日期以6个月为增量增加,我不知道为什么,因为我从未在我的代码中指定过这个,它们与我的体重值不匹配。这是我想要显示的重量对象。

"progressWeights": [
        {
            "weight": "200",
            "_id": "5af4c835b83893a9754b82ec",
            "createdOn": "2010-05-10T00:00:00.000Z"
        },
        {
            "weight": "145",
            "_id": "5afe6614cbd4ff00323461ed",
            "createdOn": "2018-05-17T07:00:00.000Z"
        },
        {
            "weight": "153",
            "_id": "5b0cbc8db62b190844cce1e9",
            "createdOn": "2010-05-28T00:00:00.000Z"
        },
        {
            "weight": "185",
            "_id": "5b0cbcadb62b190844cce1ea",
            "createdOn": "2018-05-25T00:00:00.000Z"
        }
]

现在这里是我的一些体重表代码:

// Chart Data
var weightData = [String]() 
var weightUploadDates = [String]()

func lineChartSetup() {

    // convertToDate and millisecondsSince1970 are helper methods, output is time interval
    let uploadDates = weightUploadDates.map({ convertToDate(str: $0).millisecondsSince1970 })

    // Line chart population
    setLineChart(dataPoints: weightData, values: uploadDates) 
}

func setLineChart(dataPoints: [String], values: [Int]) {

    /*
     Here are what params are holding currently
     dataPoints:
      ▿ 4 elements
       - "200"
       - "145"
       - "153"
       - "185"

     values:
     ▿ 4 elements
       - 1273449600000
       - 1526540400000
       - 1275004800000
       - 1527206400000
    */

    var idx = 0
    // data point setup and color config
    for i in dataPoints {
        let dataPoint = ChartDataEntry(x: Double(values[idx]), y: Double(i)!) 
        dataEntry.append(dataPoint)
        idx += 1
    }

    // Setting up the data set for chart
    let chartDataSet = LineChartDataSet(values: dataEntry, label: "Weight")
    let chartData = LineChartData()
    chartData.addDataSet(chartDataSet)

    // Axes Setup
    let formatter: ChartFormatter = ChartFormatter()
    let str = values.map({ String($0) })
    formatter.setValues(values: str)
    let xaxis: XAxis = XAxis()
    xaxis.valueFormatter = formatter
    lineChartView.xAxis.labelPosition = .bottom
    lineChartView.xAxis.drawGridLinesEnabled = false
    lineChartView.xAxis.valueFormatter = xaxis.valueFormatter // I think this might be the problem?

    lineChartView.data = chartData
}

我认为问题的根源是x轴值格式化程序,如上图所示,日期与我的重量对象中的任何一个都不匹配,并且它们没有正确对齐用我的体重数据。这是我的值格式化代码:

public class ChartFormatter: NSObject, IAxisValueFormatter {

    var weightData = [String]()

    // This sets the x axis? - Dates go up by 6 months for some reason?...
    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        //print("String for value desc \(value)")
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd"
        let date = dateFormatter.string(from: Date(timeIntervalSince1970: value))
        return date
    }

    public func setValues(values: [String]) {
        self.weightData = values
    }
}

当我的图表显示在' stringForValue'中的print语句时方法被调用几乎一百次,这里有一些输出:

String for vlaue desc 1280000000000.0
The date value: 08/02
String for vlaue desc 1320000000000.0
The date value: 02/18
String for vlaue desc 1360000000000.0
The date value: 09/07
String for vlaue desc 1400000000000.0
The date value: 03/27
String for vlaue desc 1440000000000.0
The date value: 10/13
String for vlaue desc 1480000000000.0

似乎我的第一个日期值(1273449600000)被舍入为1280000000000.0,然后为每次迭代添加40000000000?我不知道这是从哪里来的,也不应该这样做。

这就是我的问题,关于如何让x轴只保留我拥有的4个日期对象并让它与其适当的重量值相匹配的任何想法?任何帮助表示赞赏。如果您需要更多信息,请告诉我。

0 个答案:

没有答案