I am using iOS- Charts, and i created bar chart. I also conformed the delegate so that i can get callbacks when i am tapped on a bar but the problem is when i tapped in the non bar area also the delegate method is getting called and getting highlighted. I am using Swift3.
var mMonths : [String]?
var mAverage : Double?
var mValues : [Double]?
override func viewDidLoad() {
super.viewDidLoad()
mMonths = ["A","B","C","D","F","G","H","I","J","K","L","M"]
}
override func viewWillAppear(_ animated: Bool) {
mAverage = 50
mValues = [20,40.0,0,50,10,100,55,80,40,10.50,80,35]
mBarChartView.delegate = self
setChart(dataPoints: mMonths!, values: mValues!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// For setting up the data and customizing the Chart
private func setChart(dataPoints :[String] , values : [Double])
{
var dataEntries : [BarChartDataEntry] = []
var colors : [UIColor] = []
let belowAverageColor : UIColor = UIColor.blue
let aboveAverageColor : UIColor = UIColor.blue.withAlphaComponent(0.5)
let averageColor : UIColor = UIColor.lightGray
for i in 0..<dataPoints.count
{
let eachValue : Double = values[i]
var entry : BarChartDataEntry?
if eachValue == 0
{
entry = BarChartDataEntry.init(x: Double(i), yValues: [mAverage!])
colors.append(averageColor)
}
else if eachValue <= mAverage!
{
entry = BarChartDataEntry.init(x: Double(i), yValues: [eachValue,mAverage!-eachValue])
colors.append(belowAverageColor)
colors.append(averageColor)
}
else
{
entry = BarChartDataEntry.init(x: Double(i), yValues: [mAverage!,eachValue-mAverage!])
colors.append(belowAverageColor)
colors.append(aboveAverageColor)
}
dataEntries.append(entry!)
}
let dataSet = BarChartDataSet.init(values: dataEntries, label: "")
// removed value on top of each bar
dataSet.drawValuesEnabled = false
// removing the highlight on bar tapped
dataSet.highlightAlpha = 0
// assigning colors to bar
dataSet.colors = colors
let data = BarChartData(dataSet: dataSet)
mBarChartView.data = data
// Skipping labels in between
mBarChartView.xAxis.setLabelCount(mMonths!.count, force: false)
// setting data on X axis
mBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter.init(values: mMonths!)
// color of labels on xaxis
mBarChartView.xAxis.labelTextColor = UIColor.black
// setting maximum value of the graph
mBarChartView.leftAxis.axisMaximum = 100
mBarChartView.rightAxis.axisMaximum = 100
mBarChartView.leftAxis.axisMinimum = 0.0
mBarChartView.rightAxis.axisMinimum = 0.0
// removing grid lines
mBarChartView.leftAxis.drawGridLinesEnabled = false
mBarChartView.rightAxis.drawGridLinesEnabled = false
mBarChartView.xAxis.drawGridLinesEnabled = false
// removing left and right axis
mBarChartView.leftAxis.enabled = false
mBarChartView.rightAxis.enabled = false
// removing bottom line
mBarChartView.xAxis.drawAxisLineEnabled = false
// Emptying the description label
mBarChartView.chartDescription?.text = ""
// placing the X axis label to bottom
mBarChartView.xAxis.labelPosition = .bottom
// bottom information about the bars is hidden
mBarChartView.legend.enabled = false
// Disabling the Zooming
mBarChartView.doubleTapToZoomEnabled = false
mBarChartView.pinchZoomEnabled = true
mBarChartView.scaleXEnabled = true
mBarChartView.scaleYEnabled = false
mBarChartView.highlightFullBarEnabled = true
}
Thank you