I am using the Charts framwork for Swift found here. I have a chart that I am trying to fix two issues shown in the chart below:
The first is trying to format my X values from doubles to Ints. I want the value to show 67 and not 67.0. I have tried the following to adjust it but
numbers still stay the same:
let format = NumberFormatter()
format.minimumIntegerDigits = 0
format.minimumFractionDigits = 0
let formatter = DefaultValueFormatter(formatter: format)
chartview.rightAxis.valueFormatter = (formatter as? IAxisValueFormatter)
I also have a problem where the buttom bars dont line up with the buttom of the graph. If you look at the graph above, it looks like 0 starts above the X axis and not at the orgin. Theres a little space below the bars. I would like the bottom of each bar to touch the X-Axis.
Please try like this :
let format = NumberFormatter()
format.minimumIntegerDigits = 0
format.minimumFractionDigits = 0
let formatter = DefaultValueFormatter(formatter: format)
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Label") // replace with your set
chartDataSet.valueFormatter = formatter
For lining Button bars. please try this:
barChartView.rightAxis.axisMinimum = 0.0
barChartView.leftAxis.axisMinimum = 0.0
Related
I just updated https://github.com/danielgindi/Charts to Charts 4.1 from 3.1
Before I formatted my values with this code
let pFormatter = NumberFormatter()
pFormatter.numberStyle = .percent
pFormatter.maximumFractionDigits = 0
pFormatter.multiplier = 1
pFormatter.percentSymbol = "%"
chartData.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
But it has no effect at all. Values are rare, unformatted and no percentSymbol at all.
I tried to google, but all solutions seem to be the same code as mine.
For some reason, the formatter has no effect at all, before it worked.
I checked the changelog but I can't find anything which had changed in terms of value formatting.
I know similar question has been asked but I still could not figured out the solution.
I am getting double value like this.
let priceUsdInt = (price as NSString).doubleValue
And I want to compare this value to 1.00 so:
if priceUsdInt > 1.00 {
let priceUsdCur = priceUsdInt.currencyUS
finalPriceUsdCur = priceUsdCur
} else {
let priceUsdCur = priceUsdInt.currencyUS6
finalPriceUsdCur = priceUsdCur
}
This always bring two decimal results. Even-though value is way lower then 1.00.
Basically, what I want to achieve is if value is lesser then 1.00 then show it until six decimals i.e. 0.123456 when converted to currency format, if not show only two decimals after it i.e. 1.23.
Thank you for your time.
This demonstrates showing a 6 digit precision in currency formatting when the value from string coverted to double value is below 1.0 and 2 digit precision when its above 1.0
let belowOne = ".12023456"
let belowDoubleVal = Double(belowOne) ?? 0.0
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currency
if belowDoubleVal < 1.0 {
// this handles the 6 digit precision you require when value is below 1.0
currencyFormatter.minimumFractionDigits = 6
}
// old way using NSSNumber
// let belowOneString = currencyFormatter.string(from: NSNumber(value: belowDoubleVal))
// you can pass the double value to formatter using .string(for: Any)
// thanks for pointing this out by Leo Dabus
let belowOneString = currencyFormatter.string(for: belowDoubleVal)
I'm building a temperature line chart using Charts for iOS.
I have a timeline with an array of seconds from now time and the corresponding array of temperature temp.
example:
timeline.time = [504,633,777,803,1020]
timeline.temp = [87,88,88,86,87]
I would like my chart to show temperatures from the oldest seconds from now on the left, and newest on the right, but when I order my array of time in descending order, my chart is empty. Is there a way to show a chart with the smallest value on x axis on the left, and biggest value on the right?
Here is my current code:
var lineChartEntry = [ChartDataEntry]()
//loop in env data
for i in 0..<timeline.temp.count {
let value = ChartDataEntry(x: Double(timeline.time[i]), y: Double(timeline.temp[i]))
lineChartEntry.append(value)
}
let line1 = LineChartDataSet(values: lineChartEntry, label: "Temperature (°F)")
line1.colors = [UIColor.irbLightGreenColor()]
line1.mode = .horizontalBezier
line1.drawCirclesEnabled = false
line1.lineWidth = 3
let data = LineChartData(dataSet: line1)
data.setDrawValues(false)
temperatureLineChartView.data = data
If I try to reverse the array like so: Array(timeline.time[i].reversed()) / Array(timeline.temp[i].reversed()) my chart is empty.
If what you need to do is display the data in reversed order, then you can call the range's reversed() method to change the index and leave the data alone.
for i in (0..<timeline.temp.count).reversed() {
let value = ChartDataEntry(x: Double(timeline.time[i]), y: Double(timeline.temp[i]))
lineChartEntry.append(value)
}
Your attempt at Array(timeline.time[i].reversed()) was quite literally trying to reverse the i'th element of time[], not access the i'th element of reversed time[]. I don't see how that even compiled.
I am working on an application that needs to do some displaying and calculating of Double types in Swift. The struct that I created takes a Double as a parameter, and since it also displays it, I would like to keep the trailing zero at the end of the value. However, I cannot seem to keep the trailing zero from being truncated.
Here is what I have been trying in a Playground:
let numberString = "-3.60"
// "3.60"
let positiveString = numberString.stringByReplacingOccurrencesOfString("-", withString: "", options: nil, range: nil)
// 3.6
let positiveDouble = (positiveString as NSString).doubleValue
// "3.60"
let positiveDoubleString = NSString(format: "%0.02f", positiveDouble)
// 3.6
let positiveDoubleWithZeros = positiveDoubleString.doubleValue
I keep getting 3.6 as the result, no matter what I try. What is the obvious part of the conversion that I am missing?
You have to use NSNumberFormatter:
let formatter = NSNumberFormatter()
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 3
println(formatter.stringFromNumber(1.0000))
println(formatter.stringFromNumber(1.2345))
This example will print 1.00 for the first case and 1.234 for the second, the number of minimum and maximum decimal places can be adjust as you need.
I Hope that helps you!
How would I create an arc4random thing? Like in previous versions I've seen
int blank = arc4random(5)
But how would I do it in the most recent version of Xcode?
let maxNumber = 10
let randomIndex = Int(arc4random_uniform(UInt32(maxNumber)))