iOS Charts - LinechartView issue - ios

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

Related

LineChart in IOS using (Charts SDK)

Can we draw the verticle lines in every box in given image by using the IOS Charts SDK?
How we can draw those lines like given image?
Every box contains three lines inside it.
Setup The Chart (Code)
var lineChartView = LineChartView()
lineChartView.leftAxis.enabled = true
lineChartView.xAxis.labelCount = 24
lineChartView.legend.form = .none
let leftAxis = lineChartView.leftAxis
leftAxis.labelTextColor = UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)
leftAxis.labelCount = 4
leftAxis.drawGridLinesEnabled = true
leftAxis.granularityEnabled = true
leftAxis.yOffset = -8
let rightAxis = lineChartView.rightAxis
rightAxis.labelTextColor = UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)
rightAxis.labelCount = 4
rightAxis.drawGridLinesEnabled = true
rightAxis.granularityEnabled = true
rightAxis.yOffset = -8
lineChartView.rightAxis.drawLimitLinesBehindDataEnabled = true
lineChartView.leftAxis.drawLimitLinesBehindDataEnabled = true
lineChartView.xAxis.valueFormatter = CustomChartFormatter(xAxisHours: topAxisValues)
lineChartView.rightAxis.valueFormatter = RightCustomChartFormatter(rightAxisHours: rightAxisValues)
lineChartView.leftAxis.valueFormatter = LeftCustomChartFormatter(leftAxisHours: leftAxisValues)
lineChartView.animate(xAxisDuration: 2.5)
Setting the Data (Code)
let set1 = LineChartDataSet(entries: yValues, label: "")
set1.mode = .stepped
set1.drawCirclesEnabled = false
set1.lineWidth = 2
set1.highlightLineWidth = 20
set1.setColor(UIColor.richBlue2 ?? UIColor.blue)
set1.fill = Fill(color: UIColor.blue)
set1.fillAlpha = 0.8
let data = LineChartData(dataSet: set1)
data.setDrawValues(false)
lineChartView.data = data

How to show the value of line only when user touch at a point in line?

I am using Charts library in my project to show line chart. Till now i did this far :
but want to show the max or min value when user touches a any point in the red line like this:
Here is my code so far:
let set1 = LineChartDataSet(entries: yValues, label: "Subscribers")
set1.drawCirclesEnabled = false //hides the peek circles
set1.mode = .cubicBezier
set1.lineWidth = 4
set1.setColor(#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1))
set1.drawHorizontalHighlightIndicatorEnabled = false
set1.drawValuesEnabled = true
set1.valueTextColor = .systemGray
let data = LineChartData(dataSet: set1)
data.setDrawValues(false)//hides the peek circle values
lineChartView.data = data

How to show value of bar on the top of the bar in Charts iOS?

i am using Charts pod.
My problem is that i want to show bar value at the top but also i can't change charts function which goes like label.position = .top because then days name will come at the top.
also i cannot make days label static as it will change for year and month wise also
it will be helpful if anyones know the answer
i want code in swift 5
//here is my code:
let dataSet = BarChartDataSet(entries: enteies,label: "Days")
dataSet.valueColors = [.clear]
dataSet.colors = [#colorLiteral(red: 0.1219139919, green: 0.4706707597, blue: 0.7069483399, alpha: 1)]
let data = BarChartData(dataSets: [dataSet])
barChartView.data = data
barChartView.chartDescription?.text = ""
barChartView.scaleXEnabled = false
barChartView.scaleYEnabled = false
barChartView.highlightPerTapEnabled = true
barChartView.dragEnabled = true
barChartView.fitBars = true
barChartView.drawValueAboveBarEnabled = true
barChartView.animate(yAxisDuration: 0.5)
barChartView.legend.enabled = false
barChartView.legend.drawInside = true
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.enabled = true
barChartView.xAxis.axisLineColor = #colorLiteral(red: 0.1215686275, green: 0.4705882353, blue: 0.7058823529, alpha: 1)
barChartView.xAxis.granularityEnabled = false
barChartView.xAxis.granularity = 1
barChartView.xAxis.labelPosition = .bottom
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.granularity = 1
barChartView.leftAxis.granularityEnabled = true
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.leftAxis.axisLineColor = #colorLiteral(red: 0.1219139919, green: 0.4706707597, blue: 0.7069483399, alpha: 1)
barChartView.rightAxis.enabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
var title = [String]()
for value in insightsVM.graphData{
if insightsVM.graphData.count > 5{
title.append(String(value.title.prefix(3)))
}
else{
title.append(value.title)
}
}
if insightsVM.graphData.count > 7{
barChartView.xAxis.labelCount = 12
}
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:title)
barChartView.drawValueAboveBarEnabled = true
My values were their on the top of bar but what i did wrong was that i was clearing their text color on line: "dataSet.valueColors = [.clear]".

How to display bar values on the top of bar?

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

How can I hide 0 values on ios-chart?

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))

Resources