缩放图表时,TimeLineChart在xAxis中显示多个日期

时间:2019-06-21 10:48:03

标签: swift charts ios-charts

我只在Charts Library的TimeLineChart上的xAxis上设置日期,但是日期设置为缩放时相差一小时,因为会显示多个相同的日期。 这是因为差异是基于时间的,但是我需要以天为基础。

我的代码是:-

我正在使用以下代码。

func setupCharts(){
    chartView.delegate = self
    chartView.chartDescription?.enabled = false
    chartView.dragEnabled = true
    chartView.setScaleEnabled(true)
    chartView.pinchZoomEnabled = true
    chartView.backgroundColor = .white
    chartView.legend.enabled = false

    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    xAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
    xAxis.labelTextColor = .black
    xAxis.drawGridLinesEnabled = true
    xAxis.centerAxisLabelsEnabled = false
    xAxis.granularityEnabled = true
    xAxis.valueFormatter = DateValueFormatter()


    let leftAxis = chartView.leftAxis
    leftAxis.labelPosition = .outsideChart
    leftAxis.labelFont = .systemFont(ofSize: 12, weight: .light)
    leftAxis.drawGridLinesEnabled = true
    leftAxis.granularityEnabled = true
    leftAxis.axisMinimum = 0
    leftAxis.axisMaximum = 1000
    leftAxis.labelTextColor = .black

    chartView.rightAxis.enabled = false
    chartView.legend.form = .line
    self.setDataCount(self.weights.count)
    chartView.animate(xAxisDuration: 1.5)
}

func setDataCount(_ count: Int) {

    let values = (0..<count).map { (x) -> ChartDataEntry in
        let weightLogs = weights[x] as WeightLogsModel
        let y = weightLogs.weight!
        let date = weightLogs.dated!
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let dateValue = dateFormatter.date(from: date)
        let milisec = Int(dateValue!.timeIntervalSince1970)
        print(dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(milisec))))
        return ChartDataEntry(x: Double(milisec), y: Double(y))
    }

    let set1 = LineChartDataSet(entries: values, label: "DataSet 1")
    set1.setColor(UIColor(red: 91/255, green: 209/255, blue: 116/255, alpha: 1))
    set1.lineWidth = 1.5
    set1.drawCirclesEnabled = true
    set1.drawValuesEnabled = true
    set1.fillColor = UIColor(red: 91/255, green: 209/255, blue: 116/255, alpha: 1)
    set1.setCircleColor(UIColor(red: 91/255, green: 209/255, blue: 116/255, alpha: 1))
    set1.circleRadius = 2.5
    set1.fill = Fill(color: #colorLiteral(red: 0.3568627451, green: 0.8196078431, blue: 0.4549019608, alpha: 0.3466109155))
    set1.drawFilledEnabled = true
    set1.highlightColor = #colorLiteral(red: 0.262745098, green: 0.7882352941, blue: 0.3647058824, alpha: 1)
    set1.mode = (set1.mode == .cubicBezier) ? .linear : .cubicBezier
    let data = LineChartData(dataSet: set1)

    data.setValueTextColor(.black)
    data.setValueFont(.systemFont(ofSize: 9, weight: .light))

    chartView.data = data
}

Here is the chart when values are set and chart is not zoomed.

Here is the chart when values are set and chart is zoomed. Now you can see the values of xaxis duplicates.

0 个答案:

没有答案