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
Related
I am using the iOS Charts library for the candlestick chart, I not able to find any way to draw custom views that is a triangle as shown in the image library.
I want to show the triangle(view) on the top and bottom of the candle based on the flag.
If this is not possible in Charts library please let me know the alternate library where I can achieve this.
CandleSitckChart
{
chartView.delegate = self
chartView.chartDescription?.enabled = true
chartView.drawBordersEnabled = false
chartView.drawGridBackgroundEnabled = false
chartView.xAxis.labelTextColor = .white
chartView.rightAxis.labelTextColor = .white
chartView.dragEnabled = true
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = true
chartView.drawMarkers = true
chartView.legend.enabled = false
chartView.leftAxis.enabled = false
chartView.leftAxis.spaceTop = 0.3
chartView.leftAxis.spaceBottom = 0.3
chartView.leftAxis.axisMinimum = 0
chartView.rightAxis.enabled = true
chartView.rightAxis.axisLineColor = .primaryBlue
chartView.drawGridBackgroundEnabled = false
let candleResponse = MasterData.shared.candleResponse
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
let dates = candleResponse
.map { Date(timeIntervalSince1970: ($0.first ?? 0)/1000) }
.map { dateFormatter.string(from: $0) }
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dates)
chartView.xAxis.labelPosition = .bottom
let yVals1 = (0..<candleResponse.count).map { (i) -> CandleChartDataEntry in
return CandleChartDataEntry(x: Double(i),
shadowH: candleResponse[i][2],
shadowL: candleResponse[i][3],
open: candleResponse[i][1],
close: candleResponse[i][4])
}
let set1 = CandleChartDataSet(entries: yVals1)
set1.setDrawHighlightIndicators(true)
set1.drawVerticalHighlightIndicatorEnabled = true
set1.drawHorizontalHighlightIndicatorEnabled = true
set1.axisDependency = .right
set1.setColor(UIColor(white: 80/255, alpha: 1))
set1.drawIconsEnabled = true
set1.shadowColor = .darkGray
set1.shadowWidth = 0.7
set1.decreasingColor = .red
set1.decreasingFilled = true
set1.increasingColor = UIColor(red: 122/255, green: 242/255, blue: 84/255, alpha: 1)
set1.increasingFilled = true
set1.neutralColor = .blue
let data = CandleChartData(dataSet: set1)
data.setDrawValues(false)
chartView.data = data
}
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]".
I'm using Horizontal bar chart and all the values I have is between 10 and 30.
I want to display the values in the chart, however the drawValuesEnabled depends on maxVisibleCount (from iOS-charts source)
From BaseDataSet.swift
/// Set this to true to draw y-values on the chart.
///
/// - note: For bar and line charts: if `maxVisibleCount` is reached, no values will be drawn even if this is enabled.
open var drawValuesEnabled = true
I see maxVisibleCount is set as 100 by default in BarLineChartViewBase.swift.
I tried to set maxVisibleCount to 5 in BarLineChartViewBase.swift.
This does not update the graph.
I use Pods to use iOS charts in my project.
Chart Settings:
chartDataSet.drawValuesEnabled = true
chartDataSet.drawIconsEnabled = true
chartDataSet.visible = true
chartDataSet.colors = barColors
let data = BarChartData(dataSet: chartDataSet)
let formatter = NumberFormatter()
formatter.numberStyle = .none
formatter.maximumFractionDigits = 0
formatter.multiplier = 1.0
data.setValueFormatter(DefaultValueFormatter(formatter: formatter))
data.setDrawValues(true)
let xaxis = hBarChart.xAxis
xaxis.labelCount = 24
xaxis.valueFormatter = IndexAxisValueFormatter(values:hours)
xaxis.granularity = 1
xaxis.labelPosition = .bottom
xaxis.drawAxisLineEnabled = false
xaxis.drawGridLinesEnabled = false
xaxis.labelTextColor = .darkGray
let gridColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 0.8)
let yaxis = hBarChart.leftAxis
yaxis.drawGridLinesEnabled = true
yaxis.gridColor = gridColor
yaxis.enabled = true
yaxis.labelPosition = .insideChart
yaxis.labelTextColor = .darkGray
hBarChart.rightAxis.enabled = true
hBarChart.rightAxis.gridColor = gridColor
hBarChart.backgroundColor = .white
hBarChart.translatesAutoresizingMaskIntoConstraints = false
hBarChart.setVisibleYRangeMaximum(5, axis: .left)
hBarChart.setVisibleXRangeMaximum(5)
hBarChart.drawMarkers = true
hBarChart.extraTopOffset = 20
hBarChart.extraBottomOffset = 20
hBarChart.isUserInteractionEnabled = false
hBarChart.drawValueAboveBarEnabled = true
hBarChart.chartDescription?.text = "SDN Trend"
hBarChart.animate(yAxisDuration: 1, easingOption: .easeOutBounce)
hBarChart.legend.enabled = false
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