0
Finding data on chart
The user will make data inputs in textfield which meets x and y axis. Is there a way to take these two inputs and find their interaection on existing line chart and tell the user if that intersection is over or below that line chart line?
18 Answers
+ 7
y axis
4 +
3
2 x
1 +
0 Jan Feb March x axis
Assume above line chart with (Feb,2) marked already
Now if any value comes like
(Feb,4) or (Feb,1)
You just need to compare values of y with original to tell if above or below
// Assuming x is discrete
+ 6
You can share via Google Drive or copy paste code on Sololearn playground and save it . Then link it here. By the way what on your x axis and y axis?
+ 5
If user enters between 1-2 still you can compare But I don't understand what is between Feb and March?(What value is between Feb and March)
Feb month represent all values in month of Feb I.e from 1-28
+ 5
Can you make docs public
+ 1
thank you but i already read this, the thing i am trying to do is finding data on line chart not making a new chart. there is a line chart and user will enter data which represents x an y axis then ios will mark and tell if the intersection of these two datas is over or below the chart
+ 1
thank you i’ll try it
+ 1
on x axis theres hours beginnin from 0 to 166 increasing by 12, and on y axis theres numbers from 0 to 21 increasing by 1.
x axis = 0, 12, 24...166
y axis = 0,1,2...21 the problem is
if the input in x axis is 13 and in y axis is 1.2 how am i gonna calculate the intersection of these two and tell the user if it is over or below the line chart
+ 1
which command line will i use? first i suppouse it can be solved with an if statement but i cant
+ 1
i am a pediatrician and i am at hospital right now, but when i get home i’ll share the code and also the chart
Rstar thank you so much for your responses
+ 1
sure, i did it now
0
still couldnt manage to do
because it is ok if the user selects (Feb,1) or (Feb,4) but what if selects values between Feb and March and between 1-2. e.g (Feb8, 1.2) and additin to these should i use the if statement to declare it or sth else
0
i wanna show my code but i dont know how to post in here
0
so i can ask more clearly
0
Created a textfield and a date picker
textfield data matches the y axis
and the date picker value is amount of hours calculated by ios using birthdate matches x axis
Code below is for generating chart
@IBAction func displayChart(_ sender: Any) {
let chartConfig = ChartConfigXY(
xAxisConfig: ChartAxisConfig(from: 0, to:192 , by: 24),
yAxisConfig: ChartAxisConfig(from: 0, to: 22, by: 1)
)
let frame = CGRect(x: 10, y: 200, width: self.view.frame.width-50, height: 400)
let chart = LineChart(frame: frame, chartConfig: chartConfig, xTitle: "Postnatal Yaş Saat", yTitle: "Total Bilirubin mg/dl" ,
lines: [
(chartPoints:[(0, 4.0), (12, 6.0), (24, 8.0), (36, 9.5), (48, 11.0), (60, 12.5), (72, 13.5), (84, 14.0), (96, 14.5), (108, 14.9), (120, 15.0), (132, 15.0), (144, 15.0), (156, 15.0), (168, 15.0)], color: UIColor.red),
(chartPoints:[(0, 5.0), (12, 7.5), (24, 10.0), (36, 11.8), (48, 13.0), (60, 14.5), (72, 15.1), (84, 16.4), (96, 17.1), (108, 18.0), (120, 18.0), (120, 18.0), (132, 18.0), (144, 18.0), (156, 18.0), (168, 18.0)], color : UIColor.blue),
(chartPoints:[(0, 6.5), (12, 9.0), (24, 11.5), (36, 13.5), (48, 15.0), (60, 16.5), (72, 17.5), (84, 19.0), (96, 20.0), (108, 20.5), (120, 21.0), (132, 21.0), (144, 21.0), (156, 21.0), (168, 21.0)], color: UIColor.green)
])
self.view.addSubview(chart.view)
self.pttChartView = chart
0
code for date picker
func createDatePicker(){
postNatalAgeTextField.inputView = datePicker
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector (doneClicked))
toolbar.setItems([doneButton], animated: true)
postNatalAgeTextField.inputAccessoryView = toolbar
}
@objc func doneClicked(){
self.view.endEditing(true)
let birthDate = self.datePicker.date
let today = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour], from: birthDate, to: today)
let ageHours = components.hour
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale(identifier: "tr")
postNatalAgeTextField.text = dateFormatter.string(from: datePicker.date)
self.postNatalAgeLabel.text = "\(ageHours!) saat"
if birthDate >= today{
let alertView = UIAlertController(title: "Hata!", message: "Lütfen geçerli bir doğum tarihi giriniz. ", preferredStyle: .alert)
let action = UIAlertAction(title: "Tamam", style: .default, handler: nil)
alertView.addAction(action)
self.present(alertView, animated: false, completion: nil)
return
}
0
And the textfield for y axis
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let oldText = textField.text, let r = Range(range, in: oldText) else {
return true
}
let newText = oldText.replacingCharacters(in: r, with: string)
let isNumeric = newText.isEmpty || (Double(newText) != nil)
let numberOfDots = newText.components(separatedBy: ".").count - 1
let numberOfDecimalDigits: Int
if let dotIndex = newText.index(of: ".") {
numberOfDecimalDigits = newText.distance(from: dotIndex, to: newText.endIndex) - 1
} else {
numberOfDecimalDigits = 0
}
return isNumeric && numberOfDots <= 1 && numberOfDecimalDigits <= 2
}
0
https://docs.google.com/document/d/1j5Pf7vyknX3FdQxQRGcxk5wQcsMpenxn_rJq_VRnFGQ/edit
This is the linechart. Please help me with the code to find the inputs' intersection. x axis is hours, y axis is serum bilirubin level. If the number in the textfield, is 5.2, and hours is 11 how swift will find and tell the intersection.