Swift charts - x axis values not aligned with bars in Group bar chart


Swift charts - x axis values not aligned with bars in Group bar chart



i have created the simple bar chart with the library
from https://github.com/danielgindi/ios-charts still can't figure out how to aligned x axis values with grouped bars. I am using a separate class for value formatting and i have a general function for draw n number of grouped bar charts.


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
}


}



Grouped bar chart



Please help









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy