iOS图表3 - 使用图表对齐X标签(日期)

时间:2017-05-09 07:28:40

标签: charts swift3 alignment nsdate ios-charts

我在这里找到了类似的问题,但答案对我没有帮助。我认为由于我的数据结构。

我有一个由单个数组组成的数组,每个数组都在图表中有自己的行。然后由该线的绘图点组成多个结构。

我的问题是值/行是正确的,但未与日期正确对齐。在下面的例子中。日期从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

2 个答案:

答案 0 :(得分:0)

我在我的一个项目中也在x轴上使用日期,我只是将每个日期更改为一个字符串,并将一个字符串值数组传递给IndexAxisValueFormatter。

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

如果这不是你想要的,你能否展示一下"设置"包括?然后我就可以运行你的代码了。

答案 1 :(得分:0)

这是另一个线程,我开始解决一个没有显示的值的问题。它包括修复此问题的代码。

我认为重点是

 xaxis.forceLabelsEnabled = true

iOS Charts - single values not showing Swift