Skip to content

Instantly share code, notes, and snippets.

@touyu
Last active February 1, 2016 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save touyu/3f520f9e511ef879b005 to your computer and use it in GitHub Desktop.
Save touyu/3f520f9e511ef879b005 to your computer and use it in GitHub Desktop.
[Swift] [iOS] チャート表示ライブラリ [ios-charts] ref: http://qiita.com/touyu/items/4fbd6d8187eb74752ba0
platform :ios, '9.0'
pod 'Charts'
use_frameworks!
@IBOutlet var barChartView: BarChartView!
import UIKit
import Charts
class ViewController: UIViewController {
// storyboardから接続
@IBOutlet weak var barChartView: BarChartView!
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
months = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
let unitsSold = [50.3, 68.3, 113.3, 115.7, 160.8, 214.0, 220.4, 132.1, 176.2, 120.9, 71.3, 48.0]
barChartView.animate(yAxisDuration: 2.0)
barChartView.pinchZoomEnabled = false
barChartView.drawBarShadowEnabled = false
barChartView.drawBordersEnabled = true
barChartView.descriptionText = "京都府の月毎の降水量グラフ"
setChart(months, values: unitsSold)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "降水量")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
barChartView.data = chartData
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment