当列数超过2列时,条形图会跳起来

时间:2017-02-02 17:23:37

标签: ios swift ios-charts

按下按钮时,我正在处理数据集更改的图表。但看起来当我有超过2个柱时,图表原点会发生变化。

Chart

两列很好:

2 column chart

创建数据集

private func buildMoodDummyBarData(period: Period) -> [BarChartDataEntry] {

    if period == .week {
        let entry1 = BarChartDataEntry(x: 0, y: 720)
        let entry2 = BarChartDataEntry(x: 1, y: 440)
        let entry3 = BarChartDataEntry(x: 2, y: 0)
        let entry4 = BarChartDataEntry(x: 3, y: 30)
        let entry5 = BarChartDataEntry(x: 4, y: 30)

        return [entry1, entry2, entry3, entry4, entry5]

    } else if period == .month {

        let entry1 = BarChartDataEntry(x: 0, y: 100)
        let entry2 = BarChartDataEntry(x: 1, y: 300)
        let entry3 = BarChartDataEntry(x: 2, y: 50)
        let entry4 = BarChartDataEntry(x: 3, y: 30)
        let entry5 = BarChartDataEntry(x: 4, y: 30)

        return [entry1, entry2, entry3, entry4, entry5]

    } else {

        return []
    }
}

完成建筑数据集和更改条形颜色

func buildMoodBarData(period: Period) -> BarChartData? {

    let entries = buildMoodDummyBarData(period: period)

    let set = BarChartDataSet(values: entries, label: nil)
    set.highlightEnabled = false

    let tempData = BarChartData(dataSet: set) // Just to find max
    let max = tempData.yMax
    let onePercent = max / 100

    var colors = [UIColor]()
    for item in entries {
        let percentValue = item.y / onePercent
        colors.append(setColor(value: percentValue))
    }
    set.colors = colors

    let data = BarChartData(dataSet: set)
    data.setDrawValues(true)
    data.barWidth = 0.72

    return data
}

// barChartView is storyboard outlet
private  func makeMoodBarChart() {

    barChartView.backgroundColor = UIColor.red
    barChartView.chartDescription = nil
    barChartView.leftAxis.enabled = false
    barChartView.rightAxis.enabled = false
    barChartView.legend.enabled = false
    barChartView.xAxis.enabled = false
}

private func updateMoodBarChart(with data: BarChartData) {

    barChartView.data = data
    barChartView.fitBars = false
    barChartView.fitScreen()
}

2 个答案:

答案 0 :(得分:0)

使用新数据集更新Barchart时,将chartdata设为nil

if let bardata = buildMoodBarData(period: state) {
    barChatView.data = nil

    updateMoodBarChart(with: bardata)
}

答案 1 :(得分:0)

问题与数据集有关。当我将其替换为以下数据时,问题就消失了。我认为问题出现是因为Y比例值变化太大了。

private func buildMoodDummyBarData(period: Period) -> [BarChartDataEntry] {

    if period == .week {
        let entry1 = BarChartDataEntry(x: 0, y: 10)
        let entry2 = BarChartDataEntry(x: 1, y: 50)
        let entry3 = BarChartDataEntry(x: 2, y: 100)
        let entry4 = BarChartDataEntry(x: 3, y: 20)
        let entry5 = BarChartDataEntry(x: 4, y: 30)

        return [entry1, entry2, entry3, entry4, entry5]

    } else if period == .month {

        let entry1 = BarChartDataEntry(x: 0, y: 100)
        let entry2 = BarChartDataEntry(x: 1, y: 30)
        let entry3 = BarChartDataEntry(x: 2, y: 30)
        let entry4 = BarChartDataEntry(x: 3, y: 20)
        let entry5 = BarChartDataEntry(x: 4, y: 30)

        return [entry1, entry2, entry3, entry4, entry5]

    } else {

        return []
    }
}

这并不意味着两个数据集(20到100)中的最小值和最大值必须完全相同,但是如果差异太大(第一个数据集为30到320,第二个数据为30到720),图表基线变化。

这不是理想的解决方案。