Swift图表-X轴值与组条形图中的条形不对齐

时间:2018-07-02 10:20:47

标签: ios swift iphone ios-charts swiftcharts

我已经用图表库创建了一个简单的条形图 at&t asm inline c++ problem中的内容仍然无法弄清楚如何将x轴值与分组的条对齐。我正在使用单独的类进行值格式设置,并且具有绘制n个分组条形图的通用功能。

func createGroupedBarChart(with data: ChartDataCollection)
{
    let grpBarChartView = BarChartView()
    self.addSubview(grpBarChartView)
    self.addConstraintsWithFormat(format: "H:|-5-[v0]-5-|", views: grpBarChartView)
    self.addConstraintsWithFormat(format: "V:|-5-[v0]-5-|", views: grpBarChartView)

    grpBarChartView.noDataText = "You need to provide data for the chart."
    grpBarChartView.chartDescription?.text = data.chartDescription

    if let barGrpChartData = data.chartData as? [BarChartGrouped]
    {
        //legend
        let legend = grpBarChartView.legend
        legend.enabled = true
        legend.horizontalAlignment = .right
        legend.verticalAlignment = .top
        legend.orientation = .vertical
        legend.drawInside = true
        legend.yOffset = 10.0;
        legend.xOffset = 10.0;
        legend.yEntrySpace = 0.0;

        let xaxis = grpBarChartView.xAxis
        //xaxis.valueFormatter = self
        xaxis.granularityEnabled = true
        xaxis.drawGridLinesEnabled = true
        xaxis.labelPosition = .bottom
        xaxis.centerAxisLabelsEnabled = false
        xaxis.granularity = 1
        xaxis.labelRotationAngle = 30
        let xValues = barGrpChartData.map{$0.valueX}

        let formater:BarChartFormatter = BarChartFormatter(xValues: xValues as! [String])

        let leftAxisFormatter = NumberFormatter()
        leftAxisFormatter.maximumFractionDigits = 1

        let yaxis = grpBarChartView.leftAxis
        yaxis.spaceTop = 0.35
        yaxis.axisMinimum = 0
        yaxis.drawGridLinesEnabled = false

        grpBarChartView.rightAxis.enabled = false



        var chartDataSets: [BarChartDataSet] = []
        var dataEntries: [[BarChartDataEntry]] = []
        let yCount = barGrpChartData[0].valueY.count

        for i in  0..<barGrpChartData.count
        {

            for j in 0..<yCount {

                let barChartDataEntry = BarChartDataEntry(x: Double(i), y: barGrpChartData[i].valueY[j])
                var dataEntriesTemp: [BarChartDataEntry] = []
                dataEntriesTemp.append(barChartDataEntry)


                if ( dataEntries.count >= yCount )
                {
                    dataEntries[j].append(barChartDataEntry)
                }
                else
                {
                    dataEntries.append(dataEntriesTemp)
                }
            }

        }


        for i in 0..<dataEntries.count
        {
            let chartDataSet = BarChartDataSet(values: dataEntries[i], label:  barGrpChartData[0].chartLabel[i])
            chartDataSet.colors = [barGrpChartData[0].colors[i]]
            chartDataSets.append(chartDataSet)
        }

        let chartData = BarChartData(dataSets: chartDataSets)


        let groupSpace = 0.1
        let barSpace = 0.05
        let barWidth = 0.3
        // (0.3 + 0.05) * 2 + 0.3 = 1.00 -> interval per "group"

        let groupCount = data.chartData.count
        let startYear = 0


        chartData.barWidth = barWidth;
        grpBarChartView.xAxis.axisMinimum = Double(startYear)
        let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
        print("Groupspace: \(gg)")
        grpBarChartView.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)

        chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
        //chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
        grpBarChartView.notifyDataSetChanged()

        grpBarChartView.data = chartData

        xaxis.valueFormatter = formater
        xaxis.setLabelCount(xValues.count, force: true)
        grpBarChartView.xAxis.valueFormatter = xaxis.valueFormatter

        grpBarChartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .linear)


    }

}


public class BarChartFormatter: NSObject, IAxisValueFormatter
{
    var xValues:[String] = []

    init(xValues: [String]) {
        self.xValues = xValues;
    }

    public func stringForValue(_ value: Double, axis: AxisBase?) -> String
    {
        let count = self.xValues.count

        if index >= 0 && index < count {

            return self.xValues[index]
        }

        return ""
    }
}

class ChartDataCollection
{
    var chartType: ChartType!
    var chartDescription: String!
    var chartData:[Any] = []

    init(chartType: ChartType, description: String, chartData: [Any]) {

        self.chartType = chartType
        self.chartDescription = description
        self.chartData = chartData
    }


}

class ChartDataCollection
{
    var chartType: ChartType!
    var chartDescription: String!
    var chartData:[Any] = []

    init(chartType: ChartType, description: String, chartData: [Any]) {

        self.chartType = chartType
        self.chartDescription = description
        self.chartData = chartData
    }


}

class ChartDataCollection
{
    var chartType: ChartType!
    var chartDescription: String!
    var chartData:[Any] = []

    init(chartType: ChartType, description: String, chartData: [Any]) {

        self.chartType = chartType
        self.chartDescription = description
        self.chartData = chartData
    }


}

https://github.com/danielgindi/ios-charts

请帮助

0 个答案:

没有答案