I don't know why chart is nil.
let chart = self.chartView
So lines below are false:
else if chart != nil && origin.x + width + offset.x > chart!.bounds.size.width
{
offset.x = chart!.bounds.size.width - origin.x - width - padding
}
else if chart != nil && origin.y + height + offset.y > chart!.bounds.size.height
{
offset.y = chart!.bounds.size.height - origin.y - height - padding
}
I change to:
else if origin.x + width + offset.x > UIScreen.main.bounds.width
{
offset.x = UIScreen.main.bounds.width - origin.x - width - padding
}
else if origin.y + height + offset.y > UIScreen.main.bounds.height
{
offset.y = UIScreen.main.bounds.height - origin.y - height - padding
}
and it is working. Any suggestion why chart is nil?
I guess there is a bug in BalloonMarker initializer. It needs one more parameter for chartView.
public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets, chart: Charts.ChartViewBase)
{
super.init()
self.color = color
self.font = font
self.textColor = textColor
self.insets = insets
self.chartView = chart
_paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
_paragraphStyle?.alignment = .center
}
When initializing marker, pass your chart as last parameter:
let marker = BalloonMarker(color: UIColor.red, font: UIFont.systemFont(ofSize:12.0), textColor: UIColor.darkText, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0), chart: chartView)
and now self.chart is not nil
you have forget
chartView.marker = marker
marker.chartView = chartView
try my balloon marker
let marker = RectMarker(color: #colorLiteral(red: 0.9994240403, green: 0.9855536819, blue: 0, alpha: 1), font: NSFont.systemFont(ofSize: CGFloat(12.0)), insets: NSEdgeInsets(top: 8.0, left: 8.0, bottom: 4.0, right: 4.0))
marker.chartView = chartView
marker.minimumSize = CGSize(width: CGFloat(60.0), height: CGFloat(30.0))
chartView.marker = marker
and change
set1 = LineChartDataSet(values: yVals1, label: "what you want")


Thanks, I forget marker.chartView = chartView.
I guess it is better to set this value in initializer, like me. This is safer.
Thanks again
be careful if creating a retain cycle here marker.chartView = chartView
MarkerImage class have @objc open weak var chartView: ChartViewBase?' it avoid the retain cycle problem , no ?
Finally one Line of code save 馃榾
marker.chartView = chartView
Most helpful comment
you have forget
chartView.marker = marker
marker.chartView = chartView