[Swift通天遁地]3、手勢與圖表-(8)製做股市中經常使用的蠟燭圖表

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-cdbciivj-ks.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目錄:[Swift]通天遁地Swiftios

本文將演示如何製做股市中經常使用的蠟燭圖表。git

首先確保在項目中已經安裝了所需的第三方庫。github

點擊【Podfile】,查看安裝配置文件。swift

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'Charts'
7 end

根據配置文件中的相關配置,安裝第三方庫。數組

而後點擊打開【DemoApp.xcworkspace】項目文件。微信

爲了更好的顯示柱形圖標須要調整模擬器的朝向。app

【DemoApp】->【General】ide

->【Device Orientation】取消勾選【Portrait】肖像選項,使模擬器保持橫向顯示。post

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】

 1 import UIKit
 2 //首先在當前的類文件中,引入已經安裝的第三方類庫
 3 import Charts
 4 
 5 class ViewController: UIViewController {
 6 
 7     override func viewDidLoad() {
 8         super.viewDidLoad()
 9         // Do any additional setup after loading the view, typically from a nib.
10         
11         //建立一個包含6個數字的數組,做爲一天中的股市高位的數據
12         let highs: [Double] = [1801.6, 1762.1, 1740.9, 1726.7, 1725.9, 1736.2]
13         //建立一個包含6個數字的數組,做爲一天中的股市低位的數據
14         let lows: [Double] = [1750.0, 1718.8, 1715.0, 1695.5, 1706.7, 1722.7]
15         //建立一個包含6個數字的數組,做爲一天中的股市開市時的數據
16         let opens: [Double] = [1801.4, 1750.8, 1725.8, 1722.6, 1706.7, 1727,4]
17         //建立一個包含6個數字的數組,做爲一天中的股市結束時的數據
18         let closes: [Double] = [1752.6, 1724.0, 1720.6, 1704.5, 1724.5, 1725.4]
19 
20         //在蠟燭圖中,將使用這些數據,展現6天的股市數據。
21         
22         //建立一個圖表數據記錄類型的數組
23         var entries1: [ChartDataEntry] = Array()
24         //經過一個6次的循環
25         for (i, value) in highs.enumerated()
26         {
27             //將四個數組中的數字,添加到圖表數據記錄中。
28             entries1.append(CandleChartDataEntry(x: Double(i)+1, shadowH: value, shadowL: lows[i], open: opens[i], close: closes[i]))
29         }
30         
31         //建立第一個蠟燭圖表數據集,並設置數據集的值域和標籤文字
32         let dataSet = CandleChartDataSet(values: entries1, label: "Company A")
33         //設置數據集的填充顏色
34         dataSet.setColor(NSUIColor(red: 229/255, green: 140/255, blue: 154/255, alpha: 1))
35         //設置蠟燭的間距爲0.3
36         dataSet.barSpace = 0.3
37         //設置蠟燭的負增加顏色爲綠色
38         dataSet.decreasingColor = NSUIColor.green
39         //設置蠟燭的增加顏色爲紅色
40         dataSet.increasingColor = NSUIColor.red
41         //設置蠟燭的中性顏色爲藍色
42         dataSet.neutralColor = NSUIColor.blue
43         //設置蠟燭的陰影顏色爲黑色
44         dataSet.shadowColor = NSUIColor.black
45         //設置陰影寬度爲5.0
46         dataSet.shadowWidth = 5.0
47         
48         //建立一個指定顯示區域
49         let rect = CGRect(x: 0, y: 10, width: 560, height: 280)
50         //利用該區域,建立一個蠟燭圖表視圖
51         let chart = CandleStickChartView(frame: rect)
52         //設置圖表視圖的背景顏色爲無色
53         chart.backgroundColor = NSUIColor.clear
54         //設置圖表所顯示的數據內容
55         chart.data = CandleChartData(dataSets: [dataSet])
56         //設置圖表中的數值的最大可視數量爲200
57         chart.maxVisibleCount = 200
58         //設置水平座標軸的標籤位置
59         //共有:頂部、底部、雙側、頂部內側、底部內側五種。
60         chart.xAxis.labelPosition = .bottom
61         //設置圖表的描述信息
62         chart.chartDescription?.text = "https://www.cnblogs.com/strengthen/"
63         
64         //將配置好的圖表,添加到根視圖中。
65         self.view.addSubview(chart)
66     }
67 
68     override func didReceiveMemoryWarning() {
69         super.didReceiveMemoryWarning()
70         // Dispose of any resources that can be recreated.
71     }
72 }
相關文章
相關標籤/搜索