Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active July 3, 2018 08:00
Show Gist options
  • Select an option

  • Save lamprosg/9473993b43ee65197800b0a1b8bf12aa to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/9473993b43ee65197800b0a1b8bf12aa to your computer and use it in GitHub Desktop.
(iOS) Chart line with Charts
//In case we had to load a view from a xib
//In case we had a nib for the chart
override public func awakeAfter(using aDecoder: NSCoder) -> Any? {
let isPlaceHolder:Bool = self.subviews.count == 0
if isPlaceHolder {
let lineChartView:MyLineChartView? = Bundle.main.loadNibNamed("LineChartViewNibName", owner: nil, options: nil)?.first as? MyLineChartView
lineChartView?.autoresizingMask = self.autoresizingMask;
lineChartView?.translatesAutoresizingMaskIntoConstraints = self.translatesAutoresizingMaskIntoConstraints;
lineChartView?.lines = self.lines
return lineChartView
}
return self;
import UIKit
public class MyChartLineModel {
var yValues:[Double]?
var xValues:[Double]?
var lineColor:UIColor?
}
import UIKit
import Charts
public protocol MyLineChartViewDelegate : class {
//Graph tapped
func lineTapped(position:CGPoint, value:(x:Double, y:Double))
}
public class MyLineChartView: LineChartView, ChartViewDelegate {
/// The line width
private let lineWidth:CGFloat = 3.0
/// Array of data models for the chart lines
public var lines:[MyChartLineModel]?
/// Show x values of the line
public var showXValues:Bool = false
/// Values formatter for the X axis
public var xAxisLabelFormatter:IAxisValueFormatter?
/// The Chart View delegate
public weak var lineDelegate:MyLineChartViewDelegate?
var viewLoaded:Bool = false
//MARK: - Graph data
private func createLineChartData(lineModel:MyChartLineModel) -> LineChartDataSet? {
let line:LineChartDataSet? = self.createLineChartWithValues(xValues: lineModel.xValues, yValues: lineModel.yValues)
//Add color
if let lineColor = lineModel.lineColor {
line?.colors = [lineColor] as [NSUIColor]
}
return line
}
private func createLineChartWithValues(xValues:[Double]?, yValues:[Double]?) -> LineChartDataSet? {
//Values must not be nil and x,y values must be have the same count
guard let xValues = xValues,
let yValues = yValues,
xValues.count == yValues.count else {
return nil
}
var lineChartEntry = [ChartDataEntry]()
for i in 0..<yValues.count {
let dataEntry = ChartDataEntry(x: xValues[i], y: yValues[i])
lineChartEntry += [dataEntry]
}
let line = LineChartDataSet(values: lineChartEntry, label: "")
//Remove dots
line.drawCirclesEnabled = false
line.lineWidth = self.lineWidth
line.mode = .cubicBezier
line.drawValuesEnabled = false
return line
}
//MARK: - Graph UI
private func setUpGrid() -> Void {
self.drawGridBackgroundEnabled = false
self.clipValuesToContentEnabled = true
self.dragEnabled = false
self.pinchZoomEnabled = false
self.setScaleEnabled(false)
self.leftAxis.enabled = false
self.rightAxis.enabled = false
//Remove labels
self.xAxis.drawGridLinesEnabled = true
self.xAxis.drawAxisLineEnabled = false
self.xAxis.drawLabelsEnabled = self.showXValues
//X axis at bottom
self.xAxis.labelPosition = XAxis.LabelPosition.bottom
self.leftAxis.drawGridLinesEnabled = false
self.leftAxis.drawAxisLineEnabled = false
self.leftAxis.drawLabelsEnabled = false
self.legend.enabled = false
}
//MARK: - Layout subviews
override public func layoutSubviews() {
super.layoutSubviews()
if let _ = self.superview,
!self.viewLoaded {
self.viewLoaded = true
self.delegate = self
self.layoutLines()
}
}
private func layoutLines() -> Void {
//Reset data
self.data = nil
//Set up UI
self.setUpGrid()
guard let lines = self.lines else {
self.noDataText = NSLocalizedString("No data", comment: "")
return
}
//Add all lines
let data = LineChartData()
for line in lines {
let lineSet:LineChartDataSet? = self.createLineChartData(lineModel: line)
data.addDataSet(lineSet)
}
self.data = data
self.highlightValue(nil)
self.chartDescription?.text = ""
if let xAxisFormatter = self.xAxisLabelFormatter {
self.xAxis.valueFormatter = xAxisFormatter
}
// self.animate(xAxisDuration: 1.0, yAxisDuration: 0.0, easingOption: .easeInCubic)
}
//MARK: - Reload graph
func reloadGraph() -> Void {
self.viewLoaded = false
self.setNeedsLayout()
self.layoutIfNeeded()
}
//MARK: - Chart View Delegate
public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
self.lineDelegate?.lineTapped(position: CGPoint(x: highlight.xPx, y: highlight.yPx), value:(highlight.x, highlight.y))
}
}
import UIKit
import Charts
class MyChartLineValueFormatter: NSObject, IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
//Return whatever value you want for x
return "test"
}
}
@lamprosg
Copy link
Author

lamprosg commented Jun 17, 2018

    let lineModel1:MyChartLineModel = MyChartLineModel()
    lineModel1.xValues = [1,2,3,4,5]
    lineModel1.yValues = [0.1,0.5,1.4,1.0,1.4]
    lineModel1.lineColor = UIColor.blue
    
    self.lineChartView.showXValues = true
    self.lineChartView.xAxisLabelFormatter = MyChartLineValueFormatter()  //format x axis values

    //More lines if necessary
    self.lineChartView.lines = [lineModel1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment