Mpandroidchart: How can I change the Chart orientation (rotation)?

Created on 8 May 2015  路  12Comments  路  Source: PhilJay/MPAndroidChart

I need to change de chart orientation to landscape without need to change all the Activity orientation like "getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);".

How can i do this? I cant invert the axis values because the Y axis only accepts float values.

All 12 comments

Just rotate the View in xml (rotation in degrees):

 <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:rotation="90" />

@PhilJay I've stumbled on this issue by myself and to be honest rotation=90 is not working properly. View is not rendering correctly (it's cutting chart).

Any solution ?

still no solution for this issue ?

@cagriyalcinn Instead of rotating 90 degree.. we can go for horizontal Bar chart... It will work..

@AndroPlus thanks for advice. So what about the LineChart ?

@cagriyalcinn I didn't tried that.. Thanks

@AndroPlus But if we go for HorizontalBarChart, how do we convert to line instead of bar?

Is this exactly what you want?


screenshot_20180727-165543

public class MultipleLineChartTest extends AppCompatActivity {

    private LineChart lineChart;

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

        lineChart = findViewById(R.id.activity_multiple_line_chart_test_line_chart);

        lineChart.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    public void onGlobalLayout() {

                        lineChart.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        int offset = (lineChart.getHeight() - lineChart.getWidth()) / 2;

                        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) lineChart.getLayoutParams();
                        layoutParams.width = lineChart.getHeight();
                        layoutParams.height = lineChart.getWidth();
                        lineChart.setLayoutParams(layoutParams);

                        lineChart.setTranslationX(-offset);
                        lineChart.setTranslationY(offset);

                        initLineChart();
                    }
                });
    }

    private void initLineChart() {

        lineChart.getXAxis().setAxisMaximum(15f);
        lineChart.getAxisLeft().setAxisMaximum(50f);
        lineChart.getAxisRight().setAxisMaximum(50f);

        int entrySize = 15;
        LineDataSet line1 = new LineDataSet(getRandomEntries(entrySize), "Line chart 1");
        line1.setColor(Color.RED);
        line1.setCircleColor(Color.RED);
        line1.setLineWidth(2f);
        line1.setCircleRadius(2f);
        line1.setDrawCircles(false);
        line1.setDrawValues(false);

        LineDataSet line2 = new LineDataSet(getRandomEntries(entrySize), "Line chart 2");
        line2.setColor(Color.GREEN);
        line2.setCircleColor(Color.GREEN);
        line2.setLineWidth(2f);
        line2.setCircleRadius(2f);
        line2.setDrawCircles(false);
        line2.setDrawValues(false);

        LineDataSet line3 = new LineDataSet(getRandomEntries(entrySize), "Line chart 3");
        line3.setColor(Color.BLUE);
        line3.setCircleColor(Color.BLUE);
        line3.setLineWidth(2f);
        line3.setCircleRadius(2f);
        line3.setDrawCircles(false);
        line3.setDrawValues(false);

        LineData lineData = new LineData(line1, line2, line3);
        lineChart.setData(lineData);
        lineChart.invalidate();
    }

    private List<Entry> getRandomEntries(int entrySize) {
        List<Entry> entries = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < entrySize; i++) {
            entries.add(new Entry(i, 15 + 5 * random.nextFloat()));
        }
        return entries;
    }
}

The layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MultipleLineChartTest">


    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/activity_multiple_line_chart_test_line_chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#abc"
        android:rotation="90" />

</FrameLayout>

Labels are not coming by using this code? can anyone help

i get it done!

Labels are also rotated 180 degrees. Know how I can fix this? @Amritvirk

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chathudan picture chathudan  路  3Comments

thanhcly920 picture thanhcly920  路  3Comments

vishvendu picture vishvendu  路  3Comments

blotfi picture blotfi  路  3Comments

Giammaofwar picture Giammaofwar  路  3Comments