How To Set Labels And Values In Horizontal Bar Charts?
I am using MPAndroidChart. Please check my below code activity_main.xml  Copy
 
 
2) Use a percentage value to set the width
public class MainActivity extends AppCompatActivity {
    private TextView mLowLabel, mMidLabel, mHighLabel;
    private BarView mLowBar, mMidBar, mHighBar;
    //Some sample percentage values
    private final int low = 17;
    private final int mid = 90;
    private final int high = 34;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLowBar = (BarView) findViewById(R.id.low_bar);
        mMidBar = (BarView) findViewById(R.id.mid_bar);
        mHighBar = (BarView) findViewById(R.id.high_bar);
        mLowLabel = (TextView) findViewById(R.id.low_text);
        mMidLabel = (TextView) findViewById(R.id.mid_text);
        mHighLabel = (TextView) findViewById(R.id.high_text);
        mLowBar.set(Color.BLUE, low);
        mMidBar.set(Color.RED, mid);
        mHighBar.set(Color.GREEN, high);
        mLowLabel.setText(getPercentage(low));
        mMidLabel.setText(getPercentage(mid));
        mHighLabel.setText(getPercentage(high));
    }
    private String getPercentage(int per) {
        return per + "%";
    }
}
3) Set it up in XML. The weights of the bars are set to 1 so they fill all available space, which will constitute the "100%" reading.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.github.ppartisan.graphish.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Low"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>
            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/low_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />
            <TextView
                android:id="@+id/low_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mid"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>
            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/mid_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />
            <TextView
                android:id="@+id/mid_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="High"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>
            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/high_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />
            <TextView
                android:id="@+id/high_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
Here is an image of the output:

Post a Comment for "How To Set Labels And Values In Horizontal Bar Charts?"