如何使用动态数据正确对齐多个/分组的条形图条形图?

时间:2019-07-15 19:49:01

标签: ios swift charts mpandroidchart ios-charts

我使用图表框架设置了多个/组条形图。问题在于,条形对齐在大多数情况下不起作用,并且它不会与x轴标题和组垂直线对齐

这是我的代码,不要被GroupedChartDataModel困扰,它的类封装为2个小数组[[Double]]

func getGroupedBarChartData() -> BarChartData? {
        guard let chartData = chartData as? [GroupedChartDataModel] else {
            return nil
        }


        let groupSpace = 1 - (0.23 * Double(chartData.count))
        let barSpace = 0.03
        let barWidth = 0.2

        var groupedDataSet = [BarChartDataSet]()

        for (index, groupedChartData) in chartData.enumerated()
        {
            var dataEntryArray = [BarChartDataEntry]()

            for (i, data) in groupedChartData.values.enumerated()
            {
                let value = BarChartDataEntry(x: Double(i), y: data.value)
                dataEntryArray.append(value)
            }

            let set = BarChartDataSet(values: dataEntryArray, label: groupedChartData.title)

            set.setColor(getColor(forIndex: index))
            groupedDataSet.append(set)
        }

        let data = BarChartData(dataSets: groupedDataSet)

        data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 10)!)
        data.barWidth = barWidth

        chartView?.xAxis.axisMinimum = Double(0)

        // groupWidthWithGroupSpace(...) is a helper that calculates the width each group needs based on the provided parameters
        chartView?.xAxis.axisMaximum = Double(0) + data.groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(chartData.count)

        data.groupBars(fromX: 0, groupSpace: groupSpace, barSpace: barSpace)



        return data
    }

这是图表的其他配置。

override func configureUI() {
    guard let chartView = chartView else {
        return
    }

    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    let formatter = CustomLabelsXAxisValueFormatter()
    formatter.labels = xAxisTitleValues ?? []
    xAxis.valueFormatter = formatter
    xAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
    xAxis.granularity = 1
    xAxis.labelRotationAngle = 0
    xAxis.labelCount = xAxisTitleValues?.count ?? 0
    xAxis.centerAxisLabelsEnabled = true


    let leftAxisFormatter = NumberFormatter()
    leftAxisFormatter.maximumFractionDigits = 1

    let leftAxis = chartView.leftAxis
    leftAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
    leftAxis.spaceTop = 0.35
    leftAxis.axisMinimum = 0

    chartView.rightAxis.enabled = false

    if animationEnabled {
        chartView.animate(yAxisDuration: 1.5)
    }

    chartDrawData = getGroupedBarChartData()


    chartView.data = chartDrawData
}

传递给getGroupedBarChartData()的数据是:

[
  [530.0, 300.0],
  [1000.0, 500.0],
  [480.0, 270.0],
  [0.0, 0.0],
  [0.4000000059604645, 0.4000000059604645]
]

此图产生的结果: enter image description here

[    
   [3800.0, 8000.0, 2800.0, 0.0, 2.0],
   [3800.0, 8000.0, 2800.0, 0.0, 2.0]
] 

此图产生的结果: enter image description here

  

还值得一提的是,如果有   剩余的数据被隐藏

0 个答案:

没有答案