Jfree Chart วิธีทำ Custome Label ให้กับ Pie Chart

บทความเกี่ยวกับ : Jfree Chart วิธีทำ Custome Label ให้กับ Pie Chart



วันนี้เจอโจทย์ยากมา ให้ทำ กราฟใหม่
แบบว่าต้อง Custom กัน นิสนุง

ปรกติเราก็แสดงแบบ ตาม default ของมันอ่ะนะ
ใน jfreechart  มันก็ง่ายๆ แค่ set

Key , Value เข้าไปมันก็แสดงออกมาตามนั้นแหละ

แต่วันนี้เรามีความจำเป็นต้อง ตกแต่งในส่วนของ Value มันซักหน่อย
พูดง่ายๆ ว่าจัด format มันนั่นเอง วิธีการคือ ใช้ตัวนี้ครับ

PieSectionLabelGenerator 

ตัวนี้เป็น Interface นะครับ
เราต้องสร้าง Class มาแล้ว implement method นี้ generateSectionLabel

ตามตัวอย่าง

static class CustomLabelGenerator implements PieSectionLabelGenerator {
          public String generateSectionLabel (final PieDataset dataset, final Comparable key) {
            String result = null;   
            if (dataset != null) {

                   //Custom result ได้ตามสบาย จากตัวอย่างเราจะให้มันแสดงแค่ key
                    result = key.toString();  
            }
            return result;
        }
    }


จากนั้นตอนเรียกใช้ก็ตามนี้

        final PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        plot.setNoDataMessage("No data to display");
        plot.setLabelGenerator(new CustomLabelGenerator());
        return chart;



ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

java -Xms , java -Xmx กำหมด memory ให้ JVM เพื่อป้องกันปัญหา Out of Memory

Oracle date format จัด format date ให้แสดง พศ และ เดือน ภาษาไทยหรือตามภาษาที่เราเลือก

Java this กับ super การใช้งานคำสั่ง this กับ super ใน ภาษา Java