iOS -Chart: Y- Axis interval difference between value - ios

I want to make Y-axis interval with a certain difference. Let's say the multiple of 100 or any fix value, currently it draws with their own interval.
I am sharing my formatting code here.
let leftAxis = chartView.leftAxis
leftAxis.valueFormatter = IntAxisValueFormatter()
leftAxis.granularityEnabled = true
leftAxis.granularity = 1

I am able to solve my challenge, I am adding my fix here. First I calculated the total range then divided them by interval diff and set the LabelCount.
let totalRange = maxY - minY
let interval = 100 //Let's say 100 in my case
leftAxis.setLabelCount(Int(totalRange/interval) + 1, force: true)

One way is you calculate scale value according to your data. And you can use following function to scale as you need. But exact with value difference I don't think it is possible.
self.lineChart.setScaleMinima(1.0, scaleY: 2.0)
In above value first parameter will zoom in horizontally while second will zoom vertically.

Related

How can I change object's size according to the distance/depth in ARView/ARKit

I am working in ARKit/RealityKit/Swift, If I keep one box near to the camera then it has a perfect size. But if I move it far from the camera e.g 1 or 2 meters far then it looks smaller. I want to keep its size same in the user's eyes/perception.
I have done some basic math on the Z coordinate of the object. e.g. if z = -0.9 and size = 0.5 meter then what will be new size if z = -2 or far. But it does not work.
//Sample code to get an idea
let original = Position()
let origianlSize: Float = 0.5
let newPosition = Position() // Assume it is far from original position
let newSize = origianlSize * newPosition.z / original.z // New size is not proper

How do I create a buffered LineString with a width defined in meters when using GEOSwift?

I'm hoping to find a way using GEOSwift to take a series of user's LatLngs, construct a linestring and buffer it to always be 30 meters wide regardless of the user's location. I feel like there must be an easier way than the path I'm going down and any help would be appreciated.
Background:
From what I can tell the buffer functions width parameter is currently defined in decimal degrees as my coordinate system is EPSG 4326, which makes calculating the width in meters difficult. I can get a rough estimation of meters per decimal degree for both longitude or latitude with the Haversine formula.
The problem I have is the series of points can move both latitudinally and longitudinally. So the buffer width I need in these cases lies somewhere between ThirtyMetersInLatDegrees and ThirtyMetersInLngDegrees. And in this case the width to supply to the buffer function becomes a weird approximation/ average of the user's overall longitudinal and latitudinal movement throughout the linestring related to ThirtyMetersInLngDegrees and ThirtyMetersInLatDegrees.
i.e. assuming ThirtyMetersInLngDegrees is the max:
ThirtyMetersInLatDegrees <= bufferWidth <= ThirtyMetersInLngDegrees
How can I better accomplish this?
Here's how I'm calculating meters per decimal degree:
//Earth’s radius
let R=6378137.0
let deviceLatitude = 37.535997
let OneMeterInLatDegrees = 1/R * (180/Double.pi)
let OneMeterInLngDegrees = 1/(R*cos(Double.pi*deviceLatitude/180)) * (180/Double.pi)
let ThirtyMetersInLatDegrees = 30 * latDegreesPerMeter
let ThirtyMetersInLngDegrees = 30 * lngDegreesPerMeter

Removing numbers using NSnumberformatter from a calculated number

I have a calculation that calculate square meters from millimeters and for this to show corect I would like to remove all numbers after the 2 first digits.
This is what I have tried so far.
let spha = sizeh.wshight()
let spwa = sizew.wswidth()
let areas1 = areaModel(pwa:spwa, pha:spha)
let formatter5 = NSNumberFormatter ()
formatter5.maximum = 2
let scarea = formatter5.stringFromNumber(areas1.sarea())!
screenAreaLabel?.text = "\(scarea)sqm"
I am a little lost at this point, I am using Float in calculation of this area.
Hope someone could help me in the right direction.
More detail info added.
I would like the Screen Area to only show 24sqm not 24772610sqm
You are multiplicating a value in millimeter by another value in millimeters, therefore the result is in square millimeters.
To convert square millimeters to square meters, just divide the result by 1_000_000.
You could also set formatter.multiplier to 1.0 / 1_000_000.

How to round a double to the nearest hundredth in Swift?

I am making an app, which takes one number usually something like 10.00 or 100.00 and multiplies it by 0.15 or 0.20 and outputs it to a Label.
What ends up happening in some situations is the answer comes out to be 2.3234342, notice all the numbers after the decimal, which I don't want.
I want Swift automatically round up to the nearest hundredth or keep hundredth the same and then delete everything after the hundredth.
I want the code to automatically determine whether it will round up or keep the number the same.
You can use round() with a "scale factor" of 1000:
let x = 14.14910001
let y = round(1000.0 * x) / 1000.0
println(y)
May be it will help you.
someFloat+=0.005
label.Text = String(format: "a float number: %.02f ", someFloat))

Setting iOS-Charts Float to Integer YAxis

I tried setting yAxis to integer using NSNumberFormatter,
yAxis.valueFormatter = NSNumberFormatter()
yAxis.valueFormatter.minimumFractionDigits = 0
but the problem here is I am getting duplicate yAxis values - 0, 1, 2, 2, 3, 4 and 5.
How this can be eliminated?
These are the issues I am facing. I tried setting
barChart.leftAxis.axisMaximum = 5.0
barChart.leftAxis.axisMinimum = 0.0
barChart.leftAxis.customAxisMin = 0
barChart.leftAxis.customAxisMax = 5.0
but not helping any suggestions?
This cannot be avoided, because you don't allow fraction digit.
minimumFractionDigits = 0 means no decimal if possible, like 2.1 and 2.0, they will result in 2.1 and 2 ( if maximumFractionDigits = 1)
You need to give the information what's the raw value and what's the value of maximumFractionDigits
EDIT:
the y axis label is calculated by default via internal func computeAxisValues
it calculate the interval based on your max and min value, and the label count.
if _yAxis.isForceLabelsEnabled is enabled, then it will read labelCount and just use (max-min)/(labelCount-1) to calculate the interval. It is disabled by default.
When isForceLabelsEnabled is false, it has a small algorithm to calculate what's the proper way to get a good looking number.
Take a look at internal func computeAxisValues to find out what meets your need.
Update for your second part of question:
you should not touch axisMaximum nor min
enable forceLabelsEnabled
change yAxis.labelCount to 6 or 5 or whatever you like
enable xAxis.wordWrapEnabled

Resources