Java智慧圖表類庫JChartLib使用介紹

2014-10-25    分類:開源軟體、報表/圖表元件、程式設計開發、首頁精華0人評論發表於2014-10-25

本文由碼農網 – 小峰原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃

JChartLib是一款基於Java的智慧圖表類庫,JChartLib不僅有著漂亮的外觀,而且支援圖表座標數值的自動調整,這樣我們在圖表中顯示數值就比較簡單,不用我們計算處理。JChartLib同樣支援線型圖、柱狀圖和餅狀圖,而且可以自定義外觀樣式。另外圖表可直接在 Swing 中使用,可保持為 jpg 和 png 格式圖片。

JChartLib的預覽圖

Linechart Linechart with float values Linechart with negative values Barchart Areachart Piechart

JChartLib的使用方法

JChartLib線形圖繪製的Java程式碼如下:

/*
* JChartLib Demo App for a Linechart
* @author Silvio Schneider
/
import com.bitagentur.chart.JChartLibLinechart;
import com.bitagentur.data.JChartLibDataSet;
import com.bitagentur.data.JChartLibSerie;
import com.bitagentur.renderer.JChartLibPanel;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
/*
* A simple demonstration application showing how to create a line chart
/
public class JChartLibApp extends JFrame implements WindowListener {
/**
 * Creates a new Application Frame
 */
public JChartLibApp() {
    super("JChartLib Demo Appllication");
    final JChartLibDataSet dataset = createDataset();
    final JChartLibBaseChart chart = createChart(dataset);
    final JChartLibPanel chartPanel = new JChartLibPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);
}

/**
 * Creates a sample dataset.
 * 
 * @return a sample dataset.
 */
private JChartLibDataSet createDataset() {
    //Dataseries can be added with int arrays
    int[] values1 = new int[5];
    values1[0] = 1;
    values1[1] = 3;
    values1[2] = 4;
    values1[3] = 7;
    values1[4] = 2;

    //or by generating a Dataserie object
    JChartLibSerie values2 = new JChartLibSerie("Banana");
    values2.addValue(5);
    values2.addValue(4);
    values2.addValue(2);
    values2.addValue(6);
    values2.addValue(2);

    final JChartLibDataSet dataset = new JChartLibDataSet();
    dataset.addDataSerie("Apple", values1);   //adds the apples
    dataset.addDataSerie(values2);            //adds the bananas

    return dataset;
}

/**
 * Creates a chart
 * 
 * @param dataset the data for the chart.
 * @return a new chart
 */
private JChartLibBaseChart createChart(final JChartLibDataSet dataset) {

    // create the chart with title and axis names
    final JChartLibLinechart chart = new JChartLibLinechart(
            "Fruits are good", // chart title
            "Time goes by", // x axis text
            "Number of Fruits", // y axis text
            dataset, // data
            true // legend on
            );

    return chart;
}

/**
 * Main method - DEMO Application for JChartLib
 *
 * @param args the command line arguments
 */
public static void main(final String[] args) {
    System.out.println("JChartLibApp started");
    final JChartLibApp app = new JChartLibApp();
    app.addWindowListener(app);
    app.pack();
    app.setVisible(true);
}

