X-Axis Chart label in swift - ios

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.

Related

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

danielgindi/iOSCharts library : x axis label in horizontal bar chart

with the following code
barchart.xAxis.labelTextColor = UIColor.white
barchart.xAxis.labelPosition = .topInside
barChart.xAxis.setLabelCount(7, force: true)
Issue: x axis grid get destructed
barchart.xAxis.labelTextColor = UIColor.white
barchart.xAxis.labelPosition = .topInside
Issue: Right side Label gets hidden
Solution Required:
Need Following to be fixed:
*Grid should be aligned
*Label should be viewed
my code is :
barChart.legend.enabled = false
barChart.xAxis.labelTextColor = UIColor.white
barChart.xAxis.labelPosition = .topInside
barChart.leftAxis.drawAxisLineEnabled = true
barChart.leftAxis.enabled = true
barChart.leftAxis.drawGridLinesEnabled = false
barChart.rightAxis.drawAxisLineEnabled = false
barChart.rightAxis.drawGridLinesEnabled = true
barChart.rightAxis.drawLabelsEnabled = true
barChart.rightAxis.labelFont = NSUIFont.systemFont(ofSize: 13.0)
barChart.xAxis.labelFont = NSUIFont.systemFont(ofSize: 16.0)
barChart.rightAxis.labelTextColor = UIColor.white
barChart.leftAxis.axisMinimum = 0.0
barChart.rightAxis.axisMinimum = 0.0
barChart.rightAxis.drawLabelsEnabled = false
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i),yValues: [values[i]])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "HB2")
let chartData = BarChartData(dataSet: chartDataSet)
chartData.setValueFormatter(DefaultValueFormatter(formatter: formatter))
barChart.xAxis.valueFormatter = IndexAxisValueFormatter(values:dataPoints)
barChart.data = chartData
First of all label position should be barchart.xAxis.labelPosition = .top and I do not understand why grids are not aligned but if you use following code it should be right aligned it worked for me.
chartView.legend.enabled = false
// chartView.xAxis.labelTextColor = UIColor.white
chartView.xAxis.labelPosition = .top
chartView.leftAxis.drawAxisLineEnabled = true
chartView.leftAxis.enabled = true
chartView.leftAxis.drawGridLinesEnabled = false
chartView.rightAxis.drawAxisLineEnabled = false
chartView.rightAxis.drawGridLinesEnabled = true
chartView.rightAxis.drawLabelsEnabled = true
chartView.rightAxis.labelFont = NSUIFont.systemFont(ofSize: 13.0)
chartView.xAxis.labelFont = NSUIFont.systemFont(ofSize: 16.0)
chartView.rightAxis.labelTextColor = UIColor.white
chartView.leftAxis.axisMinimum = 0.0
chartView.rightAxis.axisMinimum = 0.0
chartView.rightAxis.drawLabelsEnabled = false
var dataEntries: [ChartDataEntry] = []
for i in 0..<unitsSold.count {
let dataEntry = BarChartDataEntry(x: Double(i),yValues: [unitsSold[i]])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "HB2")
let chartData = BarChartData(dataSet: chartDataSet)
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
chartView.data = chartData
chartView.animate(yAxisDuration: 2.5)
I have removed your first formatter I do not understand why you used it but it gave me following chart.
as per my exprience you need to add lableCount in your chart configuration like below so that it will calculate proper xAxis and show it on your chart
horiBarChartView.xAxis.labelCount = <yourXAxisArray>.count
If we are not setting this count some time it will not able to generate proper xAxis position on chart.
So try this once and let me know its working or not.

How to set maxVisibleCount variable in iOS charts

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

Label above bar not showing for horizontal bars ios-charts

I am implementing ios-charts in my project for a horizontal bar chart. I need the values of the individual bars to appear above the bars (since the chart is horizontal, the label should appear on the right of the bar). However, I am unable to make it work. The code is as follows:
func setupHorizontalChartView() {
self.chartView.chartDescription?.enabled = false
chartView.dragEnabled = false
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = false
let chartXAxis = chartView.xAxis
chartXAxis.labelPosition = .bottom
let chartLeftAxis = chartView.leftAxis
chartLeftAxis.labelFont = NSUIFont(name: "Helvetica", size: 12)!
chartLeftAxis.labelTextColor = UIColor.darkGray
chartView.rightAxis.enabled = false
chartView.drawValueAboveBarEnabled = true
let formatter = BarChartFormatter()
formatter.setValues(values: self.months)
chartView.xAxis.valueFormatter = formatter
chartView.xAxis.setLabelCount(self.months.count - 1, force: false)
chartView.xAxis.drawGridLinesEnabled = false
chartView.leftAxis.drawGridLinesEnabled = false
chartView.xAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawLabelsEnabled = false
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
chartDataSet.drawValuesEnabled = true
let chartData = BarChartData(dataSet: chartDataSet)
chartData.setDrawValues(true)
chartView.data = chartData
}
drawValueAboveBarEnabled has been set to true for the chartView. When I use the same code for BarChartView instead of HorizontalBarChartView, the values of the bars get shown above the bars. However, it doesn't work for horizontal bar chart view.
Setting
chartView.leftAxis.axisMinimum = 0
helped me.
I've open a discussion for it here GitHub

Resources