iOS Charts not showing Y values - ios

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

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.

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
}

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