I created two data sets, chartDataSet and limitDataSet.
var dataEntries = [BarChartDataEntry]()
var limitEntries = [BarChartDataEntry]()
for i in 0..<daily[index].count {
let dataEntry = BarChartDataEntry(value: values[index][i], xIndex: i)
let limitEntry = BarChartDataEntry(value: limitValues[index][i], xIndex: i)
dataEntries.append(dataEntry)
limitEntries.append(limitEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: label)
chartDataSet.colors = [UIColor(red: 108/255, green: 189/255, blue: 146/255, alpha: 1)]
let limitDataSet = BarChartDataSet(yVals: limitEntries, label: "Limit in mg")
limitDataSet.colors = [UIColor(red: 219/255, green: 70/255, blue: 70/255, alpha: 1)]
var dataSets = [BarChartDataSet]()
dataSets.append(chartDataSet)
dataSets.append(limitDataSet)
//this line does has error
let chartData = BarChartData(xVals: daily[index], dataSet: dataSets)
monthlyChart.data = chartData
I need to add the datasets in my Bar Chart, but the code I used would only work for single dataSet.
This is the chart that I was using in my project.
https://github.com/danielgindi/Charts
I have not added 2 dataSets on BarChart.
But I have done this with PieChart.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var pieChartView: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setChart(months, values: unitsSold)
setChart2(months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
formatter.maximumFractionDigits = 1
formatter.multiplier = 1.0
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartData.dataSet?.valueFormatter = formatter
pieChartView.data = pieChartData
pieChartView.holeColor = UIColor.clearColor()
pieChartView.holeRadiusPercent = 0.95
pieChartView.centerText = "Hello\nThis is Pie chart"
pieChartView.usePercentValuesEnabled = true
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(255))
let green = Double(arc4random_uniform(255))
let blue = Double(arc4random_uniform(255))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
func setChart2(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
formatter.maximumFractionDigits = 1
formatter.multiplier = 1.0
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartData.dataSet?.valueFormatter = formatter
pieChartView.data = pieChartData
pieChartView.holeColor = UIColor.clearColor()
pieChartView.holeRadiusPercent = 0.95
pieChartView.centerText = "Hello\nThis is Pie chart"
pieChartView.usePercentValuesEnabled = true
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(255))
let green = Double(arc4random_uniform(255))
let blue = Double(arc4random_uniform(255))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Related
Can I show all the markers without clicking on a bar?
currently, I need to tap on any of the bar and it'll only show the one that is clicked. I'm trying to add a marker on each of the green bar and showing all without any bar being clicked. Is it possible?
Edit:
Below is my code changed from the github demo code
(PillMarker came from this)
import Charts
import UIKit
class BarChartViewController: DemoBaseViewController {
#IBOutlet var chartView: BarChartView!
#IBOutlet var sliderX: UISlider!
#IBOutlet var sliderY: UISlider!
#IBOutlet var sliderTextX: UITextField!
#IBOutlet var sliderTextY: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.options = [.animateY]
self.setup(barLineChartView: chartView)
chartView.delegate = self
chartView.drawBarShadowEnabled = false
chartView.drawValueAboveBarEnabled = false
chartView.maxVisibleCount = 60
chartView.dragEnabled = false
chartView.scaleXEnabled = false
chartView.scaleYEnabled = false
chartView.leftAxis.drawAxisLineEnabled = false
chartView.xAxis.drawGridLinesEnabled = false
chartView.legend.enabled = false
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .systemFont(ofSize: 10)
xAxis.labelTextColor = .green
xAxis.granularity = 1
xAxis.labelCount = 7
xAxis.valueFormatter = DayAxisValueFormatter(chart: chartView)
let leftAxis = chartView.leftAxis
leftAxis.labelFont = .systemFont(ofSize: 10)
leftAxis.labelTextColor = .red
leftAxis.labelCount = 8
leftAxis.labelPosition = .outsideChart
leftAxis.spaceTop = 0.15
leftAxis.axisMinimum = 0
let l = chartView.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.drawInside = false
l.form = .square
l.formSize = 9
l.font = UIFont(name: "HelveticaNeue-Light", size: 11)!
l.textColor = .blue
l.xEntrySpace = 4
//Marker
let marker = PillMarker(color: .white, font: UIFont.boldSystemFont(ofSize: 14), textColor: .black)
chartView.marker = marker
sliderX.value = 12
sliderY.value = 50
slidersValueChanged(nil)
}
override func updateChartData() {
if self.shouldHideData {
chartView.data = nil
return
}
self.setDataCount(1, range: UInt32(sliderY.value))
}
func setDataCount(_ count: Int, range: UInt32) {
let start = 1
let yVals = (start..<start+count+1).map { (i) -> BarChartDataEntry in
let mult = range + 1
let val = Double(arc4random_uniform(mult))
return BarChartDataEntry(x: Double(i), y: val)
}
var set1: BarChartDataSet! = nil
if let set = chartView.data?.first as? BarChartDataSet {
set1 = set
set1.replaceEntries(yVals)
chartView.data?.notifyDataChanged()
chartView.notifyDataSetChanged()
} else {
set1 = BarChartDataSet(entries: yVals, label: "The year 2017")
set1.colors = ChartColorTemplates.material()
set1.highlightColor = NSUIColor(red: 46/255.0, green: 204/255.0, blue: 113/255.0, alpha: 1.0)
set1.drawValuesEnabled = false
set1.drawIconsEnabled = true
let data = BarChartData(dataSet: set1)
data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 10)!)
data.barWidth = 0.3
chartView.data = data
}
}
Yes, You can show all marker on bar.
put marker code with bar drawing code.
I'm using iOS charts library for add custom charts in my app, I have created a bar chart which is working as my requirement, For some reason I have inverted the graph so now the problem is the values based on which the bar chart is created are displayed on the chart, I want to display these values on the top of the bar. Here is the code that I'm using for creating a graph
#IBOutlet var barChartView: BarChartView!
let unitsSold = [20, 4, 6, 3, 12, 50, 25, 57, 60, 28, 17, 47]
let unitsBought = [10.0, 14.0, 60.0, 13.0, 2.0, 10.0, 15.0, 18.0, 25.0, 05.0, 10.0, 19.0]
let xaxisValue: [String] = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
//MARK:- View Life Cycle -
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
//MARK:- General Methods -
func setupView() {
//legend
let legend = barChartView.legend
legend.enabled = true
legend.horizontalAlignment = .right
legend.verticalAlignment = .bottom
legend.orientation = .horizontal
legend.drawInside = true
legend.yOffset = 10.0;
legend.xOffset = 10.0;
legend.yEntrySpace = 0.0;
legend.textColor = UIColor.white
// Y - Axis Setup
let yaxis = barChartView.leftAxis
yaxis.spaceTop = 0.35
yaxis.axisMinimum = 0
yaxis.drawGridLinesEnabled = false
yaxis.labelTextColor = UIColor.white
yaxis.axisLineColor = UIColor.white
yaxis.labelPosition = .insideChart
yaxis.enabled = false
// YAxis leftAxis = barChart.getAxisLeft();
barChartView.rightAxis.enabled = false
// X - Axis Setup
let xaxis = barChartView.xAxis
let formatter = CustomLabelsXAxisValueFormatter()//custom value formatter
formatter.labels = self.xaxisValue
xaxis.valueFormatter = formatter
xaxis.drawGridLinesEnabled = false
xaxis.labelPosition = .top
xaxis.labelTextColor = UIColor.white
xaxis.centerAxisLabelsEnabled = true
xaxis.axisLineColor = UIColor.white
xaxis.granularityEnabled = true
xaxis.enabled = true
barChartView.delegate = self
barChartView.noDataText = "You need to provide data for the chart."
barChartView.noDataTextColor = UIColor.white
barChartView.chartDescription?.textColor = UIColor.clear
setChart()
}
func setChart() {
barChartView.noDataText = "Loading...!!"
var dataEntries: [BarChartDataEntry] = []
var dataEntries1: [BarChartDataEntry] = []
for i in 0..<self.xaxisValue.count {
let dataEntry = BarChartDataEntry(x: Double(i) , y: Double(self.unitsSold[i]))
dataEntries.append(dataEntry)
let dataEntry1 = BarChartDataEntry(x: Double(i) , y: Double(self.unitsBought[i]))
dataEntries1.append(dataEntry1)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Sold")
let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: "Bought")
let dataSets: [BarChartDataSet] = [chartDataSet,chartDataSet1]
chartDataSet.colors = [UIColor(red: 0/255, green: 255/255, blue: 0/255, alpha: 0.5)]
chartDataSet1.colors = [UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 0.8)]
let chartData = BarChartData(dataSets: dataSets)
let groupSpace = 0.4
let barSpace = 0.02
let barWidth = 0.2
chartData.barWidth = barWidth
chartData.setDrawValues(true)
barChartView.xAxis.axisMinimum = 0.0
barChartView.xAxis.axisMaximum = 0.0 + chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(self.xaxisValue.count)
chartData.groupBars(fromX: 0.0, groupSpace: groupSpace, barSpace: barSpace)
barChartView.xAxis.granularity = barChartView.xAxis.axisMaximum / Double(self.xaxisValue.count)
barChartView.drawValueAboveBarEnabled = true
barChartView.keepPositionOnRotation = true
barChartView.clipValuesToContentEnabled = true
barChartView.data = chartData
barChartView.getAxis(.left).inverted = true
barChartView.notifyDataSetChanged()
barChartView.setVisibleXRangeMaximum(4)
barChartView.animate(yAxisDuration: 1.0, easingOption: .linear)
chartData.setValueTextColor(UIColor.yellow)
}
Here is the output of above code
And I want to display the label as below image (when the graph is in the inverted state)
Please let me know how can I achieve this.
I don't think that this is possible at the moment using Charts. The values are rendered in BarChartRender.swift in the drawValues(context: CGContext) function but always above the bar. They are not rendered below if the left axis is inverted. If you want this behavior you should implement it yourself changing this part of code of the function:
drawValue(
context: context,
value: formatter.stringForValue(
val,
entry: e,
dataSetIndex: dataSetIndex,
viewPortHandler: viewPortHandler),
xPos: x,
yPos: val >= 0.0
? (rect.origin.y + rect.size.height + posOffset)
: (rect.origin.y + negOffset), // Those lines should do what you're trying to achieve
font: valueFont,
align: .center,
color: dataSet.valueTextColorAt(j))
i think you have to set - like below
set = BarChartDataSet(entries: yData , label: "Dataset")
//make this enable
set.drawValuesEnabled = true
Easy today!
barChart.drawValueAboveBarEnabled = true
I want to add a line chart in UICollectionView cell by using self-created class, BaseLineChartView, which inherit from LineChartView in iOS charts API. I have to show different kinds of line charts with single line and multiple lines. So this subclass is used for getting data and setting up the chartView.
After I run the app, the cell only show chart with default noDataText. The chart doesn't show up and the change I have done in noDataText doesn't work either. But the sentence in the last line of setChart function was executed and printed in console.
Here is the code of the class:
import UIKit
import Charts
class BaseLineChartView: LineChartView {
let chartView = LineChartView()
var months = [String]()
var unitsSold = [Double]()
override init(frame: CGRect) {
super.init(frame: frame)
populateData()
lineChartSetup()
setChart(dataPoints: months, values: unitsSold)
}
func populateData(){
let _months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let _unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
months = _months
unitsSold = _unitsSold
}
func lineChartSetup() {
// MARK: General
chartView.noDataText = "No Data" //Doesn't work..
chartView.chartDescription?.enabled = true
chartView.dragEnabled = false
chartView.setScaleEnabled(false)
chartView.pinchZoomEnabled = false
chartView.highlightPerDragEnabled = true
chartView.backgroundColor = .lightGray
chartView.dragXEnabled = true
chartView.dragYEnabled = false
// MARK: xAxis
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
xAxis.labelTextColor = UIColor(red: 255/255, green: 192/255, blue: 56/255, alpha: 1)
xAxis.axisLineColor = UIColor.lightGray
xAxis.drawAxisLineEnabled = true
xAxis.drawGridLinesEnabled = false
xAxis.centerAxisLabelsEnabled = false
xAxis.granularity = 1
xAxis.labelRotationAngle = -90.0
// MARK: leftAxis
let leftAxis = chartView.leftAxis
leftAxis.labelPosition = .outsideChart
leftAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
leftAxis.drawGridLinesEnabled = false
leftAxis.granularityEnabled = true
leftAxis.labelTextColor = UIColor(red: 255/255, green: 192/255, blue: 56/255, alpha: 1)
// MARK: rightAxis
chartView.rightAxis.enabled = false
// MARK: legend
chartView.legend.enabled = false
// MARK: animation
chartView.animate(xAxisDuration: 1.5)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry.init(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
lineChartDataSet.mode = .cubicBezier
let lineChartData = LineChartData(dataSet: lineChartDataSet)
chartView.data = lineChartData
chartView.scaleXEnabled = true
chartView.scaleYEnabled = false
chartView.setVisibleXRangeMaximum(10.0)
chartView.xAxis.setLabelCount(11, force: false)
print("Create Chart Successfully!") //Successfully show in console.
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And I add this class into collectionView cell like this:
class DailyIndexCell: UICollectionViewCell {
let trackingChart: BaseLineChartView = {
let chartView = BaseLineChartView()
return chartView
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(trackingChart)
trackingChart.notifyDataSetChanged() //Doesn't work
self.setNeedsDisplay() //Doesn't work
setConstraints()
}
func setConstraints() {
//Add constraints of trackingChart
}
}
I've tried the solution in this post but didn't work ='(
Charts not plotting in tableViewCell
It's look like you have created property chartView in BaseLineChartView class and set various params for this property but don't use it. So for resolving your problem remove chartView property from BaseLineChartView and replace all its occurrences to self (or just remove). Also pay attantion to lines let xAxis = chartView.xAxis and let leftAxis = chartView.leftAxis you don't need to use it.
import UIKit
import Charts
class BaseLineChartView: LineChartView {
var months = [String]()
var unitsSold = [Double]()
override init(frame: CGRect) {
super.init(frame: frame)
populateData()
lineChartSetup()
setChart(dataPoints: months, values: unitsSold)
}
func populateData(){
let _months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let _unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
months = _months
unitsSold = _unitsSold
}
func lineChartSetup() {
// MARK: General
noDataText = "No Data" //Doesn't work..
chartDescription?.enabled = true
dragEnabled = false
setScaleEnabled(false)
pinchZoomEnabled = false
highlightPerDragEnabled = true
backgroundColor = .lightGray
dragXEnabled = true
dragYEnabled = false
// MARK: xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
xAxis.labelTextColor = UIColor(red: 255/255, green: 192/255, blue: 56/255, alpha: 1)
xAxis.axisLineColor = UIColor.lightGray
xAxis.drawAxisLineEnabled = true
xAxis.drawGridLinesEnabled = false
xAxis.centerAxisLabelsEnabled = false
xAxis.granularity = 1
xAxis.labelRotationAngle = -90.0
// MARK: leftAxis
leftAxis.labelPosition = .outsideChart
leftAxis.labelFont = .systemFont(ofSize: 10, weight: .light)
leftAxis.drawGridLinesEnabled = false
leftAxis.granularityEnabled = true
leftAxis.labelTextColor = UIColor(red: 255/255, green: 192/255, blue: 56/255, alpha: 1)
// MARK: rightAxis
rightAxis.enabled = false
// MARK: legend
legend.enabled = false
// MARK: animation
animate(xAxisDuration: 1.5)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry.init(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
lineChartDataSet.mode = .cubicBezier
let lineChartData = LineChartData(dataSet: lineChartDataSet)
data = lineChartData
scaleXEnabled = true
scaleYEnabled = false
setVisibleXRangeMaximum(10.0)
xAxis.setLabelCount(11, force: false)
print("Create Chart Successfully!") //Successfully show in console.
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I know that it is possible to hide y values when their values are equal to 0 on MPAndroidChart using a custom class for your value formatter (MPAndroidChart: Hide 0 value labels in a stacked bar chart).
Despite this, I am not able to create the same class on Swift 3.0 or get any other way to do this. I tried to "translate" the custom class from Java to Swift 3.0 without success (I can copy the code of what I have tried if you want but it is full of errors).
Is it possible to hide y values when they are equals to 0 on ios-chart library?
P.S: I am using Swift 3.0.
Thanks in advance!
I made it happen on a PieChart in one of my apps just like that :
...
let dataSet = PieChartDataSet(yVals: yVals, label: nil)
// This is where the magic happen
// You set a NSNumberFormatter with an empty zero Symbol
let noZeroFormatter = NumberFormatter()
noZeroFormatter.zeroSymbol = ""
dataSet.valueFormatter = ChartDefaultValueFormatter(formatter: noZeroFormatter)
let chartData = PieChartData(xVals: xVals, dataSet: dataSet)
...
if you want to add % in in your graph as well as hide/remove 0.0 values from graph :
used below lines of code for # Swift 3:-
func updateChartData() {
let chart = PieChartView(frame: mViewOutlet.frame)
// let chart = PieChartView(frame: CGRect(x: 122, y: 235 , width: self.mViewOutlet.frame.size.width, height: self.mViewOutlet.frame.size.height))
// 2. generate chart data entries
let track = ["Present","Leave", "EG/LC", "Halfday", "Absent", "Weeklyoff", "Holidays"]
// let money = [65, 13, 10, 2]
let money = mDaysArray
var entries = [PieChartDataEntry]()
for (index, value) in money.enumerated() {
print("index: \(index) \n value: \(value)")
let entry = PieChartDataEntry()
if value != 0 {
entry.y = Double(value)
}else{
}
entries.append(entry)
// entry.label = track[index] // if we want to remove name label
}
// 3. chart setup
let set = PieChartDataSet( values: entries, label: "Pie Chart")
// this is custom extension method. Download the code for more details.
//4. set chart color
let presentColor = UIColor(red: 80.0/255.0, green: 180.0/255.0, blue: 50.0/255.0, alpha: 1.0)
// let lateColor = UIColor(red: 241.0/255.0, green: 194.0/255.0, blue: 114.0/255.0, alpha: 1.0)
let leaveColor = UIColor(red: 203.0/255.0, green: 68.0/255.0, blue: 242.0/255.0, alpha: 1.0)
let egColor = UIColor(red: 95.0/255.0, green: 180.0/255.0, blue: 239.0/255.0, alpha: 1.0)
let halfdayColor = UIColor(red: 82.0/255.0, green: 64.0/255.0, blue: 152.0/255.0, alpha: 1.0)
let absentColor = UIColor(red: 242.0/255.0, green: 58.0/255.0, blue: 02.0/255.0, alpha: 1.0)
let weekOffColor = UIColor(red: 186.0/255.0, green: 221.0/255.0, blue: 79.0/255.0, alpha: 1.0)
let holidayColor = UIColor(red: 35.0/255.0, green: 215.0/255.0, blue: 179.0/255.0, alpha: 1.0)
let colors: [UIColor] = [presentColor,leaveColor,egColor,halfdayColor,absentColor,weekOffColor,holidayColor]
set.colors = colors
let data = PieChartData(dataSet: set)
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 2
formatter.multiplier = 1.0
formatter.percentSymbol = "%"
formatter.zeroSymbol = ""
data.setValueFormatter(DefaultValueFormatter(formatter: formatter))
chart.data = data
chart.noDataText = "No data available"
chart.usePercentValuesEnabled = true
// user interaction
chart.isUserInteractionEnabled = false
let d = Description()
// d.text = "iOSCharts.io"
chart.chartDescription = d
// chart.tintColor = UIColor.black
// chart.centerText = "Pie Chart"
chart.holeRadiusPercent = 0.2
chart.chartDescription?.enabled = false
chart.legend.enabled = false
chart.data?.notifyDataChanged()
chart.notifyDataSetChanged()
chart.setNeedsDisplay()
chart.animate(xAxisDuration: 1.3, yAxisDuration: 1.3)
chart.transparentCircleColor = UIColor.clear
// self.view.addSubview(chart)
self.mPieChartMainView.addSubview(chart)
}
let data = PieChartData(dataSet: set)
let pFormatter = NumberFormatter()
pFormatter.numberStyle = .none
pFormatter.zeroSymbol = "";
pFormatter.maximumFractionDigits = 1
pFormatter.multiplier = 1
pFormatter.percentSymbol = ""
data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
I wanted to remove 'description text' view, 'data set' view (square shape view below the graph). I am using iOS Charts library - LineChartView
import Charts
---------------------- class declaration -------------------
#IBOutlet var graphEmotionsView: LineChartView! // Class outlet variable
let paramsList = ["","Anger" , "Disgust", "Fear", "Joy", "Sadness",""]
let paramsListVal = ['give some values']
In viewdidload
//Set graph emotions view befor applying the data
self.graphEmotionsView.backgroundColor = UIColor.lightGrayColor()
//Gestures
self.graphEmotionsView.doubleTapToZoomEnabled = false
self.graphEmotionsView.pinchZoomEnabled = false
//Highlight per tap
self.graphEmotionsView.highlightPerTapEnabled = false
self.graphEmotionsView.highlightPerDragEnabled = false
self.graphEmotionsView.highlightFullBarEnabled = false
//X
self.graphEmotionsView.xAxis.labelPosition = .BottomInside
self.graphEmotionsView.xAxis.labelTextColor = UIColor.whiteColor()
var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0 ..< emotionParamsValues.count {
yVals1.append(ChartDataEntry(value: emotionParamsValues[i], xIndex: i+1))
}
print("yValues: \(yVals1)")
let set2: LineChartDataSet = LineChartDataSet()
set2.yVals = yVals1
set2.label = ""
set2.visible = true
// set chart line
set2.mode = .CubicBezier
set2.axisDependency = .Left
set2.lineWidth = 2.5
set2.setColor(UIColor(red: 157/255.0, green: 0/255.0, blue: 38/255.0, alpha: 1.0))
set2.highlightColor = UIColor.whiteColor()
// set custom point
set2.drawCirclesEnabled = true // should be true if you want hollow circles
set2.drawCircleHoleEnabled = true
set2.circleHoleRadius = 5.0
set2.setCircleColor(UIColor(red: 157/255.0, green: 0/255.0, blue: 38/255.0, alpha: 1.0))
// set fill chart
set2.drawFilledEnabled = true
set2.fillAlpha = 240 / 255.0
set2.fillColor = UIColor(red: 237/255.0, green: 26/255.0, blue: 103/255.0, alpha: 0.9)
var dataSets : [LineChartDataSet] = [LineChartDataSet]()
dataSets.append(set2)
dataSets[0].notifyDataSetChanged()
let data: LineChartData = LineChartData(xVals: emotionParamsList, dataSets: dataSets)
data.setValueTextColor(UIColor.whiteColor())
self.graphEmotionsView.data = data
Output I am getting
For fixing the description, try:
chart.description = ""
For disable the legend, try:
chart.legend.enabled = false
Shameless plug - you can find ios charts examples at ioscharts.io