ChartsOrg / ChartsOrg/Charts

Bar chart content not centerd horizontally

Open
#4,082 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
28k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

I have a problem I can not center the content chart horizontally

Capture d’écran 2019-07-30 à 12 21 43

```
private func setup(xValue: [String]) {
noDataText = "No data"

xAxis.drawGridLinesEnabled = false
xAxis.labelPosition = .bottom
xAxis.valueFormatter = IndexAxisValueFormatter(values: xValue)
xAxis.granularity = 1
xAxis.granularityEnabled = true
xAxis.labelCount = xValue.count

xAxis.axisMinimum = 0.0
xAxis.axisMaximum = Double(xValue.count)

leftAxis.spaceTop = 0.35
leftAxis.axisMinimum = 0
leftAxis.drawGridLinesEnabled = false

rightAxis.enabled = false
}
```

And to create the chart:
```
private func setChart(xValue: xAxisValue, values: [SBChartEntry]) {
let dataSets: [BarChartDataSet] = values.map {
let set = BarChartDataSet(entries: $0.value.enumerated().map { index, element in
BarChartDataEntry(x: Double(index), y: element)
}, label: $0.name)

set.colors = [$0.color]
return set
}

let chartData = BarChartData(dataSets: dataSets)

let groupSpace: Double = (values.count > 1) ? 0.7 / (Double(values.count) * 2.2) : 0.6
let barSpace = (values.count > 1) ? 0.05 : 0
let barWidth: Double = (values.count > 1) ? 0.75 / Double(values.count) : 0.4

chartData.barWidth = barWidth

if values.count > 1 {
xAxis.centerAxisLabelsEnabled = true
chartData.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace)
} else {
// For one bar
}

notifyDataSetChanged()

data = chartData

//chart animation
animate(xAxisDuration: 1, yAxisDuration: 1, easingOption: .linear)
}
```

I can not configure when the chart contains only one array (so only one bar)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the single-array case in the setup and setChart methods shown in the issue, focusing on the x-axis bounds and bar positioning. Done means a chart containing one bar is horizontally centered without breaking the existing multi-array grouping behavior; no repository file or test is named.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.