{"id":2919,"date":"2021-05-27T20:06:50","date_gmt":"2021-05-27T12:06:50","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2919"},"modified":"2021-05-27T20:06:50","modified_gmt":"2021-05-27T12:06:50","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%a38%ef%bc%88%e6%8a%98%e7%ba%bf%e5%9b%be7%ef%bc%9a%e4%ba%8b%e4%bb%b6%e5%93%8d%e5%ba%94%e3%80%81mark","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2919","title":{"rendered":"Swift &#8211; \u7b2c\u4e09\u65b9\u56fe\u8868\u5e93Charts\u4f7f\u7528\u8be6\u89e38\uff08\u6298\u7ebf\u56fe7\uff1a\u4e8b\u4ef6\u54cd\u5e94\u3001MarkerView\u6807\u7b7e\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-96-1024x327.png\" alt=\"\" class=\"wp-image-2920\" width=\"808\" height=\"258\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-1024x327.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-300x96.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-768x245.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-1536x490.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-830x265.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-230x73.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-350x112.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96-480x153.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-96.png 1610w\" sizes=\"(max-width: 808px) 100vw, 808px\" \/><\/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-97-1024x697.png\" alt=\"\" class=\"wp-image-2921\" width=\"778\" height=\"529\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97-1024x697.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97-300x204.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97-768x523.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97-830x565.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97-230x157.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97-350x238.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97-480x327.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-97.png 1492w\" sizes=\"(max-width: 778px) 100vw, 778px\" \/><\/figure><\/div>\n\n\n\n<p>\uff082\uff09\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    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        }\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 \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     \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        \/\/\u663e\u793a\u8be5\u70b9\u7684MarkerView\u6807\u7b7e\n        self.showMarkerView(value: \"\\(entry.y)\")\n    }\n     \n    \/\/\u663e\u793aMarkerView\u6807\u7b7e\n    func showMarkerView(value:String){\n        let marker = MarkerView(frame: CGRect(x: 20, y: 20, width: 80, height: 20))\n        marker.chartView = self.chartView\n        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 80, height: 20))\n        label.text = \"\u6570\u636e\uff1a\\(value)\"\n        label.textColor = UIColor.white\n        label.font = UIFont.systemFont(ofSize: 12)\n        label.backgroundColor = UIColor.gray\n        label.textAlignment = .center\n        marker.addSubview(label)\n        self.chartView.marker = marker\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     \n    \/\/\u56fe\u8868\u901a\u8fc7\u624b\u52bf\u7f29\u653e\u540e\u7684\u56de\u8c03\n    func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {\n        print(\"\u56fe\u8868\u7f29\u653e\u4e86\")\n    }\n     \n    \/\/\u56fe\u8868\u901a\u8fc7\u624b\u52bf\u62d6\u52a8\u540e\u7684\u56de\u8c03\n    func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat) {\n       print(\"\u56fe\u8868\u79fb\u52a8\u4e86\")\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-98-1024x640.png\" alt=\"\" class=\"wp-image-2922\" width=\"800\" height=\"500\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-1024x640.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-300x187.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-768x480.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-1536x959.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-830x518.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-230x144.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-350x219.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98-480x300.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-98.png 1614w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/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-99-1024x175.png\" alt=\"\" class=\"wp-image-2923\" width=\"787\" height=\"134\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-1024x175.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-300x51.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-768x131.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-1536x262.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-830x142.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-230x39.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-350x60.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99-480x82.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-99.png 1606w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/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 Foundation\nimport Charts\n \nopen class BalloonMarker: MarkerImage\n{\n    open var color: UIColor\n    open var arrowSize = CGSize(width: 15, height: 11)\n    open var font: UIFont\n    open var textColor: UIColor\n    open var insets: UIEdgeInsets\n    open var minimumSize = CGSize()\n     \n    fileprivate var label: String?\n    fileprivate var _labelSize: CGSize = CGSize()\n    fileprivate var _paragraphStyle: NSMutableParagraphStyle?\n    fileprivate var _drawAttributes = [NSAttributedStringKey : AnyObject]()\n     \n    public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)\n    {\n        self.color = color\n        self.font = font\n        self.textColor = textColor\n        self.insets = insets\n         \n        _paragraphStyle = NSParagraphStyle.default.mutableCopy()\n            as? NSMutableParagraphStyle\n        _paragraphStyle?.alignment = .center\n        super.init()\n    }\n     \n    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint\n    {\n        var offset = self.offset\n        var size = self.size\n         \n        if size.width == 0.0 &amp;&amp; image != nil\n        {\n            size.width = image!.size.width\n        }\n        if size.height == 0.0 &amp;&amp; image != nil\n        {\n            size.height = image!.size.height\n        }\n         \n        let width = size.width\n        let height = size.height\n        let padding: CGFloat = 8.0\n \n        var origin = point\n        origin.x -= width \/ 2\n        origin.y -= height\n \n        if origin.x + offset.x &lt; 0.0\n        {\n            offset.x = -origin.x + padding\n        }\n        else if let chart = chartView,\n            origin.x + width + offset.x > chart.bounds.size.width\n        {\n            offset.x = chart.bounds.size.width - origin.x - width - padding\n        }\n         \n        if origin.y + offset.y &lt; 0\n        {\n            offset.y = height + padding;\n        }\n        else if let chart = chartView,\n            origin.y + height + offset.y > chart.bounds.size.height\n        {\n            offset.y = chart.bounds.size.height - origin.y - height - padding\n        }\n         \n        return offset\n    }\n     \n    open override func draw(context: CGContext, point: CGPoint)\n    {\n        guard let label = label else { return }\n         \n        let offset = self.offsetForDrawing(atPoint: point)\n        let size = self.size\n         \n        var rect = CGRect(\n            origin: CGPoint(\n                x: point.x + offset.x,\n                y: point.y + offset.y),\n            size: size)\n        rect.origin.x -= size.width \/ 2.0\n        rect.origin.y -= size.height\n         \n        context.saveGState()\n         \n        context.setFillColor(color.cgColor)\n \n        if offset.y > 0\n        {\n            context.beginPath()\n            context.move(to: CGPoint(\n                x: rect.origin.x,\n                y: rect.origin.y + arrowSize.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + (rect.size.width - arrowSize.width) \/ 2.0,\n                y: rect.origin.y + arrowSize.height))\n            \/\/arrow vertex\n            context.addLine(to: CGPoint(\n                x: point.x,\n                y: point.y))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + (rect.size.width + arrowSize.width) \/ 2.0,\n                y: rect.origin.y + arrowSize.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + rect.size.width,\n                y: rect.origin.y + arrowSize.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + rect.size.width,\n                y: rect.origin.y + rect.size.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x,\n                y: rect.origin.y + rect.size.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x,\n                y: rect.origin.y + arrowSize.height))\n            context.fillPath()\n        }\n        else\n        {\n            context.beginPath()\n            context.move(to: CGPoint(\n                x: rect.origin.x,\n                y: rect.origin.y))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + rect.size.width,\n                y: rect.origin.y))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + rect.size.width,\n                y: rect.origin.y + rect.size.height - arrowSize.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + (rect.size.width + arrowSize.width) \/ 2.0,\n                y: rect.origin.y + rect.size.height - arrowSize.height))\n            \/\/arrow vertex\n            context.addLine(to: CGPoint(\n                x: point.x,\n                y: point.y))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x + (rect.size.width - arrowSize.width) \/ 2.0,\n                y: rect.origin.y + rect.size.height - arrowSize.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x,\n                y: rect.origin.y + rect.size.height - arrowSize.height))\n            context.addLine(to: CGPoint(\n                x: rect.origin.x,\n                y: rect.origin.y))\n            context.fillPath()\n        }\n \n        if offset.y > 0 {\n            rect.origin.y += self.insets.top + arrowSize.height\n        } else {\n            rect.origin.y += self.insets.top\n        }\n \n        rect.size.height -= self.insets.top + self.insets.bottom\n         \n        UIGraphicsPushContext(context)\n         \n        label.draw(in: rect, withAttributes: _drawAttributes)\n         \n        UIGraphicsPopContext()\n         \n        context.restoreGState()\n    }\n     \n    \/**\n    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)\n    {\n        setLabel(String(entry.y))\n    }\n     **\/\n     \n    open func setLabel(_ newLabel: String)\n    {\n        label = newLabel\n         \n        _drawAttributes.removeAll()\n        _drawAttributes[.font] = self.font\n        _drawAttributes[.paragraphStyle] = _paragraphStyle\n        _drawAttributes[.foregroundColor] = self.textColor\n         \n        _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero\n         \n        var size = CGSize()\n        size.width = _labelSize.width + self.insets.left + self.insets.right\n        size.height = _labelSize.height + self.insets.top + self.insets.bottom\n        size.width = max(minimumSize.width, size.width)\n        size.height = max(minimumSize.height, size.height)\n        self.size = size\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-100.png\" alt=\"\" class=\"wp-image-2925\" width=\"677\" height=\"110\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-100.png 858w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-100-300x49.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-100-768x125.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-100-830x135.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-100-230x38.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-100-350x57.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-100-480x78.png 480w\" sizes=\"(max-width: 677px) 100vw, 677px\" \/><\/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=\"\">\/\/\u663e\u793aMarkerView\u6807\u7b7e\nfunc 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}<\/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-101-1024x706.png\" alt=\"\" class=\"wp-image-2926\" width=\"754\" height=\"520\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-1024x706.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-300x207.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-768x529.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-1536x1059.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-830x572.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-230x159.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-350x241.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101-480x331.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/05\/image-101.png 1570w\" sizes=\"(max-width: 754px) 100vw, 754px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\uff082\uff09\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\/2919"}],"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=2919"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2919\/revisions"}],"predecessor-version":[{"id":2927,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2919\/revisions\/2927"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}