hg1
hg1

Reputation: 153

Scrollable LineChart in MPAndroidChart

I am creating an application and use the MPAndroidChart library to draw line chart. I am putting Dates on x-axis and number on y-axis. I am not able to scroll line chart horizontally when there are more data. My snippet is here:

    XAxis xAxis = chart.getXAxis();
    xAxis.setTextSize(8f);
    xAxis.setTextColor(Color.BLACK);
    xAxis.setDrawGridLines(false);
    xAxis.setPosition(xAxis.getPosition());
    xAxis.setDrawAxisLine(true);
 //   xAxis.setSpaceBetweenLabels(1);
    xAxis.setLabelRotationAngle(-90.0f);

    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setTextColor(Color.BLACK);
    leftAxis.setDrawGridLines(true);
    leftAxis.setValueFormatter(new DefaultYAxisValueFormatter(0));

    YAxis rightAxis = chart.getAxisRight();
    rightAxis.setTextColor(Color.BLACK);
    rightAxis.setDrawGridLines(true);
    rightAxis.setValueFormatter(new DefaultYAxisValueFormatter(0));
     LineDataSet barDataSet1 = null;
    if (valueSet1 != null && valueSet1.size() > 0) {
        barDataSet1 = new LineDataSet(valueSet1, "Sended");
        barDataSet1.setColor(Color.BLACK);
        barDataSet1.setCircleColor(Color.BLACK);
        barDataSet1.setCircleSize(2.0f);
        barDataSet1.setLineWidth(1.0f);
        barDataSet1.setValueTextColor(Color.BLACK);
        barDataSet1.setValueTextSize(10.0f);
        barDataSet1.setDrawCubic(true);

    }


    ArrayList dataSets = new ArrayList<>();
    if (barDataSet1 != null)
        dataSets.add(barDataSet1);

    LineData data = new LineData(xDatelist, dataSets);
    if (data != null)
        chart.setData(data);
    chart.setPinchZoom(true);

    chart.setScrollContainer(true);
    chart.setHorizontalScrollBarEnabled(true);
    chart.setScaleXEnabled(true);
    chart.setDescription("Send/Received files");
    chart.invalidate();

Still I am not able to scroll graph when there is more data then date gets compressed on x-axis. How can I solve this issue?

Upvotes: 4

Views: 7896

Answers (5)

Rahul Makwana
Rahul Makwana

Reputation: 31

You should just put this code and your problem and issue will be Resolved

use this function:-

chart.setScrollx();

In this code you can put any value you want and your graph x-axis line will scroll.

Example : chart.setScrollX(50); (here your value is 50 and your graph x-axis line move or scroll after 50 points)

Upvotes: 0

Ankit Lathiya
Ankit Lathiya

Reputation: 199

chart.setData(...); // first set the data.

// now modify viewport chart.setVisibleXRangeMaximum(7); // allow 7 values to be displayed at once on the x-axis, not more.

Upvotes: 2

Viral Patel
Viral Patel

Reputation: 1316

This worked for me.

// we can modify viewport, Scrolling the data from right to left
// allow 30 values to be displayed at once on the x-axis not allow more value 
chart.setVisibleXRangeMaximum(30); 
// set the left edge of the chart to x-index 20
// moveViewToX(...) also calls invalidate()
chart.moveViewToX(20); 

Check this for more information read this

I hope it'll help you....!

Upvotes: 1

Beena
Beena

Reputation: 2354

You can add your chart inside HorizontalScrollView to make it scroll. But to do so, you first need to calculate your chart view's desired height and width and programatically set it to chartview.

In the below code, I have temporary set it to some default value to check scrolling behaviour.

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_above="@+id/seekBar1"
    android:fillViewport="true"
    android:scrollbars="horizontal"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical" >

        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart1"
            android:layout_width="1000dp"
            android:layout_height="300dp"
            />
    </LinearLayout>
</HorizontalScrollView>

Upvotes: 5

Divya
Divya

Reputation: 245

A better approach would be to use the following methods with your chart:

chart.moveViewToX()

chart.setVisibleXRangeMaximum()

Upvotes: 1

Related Questions