Preface: Today to share a very beautiful powerful chart library, I hope that the students who need help, like to please a praise, support. Thank you!
How to join the Swift library in a project see my previous article
Http://www.jianshu.com/p/fd91c10c9f55
Compilation Environment: Xcode7.3
Add Charts Chart Library
// 在Podfile中‘Charts‘
import Charts
Create bar chart
func createLineChartView() { chartView = BarChartView.init(frame: CGRectMake(06464)) chartView.barData // 签协议 chartView.delegate = self chartView.backgroundColor = UIColor.whiteColor() self.view.addSubview(chartView) }
Analog Data Assignment
Func Setchart (datapoints: [String], values: [Double]) {varDataentries: [Barchartdataentry] = [] forI in0.. <datapoints.count {Let dataEntry = Barchartdataentry (Value:values[i], xindex:i) DATAENTRIES.A Ppend (DataEntry)} Let Chartdataset = Barchartdataset (yvals:dataentries, Label:"Units Sold") Let ChartData = Barchartdata (xvals:months, Dataset:chartdataset)//Add a boundary to show the red line in the diagramLet JX = Chartlimitline (limit:12.0, Label:"I am Limitline") ChartView.rightAxis.addLimitLine (JX) Chartview.data = ChartData//Custom Colors There are 12 bars in the//example //Colors is an array that can give the corresponding colorChartdataset.colors = [Uicolor.bluecolor (), Uicolor.redcolor (), Uicolor.cyancolor ()]//API comes with color template //Chartcolortemplates.liberty () //chartcolortemplates.joyful () //Chartcolortemplates.pastel () //chartcolortemplates.colorful () //Chartcolortemplates.vordiplom ()Chartdataset.colors = Chartcolortemplates.liberty ()/**//animation effect, a simple list of several, specifically see the API case easeinback case easeoutback case Easeinoutback Case easeinbounce Case easeoutbounce Case Easeinoutbounce * / //Add animationChartview.animate (yaxisduration:1.0, Easingoption:. Easeinbounce)}
You can also save the current chart state, write a Save button
func Createrightbarbuttonitem () {Let buttonright = Uibutton.init (Type:uibuttontype) Buttonright.frame = CGRectMake (0< /span>, 0 , 40 , 40 ) Self.navigationitem = Uibarbuttonitem (customview:buttonright) buttonright.s Ettitle ( "Save" , Forstate:uicontrolstate . Normal ) Buttonright.addtarget (self, Action: #selector (Viewcontroller.save (_:)), forControlEvents:UIControlEvents.TouchUpInside) }< /code>
Click on the method to add the following code
save(btn: UIButton) { // 保存到相册 chartView.saveToCameraRoll() print("保存成功") }
You may also want to implement different click events by clicking on a single column
You can achieve chartviewdelegate
class ViewController: UIViewController, ChartViewDelegate {// 在创建的时候签协议// chartView.delegate = self// 实现协议方法func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) { print("\(entry.value) in \(months[entry.xIndex])") }
This creates a line chart
chartView = LineChartView.init(frame: CGRectMake(0, 64, screenWidth, screenHeight - 64))
The analog data assignment is changed to the following code
var dataEntries: [ChartDataEntry] = [] forin0..<dataPoints.count { let dataEntry = ChartDataEntry(value: values[i], xIndex: i) dataEntries.append(dataEntry) } let"Units Sold") let chartData = LineChartData(xVals: months, dataSet: chartDataSet)
Summary: The icon supports zoom click, here I only give two styles of icon code example, details please see the official API and demo please click
Considering the article may be confusing some places are not clear I will put on GitHub address, easy to learn download view please click
Swift's powerful chart library-charts uses