ivnsch / SwiftCharts

Easy to use and highly customizable charts library for iOS
Apache License 2.0
2.53k stars 410 forks source link

Highlight specific bar #317

Closed lsnmarcinr closed 6 years ago

lsnmarcinr commented 6 years ago

Can I highlight specific bar on chart?

I have external spreadsheet (table) with values - when I click on value in table I need to highlight specific bar. How can I do that?

Here is preview:

zrzut ekranu 2017-11-29 o 09 30 19
lsnmarcinr commented 6 years ago

I found solution to do that through ChartPointsViewsLayer, but it's mostly not clearly to understand.

ivnsch commented 6 years ago

Sorry, missed this issue - yes, you can pass the bar layers a view generator as well, you can either re-create the chart each time (with disabled animations) where you generate a highlighted view for the current selection, or you can store the generated views in your own data structure and manipulate these, avoiding having to re-create the chart.

lsnmarcinr commented 6 years ago

@i-schuetz Okay, what if I want to do the same with ChartStackedBarsLayer? I don't have viewGenerator there, I'm using ChartStackedBarModel as barModels. How can I get bar views and highlight them?

I found method

fileprivate func generateChartPointViews(chartPointModels: [ChartPointLayerModel<T>], chart: Chart) -> [ViewWithChartPoint] but it has only private access

ivnsch commented 6 years ago

Stacked bars has also a view generator so the same approach is possible.

These are of course workarounds - ideally the layers should have a method to do this. This isn't difficult to implement, I'll add it to my list but I have no idea when I'll have time - if you want to do a pull request I'd be happy to merge it.

Edit: I'll implement a fix, it's very easy.

ivnsch commented 6 years ago

I uploaded a fix - now the barViews array of the plain/stacked bar layers is exposed, so you can just retrieve the bar from it. You have to use master!

lsnmarcinr commented 6 years ago

Great! Thanks a lot! 👍
@i-schuetz can you merge my pull request? It's required to your last fix.