Santu Reddy
Santu Reddy

Reputation: 87

How do update decoview chart with live data?

I have looked at the github resource and also here but I'm unable to get my graph to display live data. Here's my code.

public class Blink_HR extends Fragment {
TextView textView;
LinearLayout linearLayout;
DecoView mDecoView;
private int mBackIndex;
private int mSeries1Index;
private int mSeries2Index;
private int mSeries3Index;
private final float
        mSeriesMax = 50f;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.blink_hr, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    textView = (TextView) getActivity().findViewById(R.id.meditation);
    linearLayout = (LinearLayout) getView().findViewById(R.id.blinkHR);
    mDecoView = (DecoView)getActivity().findViewById(R.id.dynamicArcView);

    mDecoView.addEvent(new DecoEvent.Builder(mSeriesMax)
            .setIndex(mBackIndex)
            .setDuration(10)
            .build());


    SeriesItem seriesItem = new SeriesItem.Builder(Color.parseColor("#FFE2E2E2"))
            .setRange(0, mSeriesMax, 0)
            .setInitialVisibility(true)
            .build();

    mBackIndex = mDecoView.addSeries(seriesItem);
}




void update(int id, int value) {
    String heart = String.valueOf(value);
    Log.d("Blink Hai", heart);

    if (value > 0 && mDecoView!=null && mSeries1Index!=0) {
        SeriesItem seriesItem = new SeriesItem.Builder(Color.parseColor("#FFFF8800"))
                .setRange(0, (float)value, 0)
                .setInitialVisibility(false)
                .build();
        mSeries1Index = mDecoView.addSeries(seriesItem);
    }

    if (mDecoView != null) {
        mDecoView.addEvent(new DecoEvent.Builder(42.4f)
                .setIndex(mSeries1Index)
                .setDelay(3250)
                .build());
        mDecoView.executeReset();
    }
}

}

My update function is called every 1 second and I would expect the graph to update this data in real-time. However all I get is a blimp on the screen.

Upvotes: 0

Views: 1256

Answers (1)

BrentM
BrentM

Reputation: 5757

There is a couple of issues with the code

  • An event is added to the series mBackIndex before mBackIndex has been initialized

  • Update is triggered every 1 second but a 3.25 second delay is added to the event before it will be processed

  • The event on update always sets the DecoView position to 42.4

  • executeReset() is called every time the update is triggered, this resets all series in the charts and cancels all pending animations

Here is some sample code that will update a DecoView every 1 second to a random position with animation

public class FauxFitActivity extends AppCompatActivity {

    private DecoView mDecoView;
    private int mSeries1Index;
    private final float mSeriesMax = 50f;
    private Handler mHandler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            update();
            mHandler.postDelayed(this, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faux_fit);

        mDecoView = (DecoView) findViewById(R.id.dynamicArcView);
        createDataSeries1();

        // Start the timer
        mHandler.post(runnable);

    }

    private void createDataSeries1() {
        final SeriesItem seriesItem = new SeriesItem.Builder(Color.parseColor("#FFFF8800"))
                .setRange(0, mSeriesMax, 0)
                .setInitialVisibility(false)
                .build();

        mSeries1Index = mDecoView.addSeries(seriesItem);
    }

    private void update() {
        final Random rand = new Random();
        int newPosition = rand.nextInt((int)mSeriesMax);

        mDecoView.addEvent(new DecoEvent.Builder(newPosition).setIndex(mSeries1Index).setDuration(1000).build());
    }
}

Upvotes: 2

Related Questions