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
publicclassMainActivityextendsAppCompatActivity {
private TextView mLowLabel, mMidLabel, mHighLabel;
private BarView mLowBar, mMidBar, mHighBar;
//Some sample percentage valuesprivatefinalintlow=17;
privatefinalintmid=90;
privatefinalinthigh=34;
@OverrideprotectedvoidonCreate(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 weight
s 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"?><RelativeLayoutxmlns: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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:layout_centerInParent="true"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="56dp"android:orientation="horizontal"android:gravity="center_vertical"><TextViewandroid: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.BarViewandroid:id="@+id/low_bar"android:layout_weight="1"android:layout_width="0dp"android:layout_height="8dp" /><TextViewandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="56dp"android:orientation="horizontal"android:gravity="center_vertical"><TextViewandroid: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.BarViewandroid:id="@+id/mid_bar"android:layout_weight="1"android:layout_width="0dp"android:layout_height="8dp" /><TextViewandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="56dp"android:orientation="horizontal"android:gravity="center_vertical"><TextViewandroid: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.BarViewandroid:id="@+id/high_bar"android:layout_weight="1"android:layout_width="0dp"android:layout_height="8dp" /><TextViewandroid: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?"