Truncate value label of PieChart in Charts Library Swift - swift

`class PieChartViewController: DemoBaseViewController {
#IBOutlet var chartView: PieChartView!
#IBOutlet var sliderX: UISlider!
#IBOutlet var sliderY: UISlider!
#IBOutlet var sliderTextX: UITextField!
#IBOutlet var sliderTextY: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Pie Chart"
self.setup(pieChartView: chartView)
chartView.delegate = self
chartView.holeRadiusPercent = 0.8
chartView.usePercentValuesEnabled = true
chartView.transparentCircleRadiusPercent = 0
chartView.drawHoleEnabled = true
chartView.transparentCircleColor = UIColor.white.withAlphaComponent(1)
chartView.drawCenterTextEnabled = true
chartView.centerText = nil
let l = chartView.legend
l.enabled = false
//MARK: Entry label Styling (Value Label Above PieChart)
chartView.entryLabelColor = .black
chartView.entryLabelFont = .systemFont(ofSize: 14, weight: .medium)
//MARK: Setting Offset
let frameOffset = chartView.frame.width * 0.12
chartView.setExtraOffsets(left: frameOffset, top: frameOffset, right: frameOffset, bottom: frameOffset)
self.slidersValueChanged(nil)
}
override func updateChartData() {
if self.shouldHideData {
chartView.data = nil
return
}
self.setDataCount()
}
func setDataCount() {
let entry1 = PieChartDataEntry(value: 22, label: "Apple.It is a fruit")
let entry2 = PieChartDataEntry(value: 33, label: "Zebra.It is an animal")
let entry3 = PieChartDataEntry(value: 31, label: "Eagle.It is a bird")
let entry4 = PieChartDataEntry(value: 14, label: "Asia.It is a continent")
var enteries: [PieChartDataEntry] = []
enteries.append(entry1)
enteries.append(entry2)
enteries.append(entry3)
enteries.append(entry4)
let set = PieChartDataSet(entries: enteries, label: "Knowledge")
set.drawIconsEnabled = false
set.sliceSpace = 0
set.selectionShift = 0
set.colors = ChartColorTemplates.vordiplom()
+ ChartColorTemplates.joyful()
+ ChartColorTemplates.colorful()
+ ChartColorTemplates.liberty()
+ ChartColorTemplates.pastel()
+ [UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)]
// MARK: Setting Value Label
set.valueLinePart1OffsetPercentage = 0.20
set.valueLinePart1Length = 0.4
set.valueLinePart2Length = 0.4
set.valueLineColor = .black
set.valueTextColor = .black
set.xValuePosition = .outsideSlice
set.yValuePosition = .outsideSlice
let data = PieChartData(dataSet: set)
data.setValueFont(.systemFont(ofSize: 13, weight: .medium))
data.setValueTextColor(.black)
chartView.data = data
chartView.highlightValues(nil)
}
override func optionTapped(_ option: Option) {
chartView.setNeedsDisplay()
}
// MARK: - Actions
#IBAction func slidersValueChanged(_ sender: Any?) {
self.updateChartData()
}
}`
How to truncate or word wrap the value label in Pie Chart ?
I am using Charts Library in iOS.
If I add extra offset the pie chart shrink in smaller devices like iPhone 5s or iPhone SE.
If the text is large then the text goes out of the screen as you can see in the uploaded image. I am not able to find any properties (word wrap) to handle the large text issue.

Related

Problem with labels using library Charts with UIKit in XCode 12

Hell my friends.I'm developing an app which has to show several charts using UIKit and the library Charts. I've been requested to use both things.
I have two arrays with correct data Ciudades y Precios.
Ciudades is String type and Precios Double type. I'v been reading how to use Charts library for hours but it seems that I dont understand how to use it because the chart I get it's not the one I excpected to. I need a chart with the cities on x label but they are not showed as you can see in the picture.
This is my code
class BarChartViewController: UIViewController, ChartViewDelegate {
#IBOutlet weak var barChartView: BarChartView!
var nombres: [String]!
var precios: [Double]!
override func viewDidLoad() {
super.viewDidLoad()
barChartView.delegate = self
nombres = eventos.devolverNombre()
print(nombres)
precios = eventos.devolverPrecios()
setChart(dataPoints: nombres, values: precios)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "Es necesario aƱadir datos."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
if dataEntry != nil {
dataEntries.append(dataEntry)}
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "PRECIOS POR EVENTO")
let chartData = BarChartData(dataSet: chartDataSet)
chartDataSet.colors = [UIColor(red: 230/255, green: 126/255, blue: 34/255, alpha: 1)]
chartDataSet.colors = ChartColorTemplates.colorful()
barChartView.data = chartData
barChartView.xAxis.valueFormatter = DefaultAxisValueFormatter(block: {(index, _) in
return self.nombres[Int(index)] })
let labelCount = nombres.count
print(labelCount)
barChartView.xAxis.setLabelCount(labelCount, force: true)
barChartView.xAxis.labelRotationAngle = 90
barChartView.xAxis.centerAxisLabelsEnabled = true
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 10)
barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeInBounce)
}
}

