#9 Data Visualization 練習
Published in
6 min readAug 5, 2021
學習使用 API 製作圖表
圓餅圖 (Pic Chart)
圖中顯示標普500前十大公司的成分佔比,可以看到500間公司的前十大公司竟然佔比高達28.8%,足以說明強者恆強,更重要的是,此圖只能顯示8個項目,目前功力不足,還無法調整成能完整顯示
(數據統計至2021/08/04,股票漲跌會影響個股權重)
makePieChart()addWedge(withProportion: 0.062, color: .red)addWedge(withProportion: 0.058, color: .blue)addWedge(withProportion: 0.039, color: .green)addWedge(withProportion: 0.022, color: .lightGray)addWedge(withProportion: 0.022, color: .cyan)addWedge(withProportion: 0.021, color: .yellow)addWedge(withProportion: 0.015, color: .orange)addWedge(withProportion: 0.014, color: .magenta)addWedge(withProportion: 0.013, color: .purple)addWedge(withProportion: 0.012, color: .gray)addWedge(withProportion: 0.722, color: .black)addKeyItem(withLabel: "AAPL", color: .red)addKeyItem(withLabel: "MSFT", color: .blue)addKeyItem(withLabel: "AMZN", color: .green)addKeyItem(withLabel: "FB", color: .lightGray)addKeyItem(withLabel: "GOOGL", color: .cyan)addKeyItem(withLabel: "GOOG", color: .yellow)addKeyItem(withLabel: "TSLA", color: .orange)addKeyItem(withLabel: "BRK.B", color: .magenta)addKeyItem(withLabel: "NVDA", color: .purple)addKeyItem(withLabel: "JPM", color: .gray)addKeyItem(withLabel: "The others", color: .black)
長條圖 (Bar Chart)
統計世界使用人數前八的語言,單位為億
makeBarChart()setYAxis(minimum: 0, maximum: 13)addBar(withLength: 12.68, color: .yellow)addBar(withLength: 11.12, color: .green)addBar(withLength: 6.373, color: .red)addBar(withLength: 5.379, color: .purple)addBar(withLength: 2.766, color: .gray)addBar(withLength: 2.74, color: .blue)addBar(withLength: 2.652, color: .orange)addBar(withLength: 2.58, color: .brown)addBarLabel("英語")addBarLabel("漢語")addBarLabel("印地語")addBarLabel("西班牙語")addBarLabel("法語")addBarLabel("阿拉伯語")addBarLabel("孟加拉語")addBarLabel("俄語")
散佈圖 (Scatter Plot)
統計某班級的同學身高體重,黑點為女生,紅點為男生,可得知男生普遍身高較高,女生較矮,而體重則與性別無正相關(實際上使用散佈圖統計時,收集資料需30組以上更有參考價值)
makePlot()setXAxis(minimum: 40, maximum: 80)setYAxis(minimum: 150, maximum: 190)addPointAt(x: 50, y: 160, color: .black)addPointAt(x: 45, y: 155, color: .black)addPointAt(x: 55, y: 165, color: .black)addPointAt(x: 65, y: 163, color: .black)addPointAt(x: 70, y: 180, color: .red)addPointAt(x: 50, y: 175, color: .red)addPointAt(x: 53, y: 167, color: .red)addPointAt(x: 54, y: 173, color: .red)addPointAt(x: 65, y: 183, color: .red)