Frederik Petri
Frederik Petri

Reputation: 481

MPAndroidChart - How can I update LimitLine position?

I am trying to dynamically update the position of a LimitLine. For now, I can only set the position when I create the line. How can I change the position later?

        LimitLine ll = new LimitLine(0f, "");
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.addLimitLine(ll);

        // Set new position here...
        

Upvotes: 0

Views: 169

Answers (1)

Cook Chen
Cook Chen

Reputation: 537

enter image description here

Update your limitline and invoke chart.invalidate() then the position is changed. In the demo code LineChartActivity.java, onProgressChanged, by this way, the limitline will changed automatically:

    @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // redraw
    seekBarY.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (seekBarY.getProgress() < seekBarY.getMax()) {
                seekBarY.setProgress(seekBarY.getProgress() + 1);
            }
        }
    }, 500);

    double percent = seekBarY.getProgress() * 1.0 / seekBarY.getMax();

    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.removeAllLimitLines();
    LimitLine ll = new LimitLine((float)(percent * 50), "");
    ll.setLineColor(Color.BLACK);
    ll.setLineWidth(5f);
    leftAxis.addLimitLine(ll);
    chart.invalidate();
}

Upvotes: 1

Related Questions