Chart এ Annotations যোগ করা (Text, Image, Shape)

Annotations এবং Markers যোগ করা - জেফ্রিচার্ট (JFreeChart) - Big Data and Analytics

411

JFreeChart লাইব্রেরি ব্যবহার করে Chart এ বিভিন্ন ধরনের Annotations যোগ করা সম্ভব। Annotations চার্টে গুরুত্বপূর্ণ পয়েন্ট, টেক্সট, চিত্র (Image) বা আকার (Shape) যোগ করার জন্য ব্যবহৃত হয়। এই অ্যানোটেশনগুলো সাধারণত চার্টের ভিজ্যুয়ালিজেশনে আরো স্পষ্টতা যোগ করতে বা কিছু বিশেষ ডেটা পয়েন্ট হাইলাইট করতে ব্যবহার করা হয়।

JFreeChart-এ বিভিন্ন ধরনের অ্যানোটেশন যোগ করা যায়:

  • Text Annotations: চার্টে টেক্সট যোগ করা
  • Image Annotations: চার্টে ছবি (Image) যোগ করা
  • Shape Annotations: চার্টে আকার (Shape) যোগ করা

১. Text Annotations যোগ করা

Text Annotations চার্টে একটি নির্দিষ্ট পজিশনে টেক্সট যোগ করার জন্য ব্যবহৃত হয়। এটি সাধারণত বিশেষ ডেটা পয়েন্ট বা নির্দিষ্ট অঞ্চলে টেক্সট প্রদর্শন করতে ব্যবহৃত হয়।

উদাহরণ: Text Annotation যোগ করা

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;

public class TextAnnotationExample {
    public static void main(String[] args) {
        // XYSeries তৈরি করা
        XYSeries series = new XYSeries("Temperature");
        series.add(1, 25);
        series.add(2, 27);
        series.add(3, 29);
        series.add(4, 30);

        XYSeriesCollection dataset = new XYSeriesCollection(series);

        // XYPlot তৈরি করা
        JFreeChart chart = ChartFactory.createXYLineChart(
                "Temperature Over Time", // Chart Title
                "Time",                  // X-Axis Label
                "Temperature (°C)",      // Y-Axis Label
                dataset                  // Dataset
        );

        XYPlot plot = chart.getXYPlot();

        // Text Annotation তৈরি করা
        XYTextAnnotation annotation = new XYTextAnnotation("Peak Temperature", 3, 29);
        annotation.setFont(new Font("Arial", Font.BOLD, 12));  // ফন্ট কাস্টমাইজ করা
        annotation.setPaint(Color.RED);  // টেক্সট রঙ পরিবর্তন করা

        plot.addAnnotation(annotation);  // Annotation চার্টে যোগ করা

        // ChartPanel তৈরি এবং JFrame এ প্রদর্শন
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));

        JFrame frame = new JFrame();
        frame.setContentPane(chartPanel);
        frame.pack();
        frame.setVisible(true);
    }
}

এখানে XYTextAnnotation ব্যবহার করে Temperature ডেটা পয়েন্টের উপর "Peak Temperature" টেক্সট যুক্ত করা হয়েছে। আপনি টেক্সটের পজিশন, ফন্ট, রঙ ইত্যাদি কাস্টমাইজ করতে পারেন।


২. Image Annotations যোগ করা

Image Annotations চার্টে একটি নির্দিষ্ট পজিশনে ছবি (Image) যোগ করতে ব্যবহৃত হয়। এটি বিশেষত গ্রাফিক্যাল চার্টের ক্ষেত্রে গুরুত্বপূর্ণ ভিজ্যুয়াল ইনফরমেশন প্রদর্শন করতে ব্যবহৃত হয়।

