- This topic has 3개 답변, 3명 참여, and was last updated 4 years, 6 months 전에 by 정재 이.
-
글쓴이글
-
-
정재 이참가자
- 글작성 : 4
- 답글작성 : 11
코드는 아래와 같이 구성하였습니다. 저는 2개의 데이터가 데이터셋에 들어가면 자연스럽게 2개의 데이터만 그래프에 나올줄 알았는데, 범위로 나와버리내요… 왜 이런 현상이 생길까요? x축이 왜 위에 나타나는지도 모르겠습니다.. 저는 아래에 나타나고 싶은데….
import UIKit import Charts class BodyChartViewController: UIViewController,ChartViewDelegate,UITextViewDelegate,UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return Data.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CELL", for: indexPath) as! CustomBodychartCell cell.date = Data[indexPath.row][0] cell.heiht = Data[indexPath.row][1] cell.weight = Data[indexPath.row][2] let w = first_suso_cut(number: Float(cell.weight)!) let h = first_suso_cut(number: Float(cell.heiht)!) //cell.Label.textColor = UIColor.white cell.Label.font = UIFont.boldSystemFont(ofSize: 10) cell.Label.text = "(cell.date): (h)cm / (w)kg" return cell } @IBOutlet weak var Height: UITextView! @IBOutlet weak var NowWeight: UITextView! @IBOutlet weak var RecordButton: UIButton! @IBOutlet weak var Chart: LineChartView! @IBOutlet weak var Table: UITableView! @IBOutlet weak var Label: UILabel! var HeightTmpString : String = "" var NowWeightTmpString : String = "" let DB = DataBaseAPI.init() let Query = DataBaseQuery.init() var Data : Array<Array<String>> = [] var Dates : Array<String> = [] var Weights : Array<Double> = [] var yVals1 : [ChartDataEntry] = <a>ChartDataEntry</a> weak var axisFormatDelegate: IAxisValueFormatter? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let navview = Variable_Functions.init() self.navigationItem.titleView = navview.navView //뷰 배경 설정 self.view.backgroundColor = UIColor.systemGray3 //테이블 뷰 설정 self.Table.dataSource = self self.Table.delegate = self self.Table.rowHeight = UITableView.automaticDimension //self.Table.allowsSelection = false //버튼설정 self.RecordButton.backgroundColor = UIColor.init(rgb:0x5AC18E) self.RecordButton.tintColor = UIColor.white self.RecordButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15) //텍스트뷰 설정 self.Height.delegate = self self.NowWeight.delegate = self self.Height.font = UIFont.boldSystemFont(ofSize: 15) self.NowWeight.font = UIFont.boldSystemFont(ofSize: 15) initialize_textview() //데이터 업데이트 update_data() self.axisFormatDelegate = self setupChart() } func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if text == "\n" { textView.resignFirstResponder() return false } return true } func textViewDidBeginEditing(_ textView: UITextView) { textView.textColor = UIColor.black textView.text = "" } func textViewDidEndEditing(_ textView: UITextView) { if textView == Height{ HeightTmpString = Height.text if let HeightFloat = Float(Height.text!){ Height.text = " (first_suso_cut(number: HeightFloat)) cm" } if HeightTmpString == ""{ Height.textColor = UIColor.gray Height.text = "키" } } else if textView == NowWeight{ NowWeightTmpString = NowWeight.text if let NowWeightFloat = Float(NowWeight.text!){ NowWeight.text = " (first_suso_cut(number: NowWeightFloat)) kg" } if NowWeightTmpString == ""{ NowWeight.textColor = UIColor.gray NowWeight.text = "몸무게" } } } func initialize_textview(){ Height.textColor = UIColor.gray Height.text = "키" Height.textAlignment = NSTextAlignment.center NowWeight.textColor = UIColor.gray NowWeight.text = "몸무게" NowWeight.textAlignment = NSTextAlignment.center } func first_suso_cut(number : Float) -> String{ return String(format: "%.1f", number) } func update_label(){ } func get_date(inputdatestring : String) -> String{ var date = "" for i in inputdatestring{ if i != "-"{ date.append(i) } } return date } func update_data(){ self.Data = DB.query(statement: "SELECT * from record order by checked_date;", ColumnNumber: 3) Dates.removeAll() Weights.removeAll() for each in Data{ Dates.append(each[0]) Weights.append( Double( Float(each[2])!) ) } print("------------") print(Data,Dates,Weights) print("------------") } func setupChart(){ //self.Chart.descriptionText = "Speed" //self.Chart.descriptionTextColor = UIColor.black self.Chart.gridBackgroundColor = UIColor.darkGray self.Chart.noDataText = "데이터가 없습니다" self.Chart.rightAxis.enabled = false self.Chart.drawGridBackgroundEnabled = false self.Chart.doubleTapToZoomEnabled = false self.Chart.xAxis.drawGridLinesEnabled = false self.Chart.xAxis.drawAxisLineEnabled = false self.Chart.rightAxis.drawGridLinesEnabled = false self.Chart.rightAxis.drawAxisLineEnabled = false self.Chart.leftAxis.drawGridLinesEnabled = false self.Chart.leftAxis.drawAxisLineEnabled = false self.Chart.xAxis.labelFont = UIFont.systemFont(ofSize: 5) self.Chart.backgroundColor = UIColor.white self.Chart.animate(yAxisDuration: 0.1) //self.Chart.xAxis.valueFormatter = XAxisValueFormatter as IAxisValueFormatter setChartData(xValsArr: self.Dates, yValsArr: self.Weights) } func setChartData(xValsArr: [String], yValsArr: [Double]) { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone? yVals1.removeAll() for (index, item) in xValsArr.enumerated() { let Date = formatter.date(from: item) yVals1.append(ChartDataEntry.init(x: Date!.timeIntervalSince1970, y: yValsArr[index] ) ) print(yVals1) } print(xValsArr,yValsArr) let set1: LineChartDataSet = LineChartDataSet(entries: yVals1, label: "First Set") //set설정 set1.axisDependency = .left // Line will correlate with left axis values set1.setColor(UIColor.red.withAlphaComponent(0.5)) // our line's opacity is 50% set1.drawCirclesEnabled = false set1.lineWidth = 0.5 set1.circleRadius = 6.0 // the radius of the node circle set1.fillAlpha = 65 / 255.0 set1.fillColor = UIColor.red set1.drawFilledEnabled = false set1.highlightColor = UIColor.white set1.drawCircleHoleEnabled = true let LineChartData1 = LineChartData.init(dataSet: set1 as IChartDataSet ) Chart.xAxis.valueFormatter = axisFormatDelegate //data.setValueTextColor(UIColor.whiteColor()) //self.Chart.xAxis.valueFormatter = self //5 - finally set our data self.Chart.data = LineChartData1 //Chart.xAxis.valueFormatter = axisFormatDelegate self.Chart.reloadInputViews() } @IBAction func RecordButtonTaped(_ sender: Any) { initialize_textview() if let Weightfloat = Float(NowWeightTmpString) , let Heightfloat = Float(HeightTmpString){ //오늘날짜 let today = Date() let format = DateFormatter() format.dateFormat = "yyyy-MM-dd" let today_string = format.string(from: today) let value = "'(today_string)' , '(first_suso_cut(number: Heightfloat) )', '(first_suso_cut(number: Weightfloat))'" //디비입력 if DB.insert(statement: Query.insert(Tablename: "Record", Values: value)){ update_data() update_label() self.Table.reloadData() setChartData(xValsArr: self.Dates, yValsArr: self.Weights) } }else{ self.Label.text = "잘못된 입력입니다. 다시 입력하여 주세요." } } @IBAction func Delete(_ sender: Any) { update_data() update_label() setChartData(xValsArr: self.Dates, yValsArr: self.Weights) //Chart.reloadInputViews() Table.reloadData() } } extension BodyChartViewController: IAxisValueFormatter { func stringForValue(_ value: Double, axis: AxisBase?) -> String { let date = Date(timeIntervalSince1970: value) let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone? return formatter.string(from: date) } } class CustomBodychartCell : UITableViewCell { let DB = DataBaseAPI.init() let Query = DataBaseQuery.init() var date : String = "" var heiht : String = "" var weight : String = "" @IBOutlet weak var Label: UILabel! @IBOutlet weak var DeleteButton: UIButton! @IBAction func Delete(_ sender: Any) { if DB.delete(statement: Query.Delete(Tablename: "Record", Condition: "checked_date = '(date)' AND height = '(heiht)' AND weight = '(weight)'")){ print("Success to delete record table") }else{ print("fail to delete record table") } } }2020-06-04 오전 1:22 #8725 -
멍단비참가자
- 글작성 : 10
- 답글작성 : 98
써본지 한 1년되서 잘 기억은 나질 않긴한데, 아래 링크보니까 요렇게 해주면 되는가봐요.
저거 아니어도 워낙에 많이쓰는 라이브러리라 검색해보시거나 이것저것 하나씩 설정해서
빌드해보시면 다른 원하시는 기능도 금방 찾으실 수 있을거에요~
lineChartView.xAxis.labelPosition = XAxis.LabelPosition.bottomhttps://github.com/danielgindi/Charts/issues/727
- 이 답변은 멍단비에 의해 4 years, 6 months 전에 수정됐습니다.
2020-06-04 오전 9:56 #8737 -
야곰키 마스터
- 글작성 : 37
- 답글작성 : 579
저는 이 라이브러리를 사용해보지 않아서 멍단비님 답변으로 해결이 잘 될런지는 모르겠지만, 꼰대질(…) 하나 하자면 학생이나 혼자 코드를 작성할 때는 문제 없지만, 취업을 목표로 한다거나 협업을 해야하는 상황이라면 이름짓기 습관을 잘 들여야합니다.
지금은 어디는 카멜케이스, 어디는 축약형, 어디는 언더바를 사용한 이름 등이 있죠. 물론 예제코드고 연습용 코드지만 연습할 때 습관을 안들여 놓으면 프로젝트 할때고 실절에서고 잘 안되거든요^^;
한 번 습관들이는 것을 연습해보면 나중에는 의식하지 않아도 잘 될겁니다. 이름짓기 정말로 중요한 습관입니다. 다른 현업자분들은 어떻게 생각하실지 모르겠지만 저는 이름짓는 습관이 기능하나 더 구현하는 것보다 백만배 중요하다고 생각합니다. 한 번 관련된 내용 확인해보세요 🙂하아… 근데 이런 꼰대짓 하면 안된다는거 잘 아는데 잘 안되네요…ㅠㅠ
2020-06-04 오후 12:44 #8742
-
-
글쓴이글
- 답변은 로그인 후 가능합니다.