PieChart using Charts on swift. Need to remove the values of the elements

In the black circles data that i wont to hide. I wonna to get simple green and red chat w\o text in elements.
I checked all the methods of PieChartView and wont find method to hide this data. Sims it integrated to elements...
Inplementation code:
import Charts
class ViewController: UIViewController {
#IBOutlet weak var pieChart: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
// pieChart.chartAnimator
// pieChart.drawCenterTextEnabled = false
// pieChart.drawHoleEnabled = false
let months = ["", ""]
let unitsSold = [60.0, 40.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
// let da = ChartDataEntry(
let dataEntry1 = ChartDataEntry(x: Double(0.0), y: 60.0)
let dataEntry2 = ChartDataEntry(x: Double(0.0), y: 40.0)
dataEntries.append(dataEntry1)
dataEntries.append(dataEntry2)
print(dataEntries[0].data as Any)
let pieChartDataSet = PieChartDataSet(entries: dataEntries, label: nil)
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChart.data = pieChartData
let colors: [UIColor] = [UIColor(cgColor: UIColor.red.cgColor), UIColor(cgColor: UIColor.green.cgColor)]
pieChartDataSet.colors = colors
}
You need to set the text color as clear, it will work fine. Check out following change in your current code
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChartData.setValueTextColor(NSUIColor.clear)
pieChart.data = pieChartData

Line chart fill color is faded

I am trying to setup a line chart with one fill colour but for some reason, the fill colour is faded.
Example
Both the random view I have added to middle of screen and the fill colour of the line chart are set to be red, but for some reason the fill colour of the chart is faded.
Can see code here
#IBOutlet var liveChart : LineChartView!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Chart Tests"
configureChart(chart: liveChart)
var xAxis = [String]()
var yAxis = [Double]()
for _ in 0..<10
{
xAxis.append("")
let yVal = Double(randomBetweenNumbers(firstNum: 1.0, secondNum: 100.0))
yAxis.append(yVal)
}
setData(xAxisArray: xAxis, yAxisArray: yAxis, chart: liveChart)
let testView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
testView.center = view.center
testView.backgroundColor = UIColor.red
view.addSubview(testView)
}
func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat{
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
func configureChart(chart : LineChartView)
{
chart.chartDescription?.text = ""
chart.noDataText = "Loading Data"
chart.backgroundColor = UIColor.clear
chart.drawGridBackgroundEnabled = false
chart.dragEnabled = true
chart.rightAxis.enabled = false
chart.leftAxis.enabled = true
chart.doubleTapToZoomEnabled = false
chart.legend.enabled = false
chart.pinchZoomEnabled = true
chart.highlightPerTapEnabled = false
chart.highlightPerDragEnabled = false
chart.xAxis.enabled = false
chart.leftAxis.drawAxisLineEnabled = false
chart.leftAxis.drawGridLinesEnabled = false
chart.leftAxis.labelCount = 5
chart.leftAxis.forceLabelsEnabled = true
}
func setData(xAxisArray : [String], yAxisArray : [Double], chart : LineChartView)
{
var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
if(xAxisArray.count > 0)
{
for i in 0 ..< xAxisArray.count
{
let chartEntry = ChartDataEntry(x: Double(i), y: yAxisArray[i], data: nil)
yVals1.append(chartEntry)
}
}
let set1: LineChartDataSet = LineChartDataSet(values: yVals1, label: "")
set1.fillColor = UIColor.red
set1.drawFilledEnabled = true
set1.drawCirclesEnabled = false
let data = LineChartData()
data.addDataSet(set1)
liveChart.data = data
}
Is there a way to fix this? Or is this just the way the fill colour of the chart works?
Edit:
I am using
https://github.com/danielgindi/Charts
I assume you use this library: https://github.com/kevinbrewster/SwiftCharts
So the LineChartView automatically set alpha for the fill color: https://github.com/kevinbrewster/SwiftCharts/blob/master/SwiftCharts/LineChart.swift#L758
try to set fillAlpha property of the data set

How to update a value within a mehod?

I am new to Swift and I want to update the value with this line:
circleShape.strokeEnd = CGFloat(sum)
every time I click "calculate" (to get the value of sum). How can I do that?
This is my code:
#IBOutlet weak var centerCircle: UIImageView!
var num1 = 0.0
var num2 = 0.0
var sum = 0.0
var a = 0.0
var b = 0.0
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var sumNum: UILabel!
#IBAction func vote1(sender: UIButton) {
num1 = Double(label1.text!)!
self.label1.text = String(num1 + 1)
a = num1 + 1
}
#IBAction func vote2(sender: UIButton) {
num2 = Double(label2.text!)!
self.label2.text = String(num2 + 1)
b = num2 + 1
}
#IBAction func calculate(sender: AnyObject) {
sum = Double(a/(b+a))
sumNum.numberOfLines = 0
sumNum.lineBreakMode = NSLineBreakMode.ByWordWrapping
sumNum.text = "\(sum)"
}
override func viewDidLoad() {
super.viewDidLoad()
// round view
let roundView = UIView(frame: CGRectMake(85, 100, 150, 150))
roundView.backgroundColor = UIColor.whiteColor()
roundView.layer.cornerRadius = roundView.frame.size.width / 2
// bezier path
let circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
radius: roundView.frame.size.width / 2.2 ,
startAngle: CGFloat(-0.5 * M_PI),
endAngle: CGFloat(1.5 * M_PI),
clockwise: true)
// circle shape
let circleShape = CAShapeLayer()
circleShape.path = circlePath.CGPath
circleShape.strokeColor = UIColor.blackColor().CGColor
circleShape.fillColor = UIColor.clearColor().CGColor
circleShape.lineWidth = 14
// set start and end values
circleShape.strokeStart = 0.0
circleShape.strokeEnd = CGFloat(sum)
// add sublayer
roundView.layer.addSublayer(circleShape)
// add subview
self.view.addSubview(roundView)
self.view.insertSubview(roundView, belowSubview: centerCircle)
}
You need to declare circleShape as a global variable and modify its strokeEnd value in the calculate function.
#IBOutlet weak var centerCircle: UIImageView!
var num1 = 0.0
var num2 = 0.0
var sum = 0.0
var a = 0.0
var b = 0.0
let circleShape = CAShapeLayer()
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var sumNum: UILabel!
#IBAction func vote1(sender: UIButton) {
num1 = Double(label1.text!)!
self.label1.text = String(num1 + 1)
a = num1 + 1
}
#IBAction func vote2(sender: UIButton) {
num2 = Double(label2.text!)!
self.label2.text = String(num2 + 1)
b = num2 + 1
}
#IBAction func calculate(sender: AnyObject) {
sum = Double(a/(b+a))
sumNum.numberOfLines = 0
sumNum.lineBreakMode = NSLineBreakMode.ByWordWrapping
sumNum.text = "\(sum)"
circleShape.strokeEnd = CGFloat(sum)
}
override func viewDidLoad() {
super.viewDidLoad()
// round view
let roundView = UIView(frame: CGRectMake(85, 100, 150, 150))
roundView.backgroundColor = UIColor.whiteColor()
roundView.layer.cornerRadius = roundView.frame.size.width / 2
// bezier path
let circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
radius: roundView.frame.size.width / 2.2 ,
startAngle: CGFloat(-0.5 * M_PI),
endAngle: CGFloat(1.5 * M_PI),
clockwise: true)
// circle shape
circleShape.path = circlePath.CGPath
circleShape.strokeColor = UIColor.blackColor().CGColor
circleShape.fillColor = UIColor.clearColor().CGColor
circleShape.lineWidth = 14
// set start and end values
circleShape.strokeStart = 0.0
circleShape.strokeEnd = CGFloat(sum)
// add sublayer
roundView.layer.addSublayer(circleShape)
// add subview
self.view.addSubview(roundView)
self.view.insertSubview(roundView, belowSubview: centerCircle)
}

