Label above bar not showing for horizontal bars ios-charts - ios

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

Related

Can I show all the markers using iOS charts?

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.

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

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 xValuePosition and yValuePosition properly in Pie Chart using Swift?

I am trying to put labels outside properly and lines to point them but the lines are overlapping and labels at the sides are not displayed completely. If length of the line is increased the labels are cut completely.
This is what I'm getting now.
What I want is
Code:
`class ViewController: UIViewController{
let pieView : PieChartView = {
let set = PieChartView()
set.translatesAutoresizingMaskIntoConstraints = false
set.drawHoleEnabled = false
set.chartDescription?.text = ""
return set
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationItem.title = "Pie"
setChart()
chartData()
}
let surveyData = ["cat": 20, "dog": 30, "both": 5, "neither": 45]
func setChart(){
view.addSubview(pieView)
pieView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
pieView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
pieView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5).isActive = true
pieView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5).isActive = true
}
func chartData(){
var data = [PieChartDataEntry]()
for (key,value) in surveyData{
let entry = PieChartDataEntry(value: Double(value), label: key)
data.append(entry)
}
let chart = PieChartDataSet(values: data, label: "")
chart.colors = ChartColorTemplates.material()
chart.sliceSpace = 2
chart.selectionShift = 5
chart.xValuePosition = .outsideSlice
chart.yValuePosition = .outsideSlice
chart.valueTextColor = .black
chart.valueLineWidth = 0.5
chart.valueLinePart1Length = 0.2
chart.valueLinePart2Length = 4
chart.drawValuesEnabled = true
let chartData = PieChartData(dataSet: chart)
pieView.data = chartData
}}`
Try this with your data set:
chart.selectionShift = 0

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