{"id":1625,"date":"2022-06-04T21:49:10","date_gmt":"2022-06-04T12:49:10","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=1625"},"modified":"2022-06-04T21:52:41","modified_gmt":"2022-06-04T12:52:41","slug":"ios_swift_healthdata_with_charts","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2022\/06\/04\/ios_swift_healthdata_with_charts\/","title":{"rendered":"iOS Swift \u30b0\u30e9\u30d5\u306e\u8868\u793a\u3068\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u306e\u3067\u3001\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf(\u4f53\u91cd)\u306e\u30b0\u30e9\u30d5\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b"},"content":{"rendered":"<p>\u30b0\u30e9\u30d5\u306e\u8868\u793a\u3068\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u306e\u3067\u3001\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf(\u4f53\u91cd)\u306e\u30b0\u30e9\u30d5\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/05\/30\/swift_ios_chart\/\">\u30c1\u30e3\u30fc\u30c8\u306e\u7d44\u307f\u8fbc\u307f<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/05\/31\/ios_swift_healthkit\/\">HealthKit\u306e\u8a2d\u5b9a<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/06\/04\/swift_ios_healthkit_healthdata\/\">HealthKit\u304b\u3089\u4f53\u91cd\u30c7\u30fc\u30bf\u53d6\u5f97<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_chart.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"healthkit_chart.gif\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_chart.gif\" alt=\"healthkit_chart\" width=\"233\" height=\"408\" border=\"0\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_charts.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"healthkit_charts.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/06\/healthkit_charts.png\" alt=\"healthkit_charts\" width=\"232\" height=\"411\" border=\"0\" \/><\/a><\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9<\/p>\n<p>\u30b0\u30e9\u30d5\u306e\u671f\u9593\u306a\u3069\u8ab2\u984c\u3002<\/p>\n<p>X\u8ef8\u306e\u30e9\u30d9\u30eb\u306f\u3001self.lineChartView.xAxis.valueFormatter \u306b \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u8f9e\u66f8\u3092\u308f\u305f\u3059\u3002<\/p>\n<pre class=\"prettyprint\">import HealthKit\nimport UIKit\nimport Charts\nimport TinyConstraints\n\nclass BodyMassViewController: UIViewController {\n    lazy var lineChartView: LineChartView = {\n        let charView = LineChartView()\n            charView.backgroundColor = .white\n            charView.animate(yAxisDuration: 1.5)\n            return charView\n        }()\n    \n    var healthStore : HKHealthStore?\n    \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \/\/ Do any additional setup after loading the view.\n        \n        if HKHealthStore.isHealthDataAvailable() {\n            healthStore = HKHealthStore()\n        }\n        \n        view.addSubview(lineChartView)\n        lineChartView.centerInSuperview()\n        lineChartView.width(to: view)\n        lineChartView.heightToWidth(of: view)\n        setData()\n    }\n    func setData() {\n        let start = Calendar.current.date(byAdding: .month, value: -48, to: Date())\n        let end = Date()\n        let predicate = HKQuery.predicateForSamples(withStart: start, end: end)\n        let sampleType = HKQuantityType.quantityType(forIdentifier: .bodyMass)!\n        \n        let query = HKSampleQuery(\n            sampleType: sampleType,\n            predicate: predicate,\n            limit: HKObjectQueryNoLimit,\n            sortDescriptors: nil) {\n            (query, results, error) in\n            \n            let samples = results as! [HKQuantitySample]\n                \n            var yValues: [ChartDataEntry] = []\n            var xValues: [String] = []\n            var x = 0.0\n            \n            let formatter = DateFormatter()\n            formatter.dateFormat = \"yy\/M\/d\"\n                \n            for sample in samples {\n                let y = sample.quantity.doubleValue(for: .gram()) \/ 1000\n                let ds = ChartDataEntry(x: x, y: y)\n                yValues.append(ds)\n                xValues.append(formatter.string(from: sample.startDate))\n                x += 1\n            }\n            \n            DispatchQueue.main.async {\n                let set1 = LineChartDataSet(entries: yValues, label: \"BodyMass\")\n                let data = LineChartData(dataSet: set1)\n                self.lineChartView.data = data\n                self.lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xValues)\n            }\n        }\n        self.healthStore?.execute(query)\n    \n    }\n    \n    @IBAction func onDismiss(_ sender: Any) {\n        self.navigationController?.dismiss(animated: true)\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30b0\u30e9\u30d5\u306e\u8868\u793a\u3068\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u306e\u3067\u3001\u30d8\u30eb\u30b9\u30c7\u30fc\u30bf(\u4f53\u91cd)\u306e\u30b0\u30e9\u30d5\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b\u3002 \u30c1\u30e3\u30fc\u30c8\u306e\u7d44\u307f\u8fbc\u307f HealthKit\u306e\u8a2d\u5b9a HealthKit\u304b\u3089\u4f53\u91cd\u30c7\u30fc\u30bf\u53d6\u5f97 \u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9 \u30b0\u30e9\u30d5\u306e\u671f\u9593\u306a\u3069 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1624,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[127,126],"tags":[191,192,187,130],"class_list":["post-1625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-xcode","tag-chart","tag-healthkit","tag-ios","tag-swift"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1625"}],"version-history":[{"count":3,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1625\/revisions"}],"predecessor-version":[{"id":1630,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1625\/revisions\/1630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/1624"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}