{"id":2982,"date":"2021-05-27T20:41:15","date_gmt":"2021-05-27T12:41:15","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2982"},"modified":"2021-05-27T20:41:15","modified_gmt":"2021-05-27T12:41:15","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%a316%ef%bc%88%e6%9f%b1%e7%8a%b6%e5%9b%be3%ef%bc%9a%e5%a4%9a%e7%bb%84%e6%95%b0%e6%8d%ae%e7%9a%84","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2982","title":{"rendered":"Swift &#8211; \u7b2c\u4e09\u65b9\u56fe\u8868\u5e93Charts\u4f7f\u7528\u8be6\u89e316\uff08\u67f1\u72b6\u56fe3\uff1a\u591a\u7ec4\u6570\u636e\u7684\u663e\u793a\uff09"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"794\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/05\/image-141-1024x794.png\" alt=\"\" class=\"wp-image-2984\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-1024x794.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-300x232.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-768x595.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-1536x1190.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-830x643.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-230x178.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-350x271.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141-480x372.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-141.png 1582w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/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    \/\/\u67f1\u72b6\u56fe\u56fe\n    var chartView: BarChartView!\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u521b\u5efa\u6298\u7ebf\u56fe\u7ec4\u4ef6\u5bf9\u8c61\n        chartView = BarChartView()\n        chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40,\n                                 height: 260)\n        self.view.addSubview(chartView)\n         \n        \/\/\u7b2c\u4e00\u7ec4\u6570\u636e\n        var dataEntries1 = [BarChartDataEntry]()\n        for i in 0..&lt;10 {\n            let y = arc4random()%100 + 50\n            let entry = BarChartDataEntry(x: Double(i), y: Double(y))\n            dataEntries1.append(entry)\n        }\n        let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: \"\u56fe\u4f8b1\")\n         \n        \/\/\u7b2c\u4e8c\u7ec4\u6570\u636e\n        var dataEntries2 = [BarChartDataEntry]()\n        for i in 0..&lt;10 {\n            let y = arc4random()%50\n            let entry = BarChartDataEntry(x: Double(i), y: Double(y))\n            dataEntries2.append(entry)\n        }\n        let chartDataSet2 = BarChartDataSet(values: dataEntries2, label: \"\u56fe\u4f8b2\")\n        chartDataSet2.colors = [.orange]\n         \n        \/\/\u76ee\u524d\u67f1\u72b6\u56fe\u5305\u62ec2\u7ec4\u7acb\u67f1\n        let chartData = BarChartData(dataSets: [chartDataSet1, chartDataSet2])\n         \n        \/\/\u8bbe\u7f6e\u67f1\u72b6\u56fe\u6570\u636e\n        chartView.data = chartData\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-142-1024x784.png\" alt=\"\" class=\"wp-image-2985\" width=\"704\" height=\"539\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-1024x784.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-300x230.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-768x588.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-1536x1177.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-830x636.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-230x176.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-350x268.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142-480x368.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-142.png 1564w\" sizes=\"(max-width: 704px) 100vw, 704px\" \/><\/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    \/\/\u67f1\u72b6\u56fe\u56fe\n    var chartView: BarChartView!\n     \n    \/\/\u6bcf\u4e2a\u5206\u7ec4\u4e4b\u95f4\u7684\u95f4\u9694\n    let groupSpace = 0.31\n     \n    \/\/\u540c\u4e00\u5206\u7ec4\u5185\u67f1\u5b50\u95f4\u9694\n    let barSpace = 0.03\n     \n    \/\/\u67f1\u5b50\u5bbd\u5ea6\uff08 (0.2 + 0.03) * 3 + 0.31 = 1.00 -> interval per \"group\"\uff09\n    let barWidth = 0.2\n     \n    \/\/\u6bcf\u7ec4\u6570\u636e\u6761\u6570\n    let groupCount = 5\n     \n    \/\/x\u8f74\u6807\u7b7e\u6587\u5b57\n    let years = [\"2001\", \"2002\", \"2003\", \"2004\", \"2005\"]\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u521b\u5efa\u6298\u7ebf\u56fe\u7ec4\u4ef6\u5bf9\u8c61\n        chartView = BarChartView()\n        chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40,\n                                 height: 260)\n        self.view.addSubview(chartView)\n         \n        \/\/\u7b2c\u4e00\u7ec4\u6570\u636e\n        var dataEntries1 = [BarChartDataEntry]()\n        for i in 0..&lt;groupCount {\n            let y = arc4random()%100 + 50\n            let entry = BarChartDataEntry(x: Double(i), y: Double(y))\n            dataEntries1.append(entry)\n        }\n        let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: \"\u56fe\u4f8b1\")\n         \n        \/\/\u7b2c\u4e8c\u7ec4\u6570\u636e\n        var dataEntries2 = [BarChartDataEntry]()\n        for i in 0..&lt;groupCount {\n            let y = arc4random()%50\n            let entry = BarChartDataEntry(x: Double(i), y: Double(y))\n            dataEntries2.append(entry)\n        }\n        let chartDataSet2 = BarChartDataSet(values: dataEntries2, label: \"\u56fe\u4f8b2\")\n        chartDataSet2.colors = [.orange]\n         \n        \/\/\u7b2c\u4e09\u7ec4\u6570\u636e\n        var dataEntries3 = [BarChartDataEntry]()\n        for i in 0..&lt;groupCount {\n            let y = arc4random()%50\n            let entry = BarChartDataEntry(x: Double(i), y: Double(y))\n            dataEntries3.append(entry)\n        }\n        let chartDataSet3 = BarChartDataSet(values: dataEntries3, label: \"\u56fe\u4f8b3\")\n        chartDataSet3.colors = [.green]\n         \n        \/\/\u76ee\u524d\u67f1\u72b6\u56fe\u5305\u62ec2\u7ec4\u7acb\u67f1\n        let chartData = BarChartData(dataSets: [chartDataSet1, chartDataSet2,\n                                                chartDataSet3])\n         \n        \/\/\u8bbe\u7f6e\u67f1\u5b50\u5bbd\u5ea6\n        chartData.barWidth = barWidth\n         \n        \/\/\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff08\u4e0d\u91cd\u53e0\u663e\u793a\uff09\n        chartData.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace)\n         \n        \/\/\u8bbe\u7f6eX\u8f74\u8303\u56f4\n        chartView.xAxis.axisMinimum = Double(0)\n        chartView.xAxis.axisMaximum = Double(0) + chartData\n            .groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(groupCount)\n        chartView.xAxis.centerAxisLabelsEnabled = true  \/\/\u6587\u5b57\u6807\u7b7e\u5c45\u4e2d\n        chartView.xAxis.granularity = 1\n        chartView.xAxis.labelPosition = .bottom\n        chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:self.years)\n         \n        \/\/\u8bbe\u7f6ey\u8f74\u8303\u56f4\n        chartView.leftAxis.axisMinimum = 0\n        chartView.rightAxis.axisMinimum = 0\n         \n        \/\/\u8bbe\u7f6e\u67f1\u72b6\u56fe\u6570\u636e\n        chartView.data = chartData\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-143-1024x589.png\" alt=\"\" class=\"wp-image-2986\" width=\"785\" height=\"451\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143-1024x589.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143-300x173.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143-768x442.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143-830x478.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143-230x132.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143-350x201.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143-480x276.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-143.png 1456w\" sizes=\"(max-width: 785px) 100vw, 785px\" \/><\/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    \/\/\u67f1\u72b6\u56fe\u56fe\n    var chartView: BarChartView!\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u521b\u5efa\u6298\u7ebf\u56fe\u7ec4\u4ef6\u5bf9\u8c61\n        chartView = BarChartView()\n        chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40,\n                                 height: 260)\n        \/\/\u6587\u5b57\u663e\u793a\u5728\u67f1\u5b50\u5185\u90e8\n        chartView.drawValueAboveBarEnabled = false\n        self.view.addSubview(chartView)\n         \n        \/\/\u751f\u621010\u6761\u968f\u673a\u6570\u636e\n        var dataEntries = [BarChartDataEntry]()\n        for i in 0..&lt;10 {\n            \/\/\u6bcf\u4e2a\u67f1\u5b50\u7531\u4e24\u90e8\u5206\u6570\u636e\u7ec4\u6210\n            let value1 = Double(arc4random()%100)\n            let value2 = Double(arc4random()%100)\n            let entry = BarChartDataEntry(x: Double(i), yValues: [value1, value2])\n            dataEntries.append(entry)\n        }\n         \n        \/\/\u8fd910\u6761\u6570\u636e\u4f5c\u4e3a\u67f1\u72b6\u56fe\u7684\u6240\u6709\u6570\u636e\n        let chartDataSet = BarChartDataSet(values: dataEntries, label: \"\u6bcf\u65e5\u8bbf\u95ee\u91cf\")\n        \/\/\u5806\u53e0\u67f1\u5f62\u56fe\u6bcf\u90e8\u5206\u6587\u5b57\u6807\u7b7e\n        chartDataSet.stackLabels = [\"\u7ebf\u4e0a\", \"\u7ebf\u4e0b\"]\n        \/\/\u5806\u53e0\u67f1\u5f62\u56fe\u6bcf\u90e8\u5206\u4f7f\u7528\u7684\u989c\u8272\n        chartDataSet.colors = [ChartColorTemplates.material()[0],\n                               ChartColorTemplates.material()[1]]\n         \n        \/\/\u76ee\u524d\u67f1\u72b6\u56fe\u53ea\u5305\u62ec1\u7ec4\u7acb\u67f1\n        let chartData = BarChartData(dataSets: [chartDataSet])\n        \/\/\u6807\u7b7e\u6587\u5b57\u989c\u8272\u4e3a\u767d\u8272\n        chartData.setValueTextColor(.white)\n         \n        \/\/\u8bbe\u7f6e\u67f1\u72b6\u56fe\u6570\u636e\n        chartView.data = chartData\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-144-1024x544.png\" alt=\"\" class=\"wp-image-2987\" width=\"692\" height=\"367\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144-1024x544.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144-300x159.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144-768x408.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144-830x441.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144-230x122.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144-350x186.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144-480x255.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-144.png 1486w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/><\/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    \/\/\u67f1\u72b6\u56fe\u56fe\n    var chartView: BarChartView!\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u521b\u5efa\u6298\u7ebf\u56fe\u7ec4\u4ef6\u5bf9\u8c61\n        chartView = BarChartView()\n        chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40,\n                                 height: 260)\n        \/\/\u6587\u5b57\u663e\u793a\u5728\u67f1\u5b50\u5185\u90e8\n        chartView.drawValueAboveBarEnabled = false\n        self.view.addSubview(chartView)\n         \n        \/\/\u751f\u621010\u6761\u968f\u673a\u6570\u636e\n        var dataEntries = [BarChartDataEntry]()\n        for i in 0..&lt;10 {\n            \/\/\u6bcf\u4e2a\u67f1\u5b50\u7531\u4e24\u90e8\u5206\u6570\u636e\u7ec4\u6210\n            let value1 = Double(arc4random()%100)\n            let value2 = -Double(arc4random()%100)\n            let entry = BarChartDataEntry(x: Double(i), yValues: [value1, value2])\n            dataEntries.append(entry)\n        }\n         \n        \/\/\u8fd910\u6761\u6570\u636e\u4f5c\u4e3a\u67f1\u72b6\u56fe\u7684\u6240\u6709\u6570\u636e\n        let chartDataSet = BarChartDataSet(values: dataEntries, label: \"\u6bcf\u65e5\u4ea4\u6613\")\n        \/\/\u5806\u53e0\u67f1\u5f62\u56fe\u6bcf\u90e8\u5206\u6587\u5b57\u6807\u7b7e\n        chartDataSet.stackLabels = [\"\u6536\u5165\", \"\u652f\u51fa\"]\n        \/\/\u5806\u53e0\u67f1\u5f62\u56fe\u6bcf\u90e8\u5206\u4f7f\u7528\u7684\u989c\u8272\n        chartDataSet.colors = [ChartColorTemplates.material()[0],\n                               ChartColorTemplates.material()[1]]\n         \n        \/\/\u76ee\u524d\u67f1\u72b6\u56fe\u53ea\u5305\u62ec1\u7ec4\u7acb\u67f1\n        let chartData = BarChartData(dataSets: [chartDataSet])\n        \/\/\u6807\u7b7e\u6587\u5b57\u989c\u8272\u4e3a\u767d\u8272\n        chartData.setValueTextColor(.white)\n         \n        \/\/\u8bbe\u7f6e\u67f1\u72b6\u56fe\u6570\u636e\n        chartView.data = chartData\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-145-1024x623.png\" alt=\"\" class=\"wp-image-2988\" width=\"696\" height=\"423\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145-1024x623.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145-300x182.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145-768x467.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145-830x505.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145-230x140.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145-350x213.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145-480x292.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-145.png 1296w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/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\/2982"}],"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=2982"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2982\/revisions"}],"predecessor-version":[{"id":2989,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2982\/revisions\/2989"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}