Charts: Problem with BalloonMarker in lineChart

Created on 17 Oct 2017  路  8Comments  路  Source: danielgindi/Charts

I am using this version of BalloonMarker.
You see 3 images below. The last image doesn't show very well. What change in codes can solve my problem?

screen shot 2017-10-17 at 10 10 00 am
screen shot 2017-10-17 at 10 09 53 am
screen shot 2017-10-17 at 10 10 05 am

Most helpful comment

you have forget
chartView.marker = marker
marker.chartView = chartView

All 8 comments

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

RectMarker.swift.zip

    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")

capture d ecran 2017-10-17 a 13 24 39

capture d ecran 2017-10-17 a 13 24 32

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JW00332 picture JW00332  路  4Comments

kirti0525 picture kirti0525  路  3Comments

PrashantKT picture PrashantKT  路  3Comments

valeIT picture valeIT  路  3Comments

Bharati555 picture Bharati555  路  4Comments