I am using Charts by Daniel (https://github.com/danielgindi/Charts) to create a bar chart (MacOS). To make this simple, the demo bar chart example within the Master branch does not respond to the user clicking on a bar (same results with my implementation, so I tested with the demo as well. If I can get the Demo working, it should fix mine as well.)
func testDemo()
{
// Do any additional setup after loading the view.
let xArray = Array(1..<10)
let ys1 = xArray.map { x in return sin(Double(x) / 2.0 / 3.141 * 1.5) }
let ys2 = xArray.map { x in return cos(Double(x) / 2.0 / 3.141) }
let yse1 = ys1.enumerated().map { x, y in return BarChartDataEntry(x: Double(x), y: y) }
let yse2 = ys2.enumerated().map { x, y in return BarChartDataEntry(x: Double(x), y: y) }
let data = BarChartData()
let ds1 = BarChartDataSet(entries: yse1, label: "Hello")
ds1.colors = [NSUIColor.red]
data.append(ds1)
let ds2 = BarChartDataSet(entries: yse2, label: "World")
ds2.colors = [NSUIColor.blue]
data.append(ds2)
let barWidth = 0.4
let barSpace = 0.05
let groupSpace = 0.1
data.barWidth = barWidth
self.barChartView.xAxis.axisMinimum = Double(xArray[0])
self.barChartView.xAxis.axisMaximum = Double(xArray[0]) + data.groupWidth(groupSpace: groupSpace, barSpace: barSpace) * Double(xArray.count)
// (0.4 + 0.05) * 2 (data set count) + 0.1 = 1
data.groupBars(fromX: Double(xArray[0]), groupSpace: groupSpace, barSpace: barSpace)
self.barChartView.data = data
self.barChartView.gridBackgroundColor = NSUIColor.white
self.barChartView.chartDescription.text = "Barchart Demo"
}
I have the delegate set in viewDidLoad and the following implementation
extension StatisticsBarChart: ChartViewDelegate {
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight)
{
print(entry)
print(highlight)
}
func chartValueNothingSelected(_ chartView: ChartViewBase)
{
print("nothing selected")
}}
1 out of 10 times a bar is actually selected. But most of the time, "nothing selected" is printed. So the chart is working and the delegate methods are being called. But it is nearly impossible to click on a bar. And sometimes, clicking on one bar highlights another bar.
What am I missing for the selection to be able to work properly? I should be able to select each bar and the correct delegate function gets called every time.