{"id":2861,"date":"2021-05-27T17:46:23","date_gmt":"2021-05-27T09:46:23","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2861"},"modified":"2021-05-27T17:46:23","modified_gmt":"2021-05-27T09:46:23","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%a34%ef%bc%88%e6%8a%98%e7%ba%bf%e5%9b%be3%ef%bc%9a%e9%80%89%e4%b8%ad%e7%82%b9%e9%ab%98%e4%ba%ae","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2861","title":{"rendered":"Swift &#8211; \u7b2c\u4e09\u65b9\u56fe\u8868\u5e93Charts\u4f7f\u7528\u8be6\u89e34\uff08\u6298\u7ebf\u56fe3\uff1a\u9009\u4e2d\u70b9\u9ad8\u4eae\u3001\u5341\u5b57\u7ebf\u6837\u5f0f\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-55-1024x848.png\" alt=\"\" class=\"wp-image-2862\" width=\"749\" height=\"620\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55-1024x848.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55-300x248.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55-768x636.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55-830x687.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55-230x190.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55-350x290.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55-480x397.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-55.png 1430w\" sizes=\"(max-width: 749px) 100vw, 749px\" \/><\/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-56-1024x692.png\" alt=\"\" class=\"wp-image-2863\" width=\"732\" height=\"494\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56-1024x692.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56-300x203.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56-768x519.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56-830x561.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56-230x155.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56-350x237.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56-480x324.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-56.png 1376w\" sizes=\"(max-width: 732px) 100vw, 732px\" \/><\/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-57-1024x563.png\" alt=\"\" class=\"wp-image-2864\" width=\"747\" height=\"410\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-1024x563.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-300x165.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-768x422.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-1536x845.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-830x456.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-230x126.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-350x192.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57-480x264.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-57.png 1586w\" sizes=\"(max-width: 747px) 100vw, 747px\" \/><\/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-58-1024x626.png\" alt=\"\" class=\"wp-image-2865\" width=\"739\" height=\"451\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58-1024x626.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58-300x184.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58-768x470.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58-830x508.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58-230x141.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58-350x214.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58-480x294.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-58.png 1494w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/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-59-1024x623.png\" alt=\"\" class=\"wp-image-2866\" width=\"753\" height=\"458\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-1024x623.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-300x182.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-768x467.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-1536x934.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-830x505.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-230x140.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-350x213.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59-480x292.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-59.png 1760w\" sizes=\"(max-width: 753px) 100vw, 753px\" \/><\/figure><\/div>\n\n\n\n<p>2\uff0c\u6837\u4f8b\u4ee3\u7801<\/p>\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, ChartViewDelegate {\n     \n    \/\/\u6298\u7ebf\u56fe\n    var chartView: LineChartView!\n     \n    \/\/\u6240\u6709\u70b9\u7684\u989c\u8272\n    var circleColors: [UIColor] = []\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: 270)\n        chartView.delegate = self \/\/\u8bbe\u7f6e\u4ee3\u7406\n        self.view.addSubview(chartView)\n         \n        \/\/\u751f\u621010\u6761\u968f\u673a\u6570\u636e\n        var dataEntries = [ChartDataEntry]()\n        for i in 0..&lt;10 {\n            let y = arc4random()%100\n            let entry = ChartDataEntry.init(x: Double(i), y: Double(y))\n            dataEntries.append(entry)\n            \/\/\u6bcf\u4e2a\u70b9\u9ed8\u8ba4\u989c\u8272\u90fd\u662f\u84dd\u8272\n            circleColors.append(.cyan)\n        }\n         \n        \/\/\u8fd910\u6761\u6570\u636e\u4f5c\u4e3a1\u6839\u6298\u7ebf\u91cc\u7684\u6240\u6709\u6570\u636e\n        let chartDataSet = LineChartDataSet(values: dataEntries, label: \"\u56fe\u4f8b1\")\n        \/\/\u76ee\u524d\u6298\u7ebf\u56fe\u53ea\u5305\u62ec1\u6839\u6298\u7ebf\n        let chartData = LineChartData(dataSets: [chartDataSet])\n        \/\/\u8bbe\u7f6e\u6298\u70b9\u989c\u8272\n        chartDataSet.circleColors = circleColors\n \n        \/\/\u8bbe\u7f6e\u6298\u73b0\u56fe\u6570\u636e\n        chartView.data = chartData\n    }\n     \n    \/\/\u6298\u7ebf\u4e0a\u7684\u70b9\u9009\u4e2d\u56de\u8c03\n    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry,\n                            highlight: Highlight) {\n        print(\"\u9009\u4e2d\u4e86\u4e00\u4e2a\u6570\u636e\")\n         \n        \/\/\u5c06\u9009\u4e2d\u7684\u6570\u636e\u70b9\u7684\u989c\u8272\u6539\u6210\u9ec4\u8272\n        var chartDataSet = LineChartDataSet()\n        chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)!\n        let values = chartDataSet.values\n        let index = values.index(where: {$0.x == highlight.x})  \/\/\u83b7\u53d6\u7d22\u5f15\n        chartDataSet.circleColors = circleColors \/\/\u8fd8\u539f\n        chartDataSet.circleColors[index!] = .orange\n         \n        \/\/\u91cd\u65b0\u6e32\u67d3\u8868\u683c\n        chartView.data?.notifyDataChanged()\n        chartView.notifyDataSetChanged()\n    }\n     \n    \/\/\u6298\u7ebf\u4e0a\u7684\u70b9\u53d6\u6d88\u9009\u4e2d\u56de\u8c03\n    func chartValueNothingSelected(_ chartView: ChartViewBase) {\n        print(\"\u53d6\u6d88\u9009\u4e2d\u7684\u6570\u636e\")\n         \n        \/\/\u8fd8\u539f\u6240\u6709\u70b9\u7684\u989c\u8272\n        var chartDataSet = LineChartDataSet()\n        chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)!\n        chartDataSet.circleColors = circleColors\n         \n        \/\/\u91cd\u65b0\u6e32\u67d3\u8868\u683c\n        chartView.data?.notifyDataChanged()\n        chartView.notifyDataSetChanged()\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2\uff0c\u6837\u4f8b\u4ee3\u7801<\/p>\n","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\/2861"}],"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=2861"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2861\/revisions"}],"predecessor-version":[{"id":2867,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2861\/revisions\/2867"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}