{"id":2935,"date":"2021-05-27T20:15:09","date_gmt":"2021-05-27T12:15:09","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2935"},"modified":"2021-05-27T20:15:09","modified_gmt":"2021-05-27T12:15:09","slug":"swift-%e7%ac%ac%e4%b8%89%e6%96%b9%e5%9b%be%e8%a1%a8%e5%ba%93charts%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a310%ef%bc%88%e6%8a%98%e7%ba%bf%e5%9b%be9%ef%bc%9a%e8%87%aa%e5%8a%a8%e6%98%be%e7%a4%ba%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2935","title":{"rendered":"Swift &#8211; \u7b2c\u4e09\u65b9\u56fe\u8868\u5e93Charts\u4f7f\u7528\u8be6\u89e310\uff08\u6298\u7ebf\u56fe9\uff1a\u81ea\u52a8\u663e\u793a\u4e2d\u95f4\u62d0\u70b9\u6807\u7b7e\u3001\u6ed1\u52a8\u5c45\u4e2d\uff09"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/05\/image-107-1024x682.png\" alt=\"\" class=\"wp-image-2936\" width=\"737\" height=\"490\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-1024x682.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-300x200.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-768x512.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-1536x1023.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-830x553.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-230x153.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-350x233.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107-480x320.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-107.png 1564w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/05\/image-108-1024x185.png\" alt=\"\" class=\"wp-image-2937\" width=\"791\" height=\"142\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-1024x185.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-300x54.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-768x139.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-1536x277.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-830x150.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-230x42.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-350x63.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108-480x87.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-108.png 1584w\" sizes=\"(max-width: 791px) 100vw, 791px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport Charts\n \nclass ViewController: UIViewController {\n     \n    \/\/\u6298\u7ebf\u56fe\n    var chartView: LineChartView!\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u521b\u5efa\u6298\u7ebf\u56fe\u7ec4\u4ef6\u5bf9\u8c61\n        chartView = LineChartView()\n        chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40,\n                                 height: 250)\n        chartView.delegate = self \/\/\u8bbe\u7f6e\u4ee3\u7406\n        self.view.addSubview(chartView)\n         \n        \/\/\u8bbe\u7f6e\u4ea4\u4e92\u6837\u5f0f\n        chartView.scaleXEnabled = false \/\/\u5141\u53d6\u6d88X\u8f74\u7f29\u653e\n        chartView.scaleYEnabled = false \/\/\u53d6\u6d88Y\u8f74\u7f29\u653e\n        chartView.doubleTapToZoomEnabled = false \/\/\u53cc\u51fb\u7f29\u653e\n         \n        \/\/\u751f\u6210100\u6761\u968f\u673a\u6570\u636e\n        var dataEntries = [ChartDataEntry]()\n        for i in 0..&lt;100 {\n            let y = arc4random()%100\n            let entry = ChartDataEntry.init(x: Double(i), y: Double(y))\n            dataEntries.append(entry)\n        }\n        \/\/\u8fd950\u6761\u6570\u636e\u4f5c\u4e3a1\u6839\u6298\u7ebf\u91cc\u7684\u6240\u6709\u6570\u636e\n        let chartDataSet = LineChartDataSet(values: dataEntries, label: \"\u5ba2\u6d41\u91cf\uff08\u5929\uff09\")\n        \/\/\u76ee\u524d\u6298\u7ebf\u56fe\u53ea\u5305\u62ec1\u6839\u6298\u7ebf\n        let chartData = LineChartData(dataSets: [chartDataSet])\n         \n        \/\/\u8bbe\u7f6e\u6298\u7ebf\u56fe\u6570\u636e\n        chartView.data = chartData\n         \n        \/\/\u56fe\u8868\u6700\u591a\u663e\u793a10\u4e2a\u70b9\n        chartView.setVisibleXRangeMaximum(10)\n        \/\/\u9ed8\u8ba4\u663e\u793a\u6700\u540e\u4e00\u4e2a\u6570\u636e\n        chartView.moveViewToX(99)\n        \/\/\u81ea\u52a8\u9009\u4e2d\u56fe\u8868\u4e2d\u592e\u7684\u6570\u636e\u70b9\n        highlightCenterValue()\n    }\n     \n    \/\/\u81ea\u52a8\u9009\u4e2d\u56fe\u8868\u4e2d\u592e\u7684\u6570\u636e\u70b9\n    func highlightCenterValue() {\n        \/\/\u83b7\u53d6\u4e2d\u70b9\u5750\u6807\n        let x = Double(chartView.bounds.width\/2)\n        let selectionPoint = CGPoint(x: x, y: 0)\n        \/\/\u83b7\u53d6\u6700\u63a5\u8fd1\u4e2d\u70b9\u4f4d\u7f6e\u7684\u6570\u636e\u70b9\n        let h = chartView.getHighlightByTouchPoint(selectionPoint)\n        \/\/\u5c06\u8fd9\u4e2a\u6570\u636e\u70b9\u9ad8\u4eae\uff08\u540c\u65f6\u81ea\u52a8\u8c03\u7528 chartValueSelected \u8fd9\u4e2a\u4ee3\u7406\u65b9\u6cd5\uff09\n        chartView.highlightValue(h, callDelegate: true)\n    }\n     \n    \/\/\u663e\u793aMarkerView\u6807\u7b7e\n    func showMarkerView(value:String){\n        \/\/\u4f7f\u7528\u6c14\u6ce1\u72b6\u7684\u6807\u7b7e\n        let marker = BalloonMarker(color: UIColor(white: 180\/255, alpha: 1),\n                        font: .systemFont(ofSize: 12),\n                        textColor: .white,\n                        insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8))\n        marker.chartView = self.chartView\n        marker.minimumSize = CGSize(width: 80, height: 40)\n        marker.setLabel(\"\u6570\u636e\uff1a\\(value)\")\n        self.chartView.marker = marker\n    }\n}\n \nextension ViewController: ChartViewDelegate {\n    \/\/\u56fe\u8868\u901a\u8fc7\u624b\u52bf\u62d6\u52a8\u540e\u7684\u56de\u8c03\n    func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat) {\n        \/\/\u81ea\u52a8\u9009\u4e2d\u56fe\u8868\u4e2d\u592e\u7684\u6570\u636e\u70b9\n        highlightCenterValue()\n    }\n     \n    \/\/\u6298\u7ebf\u4e0a\u7684\u70b9\u9009\u4e2d\u56de\u8c03\n    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry,\n                            highlight: Highlight) {\n        \/\/\u663e\u793a\u8be5\u70b9\u7684MarkerView\u6807\u7b7e\n        self.showMarkerView(value: \"\\(entry.y)\")\n    }\n}<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/05\/image-109-1024x586.png\" alt=\"\" class=\"wp-image-2938\" width=\"735\" height=\"420\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-1024x586.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-300x172.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-768x439.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-1536x879.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-830x475.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-230x132.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-350x200.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109-480x275.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-109.png 1608w\" sizes=\"(max-width: 735px) 100vw, 735px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2935"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2935"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2935\/revisions"}],"predecessor-version":[{"id":2939,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2935\/revisions\/2939"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}