Convert bar chart to a grouped bar chart with danielgindi/ios-charts and Swift
You haven't actually done anything with unitsBought
.
For a bar chart you can do two things:
- Add another DataSet with your
unitsBought
- Use the other overload of
BarCharDataEntry(values: [Double], xIndex: Int)
orBarCharDataEntry(values: [Double], xIndex: Int, label: String)
to pass multiple values per entry, which makes it stacked.
How to switch from Grouped Bar chart to Simple Bar Charts
I found the answer on the Documentation. It was quite hard to find.
The only thing u have to do is call the fitScreen() method And its gets to the original state.
func updateBarchart(which segment: Int)
{
//getSumofMonths()
let pecsColor = UIColor(red:0.278, green:0.247, blue:0.247, alpha: 1.000)
let petreColor = UIColor(red:0.941, green:0.400, blue:0.184, alpha: 1.000)
if segment == 1
{
let dataSets = [createDataSet(from: pecsBevetel, label: "Pécs", color: pecsColor),createDataSet(from: petreBevetel, label: "Újpetre", color: petreColor)]
groupedBarChart.setGroupedBarchartData(from: dataSets, xValues: monthStrings)
groupedBarChart.xAxis.granularityEnabled = true
groupedBarChart.xAxis.granularity = groupedBarChart.xAxis.axisMaximum / Double(monthStrings.count)
groupedBarChart.xAxis.axisMaximum = Double(12)
groupedBarChart.setVisibleXRangeMaximum(6)
//groupedBarChart.setVisibleXRangeMinimum(0)
groupedBarChart.xAxis.centerAxisLabelsEnabled = true
}
else if segment == 0
{
groupedBarChart.setBarChartData(xValues: monthStrings, yValues: pecsBevetel, label: "Pécs", colors: [pecsColor])
//groupedBarChart.setVisibleXRangeMinimum(12)
groupedBarChart.xAxis.centerAxisLabelsEnabled = false
groupedBarChart.fitScreen() //<- This method solves the problem.
}
else if segment == 2
{
groupedBarChart.setBarChartData(xValues: monthStrings, yValues: petreBevetel, label: "Újpetre", colors: [petreColor])
//groupedBarChart.setVisibleXRangeMinimum(12)
groupedBarChart.xAxis.centerAxisLabelsEnabled = false
groupedBarChart.fitScreen() //<- This method solves the problem.
}
//groupedBarChart.xAxis.axisMinimum = Double(0.5)
groupedBarChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
groupedBarChart.chartDescription?.text = ""
groupedBarChart.fitBars = true
groupedBarChart.doubleTapToZoomEnabled = false
let legend = groupedBarChart.legend
legend.enabled = true
legend.horizontalAlignment = .center
legend.verticalAlignment = .bottom
legend.orientation = .horizontal
legend.drawInside = false
legend.yEntrySpace = 0.0;
let marker = BalloonMarker(color: UIColor.white, font: UIFont.systemFont(ofSize: 12), textColor: UIColor.black, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))
marker.minimumSize = CGSize(width: 75.0, height: 35.0)
groupedBarChart.marker = marker
groupedBarChart.drawMarkers = true
groupedBarChart.moveViewToX(0)
}
So to be short call this method AFTER you set the data of the chart.
groupedBarChart.fitScreen() //<- This method solves the problem.
iOS Charts remove values on the bar of the bar chart
Add following line of code where you setup sets of chart to fix your issue:
var set = BarChartDataSet(values: yVals, label: "Data Set")
set.drawValuesEnabled = false // Set this property to false to hide all values
let data = BarChartData(dataSet: set)
yourChartView.data = data
This code will hide all values above bar chart. I hope this will help you.
Related Topics
How to Hide the Navigation Back Button in Swiftui
Get Location from Center of Screen Swift Mapkit
Reversing the Order of a String Value
How to Block Users on Firebase in a Social Media App? for iOS
Firebase Firestore Not Updating Email Verification Status
Swiftui 2 Firebase Push Notification
How to Get Next Case of Enum(I.E. Write a Circulating Method) in Swift 4.2
How to Use JSON Arrays with Alamofire Parameters
What Are the Advantages/Use Cases of Optional Patterns Introduced in Swift 2
Swift - Class Method Which Must Be Overridden by Subclass
Using "If Let" with Logical "Or" Operator
With Firebase, Swift Removeobserver(Withhandle Does Not Remove the Observer
How to Fix 'Line Length Violation: Line Should Be 120 Characters or Less' - Swiftlint
Spritekit - Create at Random Position Without Overlapping
In Swift, Why Does Assigning to a Static Variable Also Invoke Its Getter