एंड्रॉइड में बार चार्ट ग्राफ़ लागू करना
यह ट्यूटोरियल दर्शाता है कि MPAndroidChart लाइब्रेरी का उपयोग करके एंड्रॉइड एप्लिकेशन में बार चार्ट कैसे बनाया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: मॉड्यूल-लेवल build.gradle में निर्भरता जोड़ें
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/proguard/androidx-annotations.pro'
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
compileSdkVersion 28
defaultConfig {
applicationId "com.example.barchartdemo"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
चरण 3: प्रोजेक्ट-लेवल build.gradle में रिपॉजिटरी जोड़ें
// Top-level build file
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
चरण 4: लेआउट फ़ाइल में BarChart व्यू जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 5: MainActivity.java में बार चार्ट लॉजिक लागू करें
package com.example.barchartdemo;
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private BarChart barChart;
private BarData barData;
private BarDataSet barDataSet;
private ArrayList<BarEntry> barEntries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barChart = findViewById(R.id.barChart);
getEntries();
barDataSet = new BarDataSet(barEntries, "सैंपल डेटा");
barData = new BarData(barDataSet);
barChart.setData(barData);
// चार्ट स्टाइलिंग
barDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
barDataSet.setValueTextColor(Color.BLACK);
barDataSet.setValueTextSize(18f);
// अतिरिक्त कस्टमाइज़ेशन
barChart.getDescription().setEnabled(false);
barChart.animateY(1000);
}
private void getEntries() {
barEntries = new ArrayList<>();
barEntries.add(new BarEntry(0, 2f));
barEntries.add(new BarEntry(1, 4f));
barEntries.add(new BarEntry(2, 6f));
barEntries.add(new BarEntry(3, 8f));
barEntries.add(new BarEntry(4, 7f));
barEntries.add(new BarEntry(5, 3f));
}
}
ऐप चलाएं
अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें, Android Studio में टूलबार से Run आइकन पर क्लिक करें, और अपना डिवाइस चुनें। ऐप लॉन्च होगा और आपके डेटासेट मानों के आधार पर बार चार्ट प्रदर्शित करेगा।
नोट: आधुनिक एंड्रॉइड विकास के लिए, AndroidX लाइब्रेरी और MPAndroidChart के नवीनतम संस्करण का उपयोग करने पर विचार करें।