ios charts integer convert - ios

I'm using ios Charts 3.0 on the objc project. I had searched the answers on the stackoverflow but no correct results for me. I'm using horizon bar chart and pie chart in my project and tried using
yAxis.valueFormatter = NSNumberFormatter()
yAxis.valueFormatter.minimumFractionDigits = 0
for every axis as well as the custom formatter with this :
- (NSString *)stringForValue:(double)value
axis:(ChartAxisBase *)axis
{
return [#((NSInteger)value) stringValue];
}
Both ways did not work for me. Could you guys please help to resolve this?

Try the code below. I wrote it at Swift and don't know much Objective-C. I hope it helps. I'm using this for BarChart and it works like a charm.
let fmt = NumberFormatter()
fmt.numberStyle = .decimal
fmt.maximumFractionDigits = 0
fmt.groupingSeparator = ","
fmt.decimalSeparator = "."
yAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: fmt)

You need to extend IValueFormatter protocol with custom formatter class,
import Foundation
import Charts
// Custom value formatter class
class BarValueFormatter : NSObject, IValueFormatter {
// This method is called when a value (from labels inside the chart) is formatted before being drawn.
func stringForValue(_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String {
let digitWithoutFractionValues = String(format: "%.0f", value)
return digitWithoutFractionValues
}
}
Now in your code initialise bar value formatter using custom class. It will remove all precision/franction values.
let chartDataSet = BarChartDataSet(values: <dataSetValues>, label: <stringValue>)
let valueFormatter = BarValueFormatter()
chartDataSet.valueFormatter = valueFormatter

Related

How to use String values for X-Axis in Charts instead of doubles?

I want to display String values for X-Axis in line chart via using 'Charts'
I've followed tutorial for same here. As per wrote there let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet) I'm not finding in current latest version of library.
In, current library version's demo code I'm only able to fill double type of data for X-axis and Y-axis.
Please help me to solve this.Here is desired output.
There's a protocol IAxisValueFormatter that you can implement and achieve your expected result.
EDIT:
How to use it
While initialising
chartView.xAxis.valueFormatter = self
and implementing protocol.
extension LineChart1ViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let months = ["January","February","March","April","May","June","July","Auguest","September","October","November","December"]
return months[Int(value)]
}
}

How to convert smileys to emoticon?

Is there a way to convert ":)" to 😊 like : detection and conversion, I have a UITextView in a chat application which should convert the smiley to the respective emoticon.
Get the offical list: emoji
Just an example: yourTextView.text = "your smiley: \u{1f642}"
If you want to convert emoticons to emoji on the fly either you need to specify it by yourself and analyze the input string or using a 3rd-party lib e.g. pods for converting and watching input string through text change events e.g.: docs
You can use the logic in this package npm, you find also a map of smile and the respective emoji:
https://www.npmjs.com/package/smile2emoji
I have created this simple class based on the npm package suggested by #emish89 https://www.npmjs.com/package/smile2emoji.
https://gist.github.com/lorenzOliveto/f20a89e9f68276cae21497a177ad8a4c
Swift 5
You should implement delegate textViewDidChange for your UITextView and find all need substrings in its text then replace them inside with textStorage property:
extension ViewController : UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
guard let text = textView.text else { return }
var index = text.startIndex
while let range = text.range(of: ":)", range: index..<text.endIndex) {
index = range.upperBound
textView.textStorage.replaceCharacters(in: NSRange(range, in: text), with: "😀")
}
}
}
It works while editing or paste text.

Swift 3, accessing user input Text Field values from ViewControler and used them in a model .swift file

