Now a days, Android has became an important market in all business sectors. We see many enterprises companies coming up with their products and business solutions. Displaying statistics and graphical representation has became a need of the application UI.
MPAndroidChart is a powerful & easy to use chart library for Android. It runs on API level 8 and upwards. As an additional feature, this library allows cross-platform development between Android and iOS.
We can draw below chart types,
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//build.gradle
//activity_main.xml
//MainActivity.java
MPAndroidChart is a powerful & easy to use chart library for Android. It runs on API level 8 and upwards. As an additional feature, this library allows cross-platform development between Android and iOS.
We can draw below chart types,
- Simple Bar Chart
- Grouped Bar Chart
- Horizontal Bar Chart
- Simple Line Chart
- Line Chart with Cubic Lines
- Grouped Line Chart
- Combined Line and Bar Chart
- Pie Chart
- Scatter Chart
- Candlestick Chart
- Radar Chart
Refer the below link for complete sample code:-
Download Sample Code
Have a look on few code snippets,
//build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.example.harshalbenake.graphicaldiagrams" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.0.0' compile files('libs/mpandroidchartlibrary-2-1-5.jar') } |
//activity_main.xml
1 2 3 4 5 6 7 8 9 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.github.mikephil.charting.charts.BarChart android:id="@+id/chart1" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> |
//MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public class MainActivity extends Activity implements OnChartValueSelectedListener { protected BarChart mChart; protected String[] mMonths = new String[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mChart = (BarChart) findViewById(R.id.chart1); mChart.setDescription(""); mChart.setOnChartValueSelectedListener(this); mChart.setDrawBarShadow(false); mChart.setDrawValueAboveBar(true); XAxis xAxis = mChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); setData(12, 50); } private void setData(int count, float range) { ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < count; i++) { xVals.add(mMonths[i]); } ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); for (int i = 0; i < count; i++) { float mult = (range + 1); float val = (float) (Math.random() * mult); yVals1.add(new BarEntry(val, i)); } BarDataSet set1 = new BarDataSet(yVals1, "HB"); set1.setBarSpacePercent(35f); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); dataSets.add(set1); BarData data = new BarData(xVals, dataSets); data.setValueTextSize(10f); mChart.setData(data); } @Override public void onValueSelected(Entry entry, int i, Highlight highlight) { Toast.makeText(MainActivity.this,"value: "+entry.getVal(),Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected() { } } |
No comments:
Post a Comment