Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
115
Line chart with marker
posted

Hi. I need to create a simple line chart with marker (vertical line). The app will need to update marker position (via timer or some user actions).

What is the bes way to achieve that?

Will that be ok to use second line series (with constant X value) as a marker? If so can anyone please provide some code to create such series?

Thanks!

  • 115
    Verified Answer
    posted

    I have not found solution but the following work around was helpful.

    1. I put chart with my line series to the RelativeLayout

    2. I've added following View (vertical red line)

    <View
    android:id="@+id/marker"
    android:layout_width="2dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/dataChart"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignTop="@+id/dataChart"
    android:background="#a00"
    />

    3. The following code is used to make my line act as a marker:

    View marker = findViewById(R.id.marker);
    RelativeLayout.LayoutParams lparams = (RelativeLayout.LayoutParams) marker
    .getLayoutParams();

    //we should initialize yLabelWidth when constructing the chart. I use something like yLabelWidth = NumericYAxis.labelAreaRect.width();

    int margin0 = (int) yLabelWidth; 

    //marker index is index of the marked value of the series' data source

    int mleft = (int) ((dataChart.getWidth() - margin0) * ((float) markerIndex / (float) seriesDataSource
    .size()));

    mleft += margin0;

    Log.i("mleft", String.valueOf((mleft)));

    lparams.setMargins(mleft, 0, 0, 0);
    marker.setLayoutParams(lparams);