How do I draw Bar-Chart using danielgindi/Charts library with Swift 3.0

Problem Statement : Unable to run due to some errors in implementation of Bar-Chart using 'danielgindi/Charts library' in swift 3.0 syntax errors.
Actual Error in Swift 3.0:
After correcting above error, it gives another error in next line, as shown below.
Coding Stuff:
import Charts
class ChartViewController: UIViewController {
#IBOutlet var barChartView: BarChartView!
var months: [String]!
var dataEntries: [BarChartDataEntry] = []
override func viewDidLoad() {
super.viewDidLoad()
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [values[i]])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
barChartView.data = chartData
}
Please help me, how do I resolve such errors in swift 3.0
I've had this issue as well. It was this line here:
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [values[i]])
You cannot convert dataPoints: [String] to type Double.
My solution was to remove the Double. Then that did not satisfy my data types, so I made them all of type [Int] like so:
func setChart(dataPoints: [Int], values: [Int]) {
self.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(values[i]) )
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData(dataSet: chartDataSet)
self.data = chartData
You can convert [Int] to Double, so that fixed my issue. I changed the variable months: [String] to
let months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
import UIKit
import Charts
class ViewController: UIViewController, ChartViewDelegate {
#IBOutlet weak var barChartView: BarChartView!
weak var axisFormatDelegate: IAxisValueFormatter?
var months: [String]!
var unitsSold = [Double]()
override func viewDidLoad() {
super.viewDidLoad()
barChartView.delegate = self
axisFormatDelegate = self
months = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
unitsSold = [20.0, 54.0, 6.0, 30.0, 92.0, 16.0,114.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
// Prevent from setting an empty data set to the chart (crashes)
guard dataPoints.count > 0 else { return }
var dataEntries = [BarChartDataEntry]()
for i in 0..<dataPoints.count {
let entry = BarChartDataEntry(x: Double(i), y: values[i], data: months as AnyObject?)
dataEntries.append(entry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "Units Sold")
chartDataSet.drawValuesEnabled = false
chartDataSet.colors = [UIColor.red]
chartDataSet.colors = [UIColor.lightGray]
chartDataSet.highlightColor = UIColor.orange.withAlphaComponent(0.3)
chartDataSet.highlightAlpha = 1
let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData
let xAxisValue = barChartView.xAxis
xAxisValue.valueFormatter = axisFormatDelegate
// chartDataSet.colors = ChartColorTemplates.colorful() //multiple colors
//Animation bars
barChartView.animate(xAxisDuration: 0.0, yAxisDuration: 1.0, easingOption: .easeInCubic)
// X axis configurations
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1
barChartView.xAxis.drawAxisLineEnabled = true
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 15.0)
barChartView.xAxis.labelTextColor = UIColor.black
barChartView.xAxis.labelPosition = .bottom
// Right axis configurations
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawLabelsEnabled = false
// Other configurations
barChartView.highlightPerDragEnabled = false
barChartView.chartDescription?.text = ""
barChartView.legend.enabled = false
barChartView.pinchZoomEnabled = false
barChartView.doubleTapToZoomEnabled = false
barChartView.scaleYEnabled = false
barChartView.drawMarkers = true
let l = barChartView.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.drawInside = false
l.form = .circle
l.formSize = 9
l.font = UIFont(name: "HelveticaNeue-Light", size: 11)!
l.xEntrySpace = 4
// On tapped bar marker before adding BalloonMarker.swift
let marker = BalloonMarker(color: UIColor.orange, font: UIFont.boldSystemFont(ofSize: 13), textColor: UIColor.white, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 15.0, right: 7.0))
marker.chartView = barChartView
barChartView.marker = marker
}
}
extension ViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return months[Int(value)]
}
}
Install pod file
Assign barchartview class to chart view in storyboad
Follow setchart() method
For ballon marker add BallonMarker.swift file to your project library for this file chartsDemo->Components-> BallonMarker.swift
I have implemented a demo for that
This will Support Swift 4.2 and above
Check this link :
https://github.com/vijay1864/Barchart-Demo-with-Charts-Library
This Demo was Made with help of danielgindi Charts Library
https://github.com/danielgindi/Charts
You can iterate month and units sold values as xVal and yVal.
var barChartValues: [BarChartDataEntry] = []
for (key,val) in self.barChartDataval.enumerated() {
let xVal = Double(key)
let yVal = Double(val.cost)
let dataEntry = BarChartDataEntry(x: xVal, y: yVal!)
barChartValues.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: barChartValues, label: "Expenses Summary")
chartDataSet.colors = [Constants.k_COLORS.BLUE]
var dataSets = [IChartDataSet]()
dataSets.append(chartDataSet)
let chartData = BarChartData(dataSets: dataSets)
self.barChartView.data = chartData
self.barChartView.chartDescription?.text = ""
self.barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
For Line chart We can have following
import UIKit
import Charts
class LineChartVC: UIViewController , ChartViewDelegate{
#IBOutlet var lineChartView: LineChartView!
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
self.lineChartSetUp()
}
}
extension LineChartVC{
func lineChartSetUp(){
self.lineChartView.delegate = self
self.lineChartView.chartDescription?.enabled = false//true
self.lineChartView.dragEnabled = true
self.lineChartView.setScaleEnabled(true)
self.lineChartView.pinchZoomEnabled = true
self.lineChartView.drawGridBackgroundEnabled = false
// x-axis limit line
/*
let llXAxis = ChartLimitLine(limit: 10.0, label: "Index 10")
llXAxis.lineWidth = 4.0
llXAxis.lineDashLengths = [(10.0), (10.0), (0.0)]
llXAxis.labelPosition = ChartLimitLine.LabelPosition.rightBottom//ChartLimitLabelPositionRightBottom
llXAxis.valueFont = UIFont.systemFont(ofSize: 10.0)
//[_chartView.xAxis addLimitLine:llXAxis];
self.lineChartView.xAxis.gridLineDashLengths = [10.0, 10.0]
self.lineChartView.xAxis.gridLineDashPhase = 0.0
let ll1 = ChartLimitLine(limit: 150.0, label: "Upper Limit")
ll1.lineWidth = 4.0
ll1.lineDashLengths = [5.0, 5.0]
ll1.labelPosition = ChartLimitLine.LabelPosition.rightTop //ChartLimitLabelPositionRightTop
ll1.valueFont = UIFont.systemFont(ofSize: 10.0)
let ll2 = ChartLimitLine(limit: -30.0, label: "Lower Limit")
ll2.lineWidth = 4.0
ll2.lineDashLengths = [5.0, 5.0]
ll2.labelPosition = ChartLimitLine.LabelPosition.rightBottom//ChartLimitLabelPositionRightBottom
ll2.valueFont = UIFont.systemFont(ofSize: 10.0)
let leftAxis: YAxis? = self.lineChartView.leftAxis
leftAxis?.removeAllLimitLines()
// leftAxis?.addLimitLine(ll1)
// leftAxis?.addLimitLine(ll2)
// leftAxis?.axisMaximum = 200.0
// leftAxis?.axisMinimum = -50.0
// leftAxis?.axisMaximum = 100.0
// leftAxis?.axisMinimum = 0.0
//
leftAxis?.gridLineDashLengths = [5.0, 5.0]
leftAxis?.drawZeroLineEnabled = false
leftAxis?.drawLimitLinesBehindDataEnabled = true
self.lineChartView.rightAxis.enabled = false
//[_chartView.viewPortHandler setMaximumScaleY: 2.f];
//[_chartView.viewPortHandler setMaximumScaleX: 2.f];
*/
let marker = BalloonMarker(color: UIColor(white: 180 / 255.0, alpha: 1.0), font: UIFont.systemFont(ofSize: 12.0), textColor: UIColor.white, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))
marker.chartView = self.lineChartView
marker.minimumSize = CGSize(width: 80.0, height: 40.0)
self.lineChartView.marker = marker
self.lineChartView.legend.form = Legend.Form(rawValue: 5)!
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
self.setChart(months, values: unitsSold)
}
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x:Double(i) , y: values[i])
dataEntries.append(dataEntry)
}
print(dataEntries )
let data = LineChartData()
let ds1 = LineChartDataSet(values: dataEntries, label: "Units Sold")
ds1.colors = [UIColor.red]
data.addDataSet(ds1)
self.lineChartView.data = data
}
func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight) {
// print("\(entry.value) in \(months[dataSetIndex])")
print(entry )
}
}