简体   繁体   中英

How to overlay the x and y axis on top of line chart using MPAndroid chart library in Android?

I have read the whole documentation of MPAndroid Chart library but haven't found anything to do so. I actually want my chart to extend to full width of screen so that the x and y axis comes to top of the chart. I have attached an image to show how should it look exactly.

该图表应该看起来像屏幕的上半部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">

<com.github.mikephil.charting.charts.LineChart
    android:id="@+id/progress_graph"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="66"/>



    <FrameLayout
        android:id="@+id/workout_frame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="17"
        android:background="@color/dashboard_item1"
        android:foreground="?attr/selectableItemBackground"
        android:padding="15dp">


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="36dp"
                android:layout_height="match_parent"
                android:src="@drawable/nav_workout_icon" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:layout_marginLeft="10dp">

                <TextView
                    android:id="@+id/workout_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/workout_title"
                    android:textColor="@color/text_primary"
                    android:textSize="20sp"/>

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

                    <TextView
                        android:id="@+id/workout_calories_consumed"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/workout_calories_consumed_sample"
                        android:textColor="@color/text_primary"
                        android:textSize="26sp"
                        android:textStyle="bold"/>

                    <TextView
                        android:id="@+id/calorie_unit_workout_left"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/calorie_unit"
                        android:textColor="@color/text_secondary"
                        android:textSize="20sp"
                        android:layout_marginLeft="7dp"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="right"
            android:gravity="center">

            <TextView
                android:id="@+id/workout_calories_total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/workout_calories_total_sample"
                android:textColor="@color/accent"
                android:textSize="22sp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/workout_unit_calorie_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/calorie_unit"
                android:textColor="@color/accent"
                android:textSize="18sp"/>
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/diet_frame"
        android:foreground="?attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="17"
        android:background="@color/dashboard_item2"
        android:padding="15dp">


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="36dp"
                android:layout_height="match_parent"
                android:src="@drawable/nav_diet_icon" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:layout_marginLeft="10dp">

                <TextView
                    android:id="@+id/diet_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/diet_title"
                    android:textColor="@color/text_primary"
                    android:textSize="20sp"/>

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

                    <TextView
                        android:id="@+id/diet_calories_consumed"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/diet_calories_consumed_sample"
                        android:textColor="@color/text_primary"
                        android:textSize="26sp"
                        android:textStyle="bold"/>

                    <TextView
                        android:id="@+id/calorie_unit_diet_left"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/calorie_unit"
                        android:textColor="@color/text_secondary"
                        android:textSize="20sp"
                        android:layout_marginLeft="7dp"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_gravity="right"
            android:gravity="center">

            <TextView
                android:id="@+id/diet_calories_total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/diet_calories_total_sample"
                android:textColor="@color/accent"
                android:textSize="22sp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/diet_unit_calorie_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/calorie_unit"
                android:textColor="@color/accent"
                android:textSize="18sp"/>
        </LinearLayout>

    </FrameLayout>

    <TextView
        android:id="@+id/sync_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textColor="@android:color/black"
        android:visibility="gone" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:visibility="gone">

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="@android:color/black"
            android:visibility="gone" />
    </ScrollView>
</LinearLayout>

Edited: This is my layout xml code.

You can setPosition(YAxisLabelPosition pos): Sets the position where the axis-labels should be drawn. Either INSIDE_CHART or OUTSIDE_CHART.

Also you can set Bottom or Top position.

 XAxis xAxis = chart.getXAxis();
 xAxis.setPosition(XAxisPosition.TOP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM