iOS Charts remove decimal from yValues
Thanks for every one who tried to help, here was the fix, adding the below code
let formatter = NumberFormatter()
formatter.numberStyle = .none
formatter.maximumFractionDigits = 0
formatter.multiplier = 1.0
chartData.valueFormatter = DefaultValueFormatter(formatter: formatter)
to the setBarChartData func
func setBarChartData(xValues: [String], yValues: [Double], label: String) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<yValues.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
let chartData = BarChartData(dataSet: chartDataSet)
let formatter = NumberFormatter()
formatter.numberStyle = .none
formatter.maximumFractionDigits = 0
formatter.multiplier = 1.0
chartData.valueFormatter = DefaultValueFormatter(formatter: formatter)
let chartFormatter = BarChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter
self.data = chartData
}
iOS Charts ValueFormatter
Almost there, just need to add the following class:
class ChartValueFormatter: NSObject, IValueFormatter {
fileprivate var numberFormatter: NumberFormatter?
convenience init(numberFormatter: NumberFormatter) {
self.init()
self.numberFormatter = numberFormatter
}
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
guard let numberFormatter = numberFormatter
else {
return ""
}
return numberFormatter.string(for: value)!
}
}
Now use this as the number formatter:
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = Locale.current
let valuesNumberFormatter = ChartValueFormatter(numberFormatter: numberFormatter)
lineChartDataSet.valueFormatter = valuesNumberFormatter
lineChartDataSet.valueFont = lineChartDataSet.valueFont.withSize(chartFontPointSize)
remove zero line from ios charts
If you just want to hide the xAxis, then I see two ways:
You could disable the xAxis xAxis.enabled = false
Or set the xAxis line color to transparent xAxis.axisLineColor = UIColor.clear
Charts 3.0 - Can not show decimal in BarChart - iOS Swift
I get the answer:
let format = NumberFormatter()
format.numberStyle = .decimal
let formatter = DefaultValueFormatter(formatter: format)
chartData.setValueFormatter(formatter)
Y-axis is repeating values - iOS Charts
you could also try:
/// When true, axis labels are controlled by the `granularity` property.
/// When false, axis values could possibly be repeated.
/// This could happen if two adjacent axis values are rounded to same value.
/// If using granularity this could be avoided by having fewer axis values visible.
public var granularityEnabled = false
and set yAxis.granularity
= some value
How can I hide 0 values on ios-chart?
I made it happen on a PieChart in one of my apps just like that :
...
let dataSet = PieChartDataSet(yVals: yVals, label: nil)
// This is where the magic happen
// You set a NSNumberFormatter with an empty zero Symbol
let noZeroFormatter = NumberFormatter()
noZeroFormatter.zeroSymbol = ""
dataSet.valueFormatter = ChartDefaultValueFormatter(formatter: noZeroFormatter)
let chartData = PieChartData(xVals: xVals, dataSet: dataSet)
...
How can I convert these doubles to integers on this iOS Bar Chart?
Give the chartDataSet
its own valueFormatter
:
This Q&A shows how to create the valueFormatter you need. Then set the valueFormatter
of charDataSet
to:
chartDataSet.valueFormatter = DigitValueFormatter()
X Axis values being repeated rather than replaced iOS Charts
I fixed the issue myself by adding
chartContainer.xAxis.granularityEnabled = true
chartContainer.xAxis.granularity = 1.0
Related Topics
How to Observe Array Property Changes in Rxswift
How to Retrieve a Value from Dictionary in Swift 3
Swiftui - Pass Data to Different Views
Swift: How to Hold Any Possible Instance of a Generic Type in a Variable
How to Create a Nsmutabledictionary in Swift
Swift: Reusable Uiview in Storyboard and Sizing Constraints
How to Blur the Background in a Swiftui MACos Application
Changing Nav Bar Item Programmatically in Swift
How to Test an Optionset with a Switch Statement
Testing Protocol Conformance with Associated Types
Nspopover to Start in a Detached State
Not Getting Expected Delegate Calls When Trying to Restore In-App Purchases with Storekit
How to Give PDF Data a Filename for User to Save in Swift
A Codable Structure Contains a Protocol Property
How to Load the Photo Library into Uicollectionview? Swift
Using Structs (Bytes) with Swift - Struct to Nsdata and Nsdata to Struct
Multidimensional Dictionaries Possible in Swift
Arkit - How to Display the Feed from a Virtual Scncamera Placed on Scnplane