How to set maxVisibleCount variable in iOS charts - ios

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

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 draw custom views on top and bottom of the candle in candlestick chart

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
}

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]".

iOS Charts not showing Y values

I am writing an iOS App in Swift 4.2. I am using Charts Library to display Horizontal Bar chart. When I open the sample project, it shows the Y Values but, in my project it isn't showing the Y values. I copied the exact code from its sample.
Sample screenshot:
Observe 8.0, 14.0, 4.0, 10.0
My screenshot:
My Code:
#IBOutlet weak var chartView: HorizontalBarChartView!
func setupCharts(){
chartView.noDataTextColor=UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor)
chartView.noDataText = "Chart_NoDataText".localized
chartView.chartDescription?.enabled = false
chartView.dragEnabled = false
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = false
//chartView.delegate = self
chartView.drawBarShadowEnabled = false
chartView.drawValueAboveBarEnabled = true
chartView.maxVisibleCount = 60
chartView.setVisibleXRangeMaximum(4)
chartView.drawGridBackgroundEnabled=false
chartView.extraRightOffset=CGFloat(24)
chartView.isUserInteractionEnabled = false
chartView.legend.enabled=false
chartView.fitBars = true
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = UIFont(name: "Lato-Regular", size: 11)!
xAxis.drawAxisLineEnabled = false
xAxis.drawGridLinesEnabled = false
xAxis.granularity = 1
xAxis.enabled=true
//
let leftAxis = chartView.leftAxis
leftAxis.labelFont = UIFont(name: "Lato-Regular", size: 10)!
leftAxis.drawAxisLineEnabled = false
leftAxis.drawGridLinesEnabled = false
leftAxis.axisMinimum = 0
leftAxis.enabled=false
let rightAxis = chartView.rightAxis
rightAxis.labelFont = UIFont(name: "Lato-Regular", size: 10)!
rightAxis.drawAxisLineEnabled = false
rightAxis.drawGridLinesEnabled = false
rightAxis.axisMinimum = 0
rightAxis.enabled = false
}
func setChartDataWithValues(_ count: Int, values:[Double], labels:[String]) {
let yVals = (0..<count).map { (i) -> BarChartDataEntry in
let val = values[i]
return BarChartDataEntry(x: Double(i), y: val)
}
let barChartDataSet = BarChartDataSet(values: yVals, label: "MF")
barChartDataSet.drawIconsEnabled = false
barChartDataSet.colors=[UIColor(hex: UIConstants.Graphics.BrandTheme.ThemeColor)]
let data = BarChartData(dataSet: barChartDataSet)
data.setValueFont(UIFont(name:"Lato-Regular", size:10)!)
data.setValueTextColor(UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor))
let xAxis = chartView?.xAxis
xAxis?.setLabelCount(3, force: false)
xAxis?.valueFormatter=IndexAxisValueFormatter(values: labels )
chartView?.data = data
}
Calling:
self.setChartDataWithValues(3,values:values, labels: ["Mutual Funds","Fixed Deposits","Savings Account"].reversed())
You should add data.setDrawValues(true) when you create the BarChartData:
...
let data = BarChartData(dataSet: barChartDataSet)
data.setDrawValues(true)
data.setValueFont(UIFont(name:"Lato-Regular", size:10)!)
data.setValueTextColor(UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor))
...
For version 3.2.1 of Charts you also need to remove this line because values are not rendered if drawIconsEnabled is false
barChartDataSet.drawIconsEnabled = false

X-Axis Chart label in swift

I have used danielgindi/Charts in my xcode project. Here the last time label in x-axis not getting showed. How to show the last time in x-axis label. Image attached below.
Below code is used by me :-
let lineChartDataSet = LineChartDataSet(yVals: dataEntriesline, label: nil)
lineChartDataSet.drawCirclesEnabled = false
lineChartDataSet.colors = [NSUIColor(red: CGFloat(228.0/255), green: CGFloat(204.0/255), blue: CGFloat(88.0/255), alpha: 1)]
lineChartDataSet.lineWidth = 2
lineChartDataSet.setDrawHighlightIndicators(false)
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
magneticChart.xAxis.labelPosition = .Bottom
magneticChart.data = lineChartData
magneticChart.animate(xAxisDuration: 3.0, yAxisDuration: 3.0)
magneticChart.legend.enabled = false
magneticChart.descriptionText = ""
magneticChart.data?.setDrawValues(false)
magneticChart.rightAxis.drawLabelsEnabled = false
magneticChart.xAxis.setLabelsToSkip(239)
magneticChart.xAxis.drawLabelsEnabled = true
magneticChart.drawBordersEnabled = true
magneticChart.borderColor = UIColor.blackColor()
dataEntrieslineIntialMarkerMagnetic = dataEntriesline
magneticChart.xAxis.avoidFirstLastClippingEnabled = false
magneticChart.xAxis.drawLabelsEnabled = true
magneticChart.xAxis.avoidFirstLastClippingEnabled = false
This is the reason.
Try to set it to true.

Resources