This is my first question. I'm new to swift and programming in general so please don't laugh if I'm asking stupid question :)
So I hava a SettingsViewControler where users sets their values, lets say they set the temperature value. What i'm trying to do, is to take that temperature value that they input and pass it to my model.swift file, to introduce that value in the formula, to calculate the new value with the input temperature. I hope this make sense.
Is there a way to do that directly by calling the class form VC to the newData class that I created in model.swift file, or I should use some methods like UserDefaults to pass data.
Here is the code example:
First I created a Settings.swift file
// Settings.swift file
import Foundation
class Settings {
var inputTemperature: Float = 0
init(inputTemperature: Float) {
self.inputTemperature = inputTemperature
}
}
Here is the Settings View Controller
//Settings ViewCOntroller. swift file
import UIKit
class SettingsViewController: UIViewController {
#IBOutlet weak var inputTemperatureTextField: UITextField!
var getTemp = Settings(inputTemperature: 72)
override func viewDidLoad() {
super.viewDidLoad()
}
func getValues() {
getTemp.inputTemperature = (inputTemperatureTextField.text! as NSString).floatValue
}
}else if textField == inputTemperatureTextField {
textField.resignFirstResponder()
getValues()
}
So now I have another Calculations.swift file where I want to get the inputTemperature value to use it in the formula
//Calculation. swift file
import Foundation
class Calculations {
var inputA: Float = 0
var inputB: Float = 0
var resultC: Float = 0
init(inputA: Float, inputB: Float) {
self.inputA = inputA
self.inputB = inputB
}
// Here i want to add the temperature value
func calc() {
resutC = inputA * inputC // * inputTemperature
}
I want the get (inputTemperatureTextField.text! as NSString).floatValue value from SettingsView COntroller to introduce it in the formula located Calculation.swift file
Thanks
You should really post some of your code to give us some insight in what exactly it is you want.
Lets say in your model you have a temperature value like
var temp: Int?
Then you can initialize it in your VC, and access the temp value
var model = Model()
let model.temp = inputTextField.text
If you are using model.swift for calculation. You will create an object of the model-class in the View controller, after creating an object you can pass the test field value to the model.
var modelObject: model? = model();
modelObject.temprature = txtTemprature.text;

Disable future dates selection in FScalendar swift

I am using https://github.com/WenchaoD/FSCalendar in my project . MaximumSelectedDate is a read-only property .Then how can disable future dates ?
You should be using the delegate method to address this
func maximumDate(for calendar: FSCalendar) -> Date {
return Date()
}
A workaround could be to edit FSCalendar method file. First make a bool variable, say isAllowedToLimitFutureDates and a string variable maxValidFutureDateAsString then change line 172 of this link to:
if(!isAllowedToLimitFutureDates)
{
_maximumDate = [self.formatter dateFromString:#"2099-12-31"];
}
else
{
_maximumDate = maxValidFutureDateAsString; // say "2017-03-13"
}
So when you want to limit the dates set isAllowedToLimitFutureDates = true.
Similar approach to line 1707.
In case you cannot edit file and used PODs, then you can customize this control and override them.
Hope that helps!
#Devraj answer is correct, there are delegates for both minimum and maximum dates, all you need to do is implementing the proper one (the later one in your case) in the controller that's conforming to FSCalendarDelegate and that'll do the trick.
func maximumDateForCalendar(calendar: FSCalendar) -> NSDate {
return NSDate() // NSDate of your choosing here
}
for Swift 3
fileprivate lazy var dateFormatter2: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
return formatter }()
let today = dateFormatter2.string(from: calendar.selectedDate!)
let dateObj = dateFormatter2.date(from: today)
if dateObj! > calendar.today! {
// Your logic here
}

iOS Charts custom labels: integer values on Bubble Chart

I want to show the values displayed inside the bubbles as whole number Ints. For example: instead of "22.0" I just want "22".
The answer here doesn't work with the new iOS Charts because it requires an IValueFormatter instead of a NumberFormatter:
let numberFormatter = NumberFormatter()
numberFormatter.generatesDecimalNumbers = false
chartData.setValueFormatter(numberFormatter)
Error Message:
Cannot convert value of type 'NumberFormatter' to expected argument type 'IValueFormatter?'
Is there any way to solve this problem?
With the new charts, you still use numberformatter and then just convert at the end. Reference the code below.
let format = NumberFormatter()
format.generatesDecimalNumbers = false
let formatter = DefaultValueFormatter(formatter: format)
chartData.leftAxis.valueFormatter = (formatter as? IAxisValueFormatter)
Hope this helps!

Resources