@Override
public void windowOpened(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowClosing(WindowEvent e) {
    //Exit and goodby
    if (e.getWindow() == this) {
        dispose();
        System.out.println("Thanks for using the JChartLibApp");
        System.exit(0);
    }
}

@Override
public void windowClosed(WindowEvent e) {
    //Nothing to do

}

@Override
public void windowIconified(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowDeiconified(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowActivated(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowDeactivated(WindowEvent e) {
    //Nothing to do
}
}

JChartLib柱形圖繪製的Java程式碼如下:

/*
* JChartLib Demo App for a Barchart
* @author Silvio Schneider
/
import com.bitagentur.chart.JChartLibBarchart;
import com.bitagentur.data.JChartLibDataSet;
import com.bitagentur.data.JChartLibSerie;
import com.bitagentur.renderer.JChartLibPanel;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
/*
* A simple demonstration application showing how to create a bar chart
/
public class JChartLibApp extends JFrame implements WindowListener {
/**
 * Creates a new Application Frame
 */
public JChartLibApp() {
    super("JChartLib Demo Appllication");
    final JChartLibDataSet dataset = createDataset();
    final JChartLibBarchart chart = createChart(dataset);
    final JChartLibPanel chartPanel = new JChartLibPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);
}

/**
 * Creates a sample dataset.
 * 
 * @return a sample dataset.
 */
private JChartLibDataSet createDataset() {
    //Dataseries can be added with int arrays
    int[] values1 = new int[5];
    values1[0] = 1;
    values1[1] = 3;
    values1[2] = 4;
    values1[3] = 7;
    values1[4] = 2;

    //or by generating a Dataserie object
    JChartLibSerie values2 = new JChartLibSerie("Banana");
    values2.addValue(5);
    values2.addValue(4);
    values2.addValue(2);
    values2.addValue(6);
    values2.addValue(2);

    final JChartLibDataSet dataset = new JChartLibDataSet();
    dataset.addDataSerie("Apple", values1);   //adds the Apples
    dataset.addDataSerie(values2);            //adds the Bananas

    return dataset;
}

/**
 * Creates a chart
 * 
 * @param dataset the data for the chart.
 * @return a new chart
 */
private JChartLibBaseChart createChart(final JChartLibDataSet dataset) {

    // create the chart with title and axis names
    final JChartLibBarchart chart = new JChartLibBarchart(
            "Fruits are good", // chart title
            "Time goes by", // x axis text
            "Number of Fruits", // y axis text
            dataset, // data
            true // legend on
            );

    return chart;
}

/**
 * Main method - DEMO Application for JChartLib
 *
 * @param args the command line arguments
 */
public static void main(final String[] args) {
    System.out.println("JChartLibApp started");
    final JChartLibApp app = new JChartLibApp();
    app.addWindowListener(app);
    app.pack();
    app.setVisible(true);
}

@Override
public void windowOpened(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowClosing(WindowEvent e) {
    //Exit and goodby
    if (e.getWindow() == this) {
        dispose();
        System.out.println("Thanks for using the JChartLibApp");
        System.exit(0);
    }
}

@Override
public void windowClosed(WindowEvent e) {
    //Nothing to do

}

@Override
public void windowIconified(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowDeiconified(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowActivated(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowDeactivated(WindowEvent e) {
    //Nothing to do
}
}

JChartLib餅狀圖繪製的Java程式碼如下:

/*
* JChartLib Demo App for a piechart
* @author Silvio Schneider
/
import com.bitagentur.chart.JChartLibPiechart;
import com.bitagentur.data.JChartLibDataSet;
import com.bitagentur.data.JChartLibSerie;
import com.bitagentur.renderer.JChartLibPanel;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;
import javax.swing.JFrame;
/
* A simple demonstration application showing how to create a pie chart.

/
public class JChartLibApp_piechart extends JFrame implements WindowListener {
/**
 * Creates a new Application Frame
 */
public JChartLibApp_piechart() {
    super("JChartLib Demo Appllication");

    final JChartLibDataSet dataset = createDataset();
    final JChartLibPiechart chart = createChart(dataset);
    final JChartLibPanel chartPanel = new JChartLibPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);
}

/**
 * Creates a sample dataset.
 * 
 * @return a sample dataset.
 */
private JChartLibDataSet createDataset() {
    //or by generating a Dataserie object
    JChartLibSerie values1 = new JChartLibSerie("Fruits");
    values1.addValue("Apple", 1);
    values1.addValue("Banana", 1);
    values1.addValue("Orange", 1);
    values1.addValue("Tomato", 1);

    final JChartLibDataSet dataset = new JChartLibDataSet();
    dataset.addDataSerie(values1);          //adds the Apples

    return dataset;
}

/**
 * Creates a chart
 * 
 * @param dataset the data for the chart.
 * @return a new chart
 */
private JChartLibPiechart createChart(final JChartLibDataSet dataset) {

    // create the chart with title and axis names
    final JChartLibPiechart chart = new JChartLibPiechart(
            "yammi fruitpie", // chart title
            "", // x axis text
            "", // y axis text
            dataset // data
            );

    return chart;
}

/**
 * Main method - DEMO Application for JChartLib
 *
 * @param args the command line arguments
 */
public static void main(final String[] args) {
    System.out.println("JChartLibApp started");
    final JChartLibApp_piechart app = new JChartLibApp_piechart();
    app.addWindowListener(app);
    app.pack();
    app.setVisible(true);
}

@Override
public void windowOpened(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowClosing(WindowEvent e) {
    //Exit and goodby
    if (e.getWindow() == this) {
        dispose();
        System.out.println("Thanks for using the JChartLibApp");
        System.exit(0);
    }
}

@Override
public void windowClosed(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowIconified(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowDeiconified(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowActivated(WindowEvent e) {
    //Nothing to do
}

@Override
public void windowDeactivated(WindowEvent e) {
    //Nothing to do
}
}

JChartLib的功能介紹就到這裡,如有疑問,可以訪問文末的官方網站,或者留言討論。

本文連結:http://www.codeceo.com/article/java-jchartlib.html
本文作者:碼農網 – 小峰
原創作品,轉載必須在正文中標註並保留原文連結和作者等資訊。]

相關文章