Android में GraphView लाइब्रेरी से ग्राफ/चार्ट बनाने का तरीका
यह उदाहरण GraphView लाइब्रेरी का उपयोग करके लाइन ग्राफ बनाने का तरीका दिखाता है। यह लाइन, बार, पॉइंट्स आदि कई ग्राफ टाइप सपोर्ट करती है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: build.gradle (Module: app) में डिपेंडेंसी जोड़ें
dependencies {
implementation 'com.jjoe64:graphview:4.2.2'
// नवीनतम वर्जन चेक करें
}
चरण 3: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
चरण 4: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GraphView graph = findViewById(R.id.graph);
// डेटा पॉइंट्स: (x, y)
LineGraphSeries series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
// वैकल्पिक: ग्राफ कस्टमाइजेशन
series.setColor(0xFF1E88E5); // लाइन कलर
series.setThickness(4); // लाइन मोटाई
series.setDrawDataPoints(true);
series.setDataPointsRadius(8);
// व्यूपोर्ट सेटिंग्स
graph.getViewport().setScalable(true); // जूम इनेबल
graph.getViewport().setScrollable(true); // स्क्रॉल इनेबल
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
अन्य ग्राफ टाइप्स
- BarGraphSeries - बार चार्ट के लिए
- PointsGraphSeries - स्कैटर प्लॉट के लिए
- Multiple series - एक ग्राफ पर कई सीरीज:
graph.addSeries(series2)
लोकप्रिय विकल्प
- MPAndroidChart - सबसे फीचर-रिच (पाई, रडार, कैंडलस्टिक, एनिमेशन)
- HelloCharts - सरल और हल्का
- AnyChart - एंटरप्राइज ग्रेड