উদাহরণ: Image Annotation যোগ করা

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageAnnotationExample {
    public static void main(String[] args) {
        // XYSeries তৈরি করা
        XYSeries series = new XYSeries("Temperature");
        series.add(1, 25);
        series.add(2, 27);
        series.add(3, 29);
        series.add(4, 30);

        XYSeriesCollection dataset = new XYSeriesCollection(series);

        // XYPlot তৈরি করা
        JFreeChart chart = ChartFactory.createXYLineChart(
                "Temperature Over Time", // Chart Title
                "Time",                  // X-Axis Label
                "Temperature (°C)",      // Y-Axis Label
                dataset                  // Dataset
        );

        XYPlot plot = chart.getXYPlot();

        // Image Annotation তৈরি করা
        try {
            Image image = ImageIO.read(new File("path/to/your/image.png"));
            XYImageAnnotation imageAnnotation = new XYImageAnnotation(3, 29, image);
            plot.addAnnotation(imageAnnotation);  // Image Annotation চার্টে যোগ করা
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ChartPanel তৈরি এবং JFrame এ প্রদর্শন
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));

        JFrame frame = new JFrame();
        frame.setContentPane(chartPanel);
        frame.pack();
        frame.setVisible(true);
    }
}

এখানে XYImageAnnotation ব্যবহার করে Temperature ডেটা পয়েন্টে একটি ছবি যোগ করা হয়েছে। আপনি ছবির পজিশন এবং ছবির ফাইল পাথ নির্ধারণ করতে পারেন।


৩. Shape Annotations যোগ করা

Shape Annotations চার্টে আকার (Shape) যোগ করার জন্য ব্যবহৃত হয়। এটি ডেটার নির্দিষ্ট পয়েন্টে আকার বা চিহ্ন (যেমন বর্গাকার, গোলাকার) ব্যবহার করে।

উদাহরণ: Shape Annotation যোগ করা

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class ShapeAnnotationExample {
    public static void main(String[] args) {
        // XYSeries তৈরি করা
        XYSeries series = new XYSeries("Temperature");
        series.add(1, 25);
        series.add(2, 27);
        series.add(3, 29);
        series.add(4, 30);

        XYSeriesCollection dataset = new XYSeriesCollection(series);

        // XYPlot তৈরি করা
        JFreeChart chart = ChartFactory.createXYLineChart(
                "Temperature Over Time", // Chart Title
                "Time",                  // X-Axis Label
                "Temperature (°C)",      // Y-Axis Label
                dataset                  // Dataset
        );

        XYPlot plot = chart.getXYPlot();

        // Shape Annotation তৈরি করা (Rectangular shape)
        Shape rectangle = new Rectangle2D.Double(2, 25, 1, 5);
        XYShapeAnnotation shapeAnnotation = new XYShapeAnnotation(rectangle, new BasicStroke(2.0f), Color.RED);
        plot.addAnnotation(shapeAnnotation);  // Shape Annotation চার্টে যোগ করা

        // ChartPanel তৈরি এবং JFrame এ প্রদর্শন
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));

        JFrame frame = new JFrame();
        frame.setContentPane(chartPanel);
        frame.pack();
        frame.setVisible(true);
    }
}

এখানে XYShapeAnnotation ব্যবহার করে একটি আয়তক্ষেত্র (Rectangle) আকার যোগ করা হয়েছে। আপনি বিভিন্ন আকার (Circle, Line, Polygon ইত্যাদি) ব্যবহার করতে পারেন।


সারসংক্ষেপ

JFreeChart লাইব্রেরি ব্যবহার করে Chart-এ বিভিন্ন ধরনের Annotations যোগ করা যায়:

  • Text Annotation: চার্টের নির্দিষ্ট পয়েন্টে টেক্সট যোগ করা।
  • Image Annotation: চার্টের নির্দিষ্ট পয়েন্টে ছবি যোগ করা।
  • Shape Annotation: চার্টে আকার (Shape) যোগ করা।

এই অ্যানোটেশনগুলো ব্যবহার করে আপনি আপনার চার্টের ভিজ্যুয়াল উপস্থাপনাকে আরও আকর্ষণীয় এবং বোঝার সহজ করে তুলতে পারবেন।

Content added By
Promotion

Are you sure to start over?

Loading...