Java图表类库JChartLib使用介绍
jopen
10年前
JChartLib是 一款基于Java的智能图表类库,JChartLib不仅有着漂亮的外观,而且支持图表坐标数值的自动调整,这样我们在图表中显示数值就比较简单,不用我 们计算处理。JChartLib同样支持线型图、柱状图和饼状图,而且可以自定义外观样式。另外图表可直接在 Swing 中使用,可保持为 jpg 和 png 格式图片。
JChartLib的预览图
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 } }来自:http://原网站已经失效/article/java-jchartlib.html