Create a MarkerView when user clicks on Chart

Tophat Gordon picture Tophat Gordon · Jul 5, 2016 · Viewed 9.2k times · Source

I have searched and searched for how to display the MarkerView when the user clicks on a bar in a bar chart using Charts (was iOS-charts) for Swift.

The documentation states the library is capable of "Highlighting values (with customizable popup-views)" using MarkerViews, but I don't know how to show one.

I want a little tool tip to display, like the image below, when the user clicks on a bar in the bar chart.

Tool tip on bar graph: enter image description here

I have the chartValueSelected function ready which fires when a bar is selected.

Answer

Edison picture Edison · Jul 5, 2016

So you are using Charts right?

Did you check BallonMarker.swift?

/ChartsDemo/Classes/Components/BallonMarker.swift

Swift

let marker:BalloonMarker = BalloonMarker(color: UIColor.redColor(), font: UIFont(name: "Helvetica", size: 12)!, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0))
marker.minimumSize = CGSizeMake(75.0, 35.0)
chartView.marker = marker

Swift 3 Update

let marker:BalloonMarker = BalloonMarker(color: UIColor.black, font: UIFont(name: "Helvetica", size: 12)!, textColor: UIColor.white, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0))
marker.minimumSize = CGSize(width: 75.0, height: 35.0)
chartView.marker = marker

Objective-C

BalloonMarker *marker = [[BalloonMarker alloc] initWithColor:[UIColor colorWithRed:14.0/255.0 alpha:1.0] font:[UIFont systemFontOfSize:12.0] insets: UIEdgeInsetsMake(7.0, 7.0, 7.0, 7.0)];
marker.minimumSize = CGSizeMake(75.f, 35.f);
_chartView.marker = marker;