2016-12-04 64 views
0

我是JFreeChart的新手。我用JFreeChart創建了折線圖。現在,我想添加有關圖表外側的附加細節(即在圖表的左側和底部)。另外,我想用生成的圖表保存所有的信息。任何機構都可以幫助我。如何在圖表外添加有關圖表的其他詳細信息

package hh; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.io.File; 
import java.io.IOException; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.axis.NumberTickUnit; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 
import org.jfree.chart.renderer.xy.XYSplineRenderer; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 

public class XYLineChartExample extends JFrame { 

    // double x=1.08,y=0.370; 
    JFreeChart chart; 

    public XYLineChartExample() { 
     super("XY Line Chart Example with JFreechart"); 

     JPanel chartPanel = createChartPanel(); 
     add(chartPanel, BorderLayout.CENTER); 

     setSize(1240, 600); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
    } 

    private JPanel createChartPanel() { 
     String chartTitle = "Objects Movement Chart"; 
     String xAxisLabel = "X"; 
     String yAxisLabel = "Y"; 

     XYDataset dataset = createDataset(); 

     JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, 
       xAxisLabel, yAxisLabel, dataset); 
     XYPlot plot = chart.getXYPlot(); 
     XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); 
     plot.setRenderer(renderer); 
     renderer.setSeriesPaint(2, Color.RED); 
     renderer.setSeriesPaint(3, Color.BLACK); 
     renderer.setSeriesPaint(4, Color.BLACK); 
     renderer.setSeriesPaint(5, Color.BLUE); 
     renderer.setSeriesPaint(6, Color.RED); 

     File imageFile = new File(
       "C:/Users/varinder/Desktop/Graphs/Branch1_Grap.jpg"); 
     int width = 1240; 
     int height = 500; 
     try { 
      ChartUtilities.saveChartAsJPEG(imageFile, chart, width, height); 
     } catch (IOException ex) { 
      System.err.println(ex); 
     } 

     return new ChartPanel(chart); 

    } 

    private XYDataset createDataset() { 

     XYSeriesCollection dataset = new XYSeriesCollection(); 
     XYSeries series = new XYSeries("Object 1"); 
     XYSeries series2 = new XYSeries("Object 2"); 
     XYSeries series3 = new XYSeries("Object 3"); 
     XYSeries series4 = new XYSeries("Object 4"); 
     XYSeries series5 = new XYSeries("Object 5"); 
     XYSeries series6 = new XYSeries("Object 6"); 
     XYSeries series7 = new XYSeries("Object 7"); 

     // fixed X axis 

     series.add(.10, .000); 
     series.add(.20, .000); 
     series.add(.30, .000); 
     series.add(.40, .000); 
     series.add(.50, .000); 
     series.add(.60, .000); 
     series.add(.70, .000); 
     series.add(.80, .000); 
     series.add(.90, .000); 
     series.add(1.00, .000); 
     series.add(1.10, .000); 
     series.add(1.20, .000); 
     series.add(1.30, .000); 
     series.add(1.40, .000); 
     series.add(1.50, .000); 
     series.add(1.60, .000); 
     series.add(1.70, .000); 
     series.add(1.80, .000); 
     series.add(1.90, .000); 
     series.add(2.00, .000); 
     series.add(2.10, .000); 
     series.add(2.20, .000); 
     series.add(2.30, .000); 
     series.add(2.30, .000); 
     series.add(2.40, .000); 
     series.add(2.50, .000); 
     series.add(2.60, .000); 
     series.add(2.70, .000); 
     series.add(2.80, .000); 
     series.add(2.90, .000); 
     series.add(3.00, .000); 
     series.add(3.10, .000); 
     series.add(3.20, .000); 
     series.add(3.30, .000); 
     series.add(3.40, .000); 
     series.add(3.50, .000); 
     series.add(3.60, .000); 
     // series.add(3.70, .000); 

     // fixed Y axis 
     series2.add(.00, .100); 
     series2.add(.00, .200); 
     series2.add(.00, .300); 
     series2.add(.00, .400); 
     series2.add(.00, .500); 
     series2.add(.00, .600); 
     series2.add(.00, .700); 
     series2.add(.00, .800); 
     series2.add(.00, .900); 
     series2.add(.00, 1.000); 
     series2.add(.00, 1.100); 
     series2.add(.00, 1.200); 

     // Bed level with red color 
     // series2.add(0, 193.500); 

     // series3.add(1.92, 100.370); 
     series3.add(0.88, 0.900); 
     series3.add(2.12, 0.900); 
     series3.add(0.75, 0.900); 
     series3.add(2.25, 0.900); 
     series3.add(0.59, 0.300); 
     series3.add(2.41, 0.300); 
     series3.add(0.00, 0.300); 
     series3.add(3.1, .300); 

     series4.add(0.88, 0.304); 
     series4.add(2.12, 0.304); 

     series4.add(0.879, 0.876);// one decrease 
     series4.add(2.12, 0.876); 
     series5.add(0.88, 0.876); 
     series5.add(1.109, 0.876); 
     series5.add(1.109, 0.376); 

     series6.add(2.12, 0.876); 
     series6.add(1.891, 0.876); 
     series6.add(1.890, 0.376); 
     series6.add(1.109, 0.376); 

     series7.add(1.109, 0.876);// sement area by adding 0.1. 
     series7.add(1.119, 0.876); 
     series7.add(1.119, 0.386); 
     series7.add(1.881, 0.386); 
     series7.add(1.881, 0.876); 
     series7.add(1.891, 0.876); 
     // series7.add(1.11, 0.); 

     dataset.addSeries(series); 
     dataset.addSeries(series2); 
     dataset.addSeries(series3); 
     dataset.addSeries(series4); 
     dataset.addSeries(series5); 
     dataset.addSeries(series6); 
     dataset.addSeries(series7); 
     return dataset; 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 

       new XYLineChartExample().setVisible(true); 
      } 
     }); 
    } 
} 
+0

這不是一個簡單的例子。你嘗試了什麼? –

+0

什麼_附加details_,爲[示例](http://stackoverflow.com/a/13309587/230513)? – trashgod

回答

0

對於軸,更好地利用循環使代碼更清潔:

for(double i=0;i<3.70;i+=0.1){ series.add(i,0.0) } 
for(  i=0;i<1.2;i+=0.1){ series2.add(0.0,i) } 

我personnaly使用JavaFX的,而不是舊版本揮杆Jelement,所以你可以使用一個BorderPane(https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html你這裏的圖片),所以你可以把一些元素周圍(並把borderpane在一個區,現場建設者的可能性較大(http://code.makery.ch/library/javafx-8-tutorial/fr/part1/

編輯https://code.lds.org/nexus/content/sites/maven-sites/stack/modules/swing/1.1.1/apidocs/org/lds/stack/swing/BorderPane.html這裏是你的揮杆版本,與setNorthComponent /南/ ...方法的JPanel;)

EDIT2

這裏,要小心有好名字對於你的班級「啓動器」,如果它不行,那麼你可以添加控制器的按鈕

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
/** 
* Class that starts the application, contains the primary stage. 
*/ 
public class Launcher extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     BorderPane pane = new BorderPane(); 
     primaryStage.setTitle("Line Chart"); 
      //defining the axes 
      final NumberAxis xAxis = new NumberAxis(); 
      final NumberAxis yAxis = new NumberAxis(); 
      xAxis.setLabel("Time"); 

      //creating the chart 
      final LineChart<Number,Number> lineChart = 
        new LineChart<Number,Number>(xAxis,yAxis); 

      lineChart.setTitle("Test"); 
      //defining a series 
      XYChart.Series series = new XYChart.Series(); 
      XYChart.Series series1 = new XYChart.Series(); 
      XYChart.Series series2= new XYChart.Series(); 
      XYChart.Series series3 = new XYChart.Series(); 
      XYChart.Series series4 = new XYChart.Series(); 
      XYChart.Series series5 = new XYChart.Series(); 
      XYChart.Series series6 = new XYChart.Series(); 
      XYChart.Series series7 = new XYChart.Series(); 

      double i; 
      for(i=0;i<3.70;i+=0.1){ 
        series.getData().add(new XYChart.Data(i,0.0)); 
      } 
      for(i=0;i<1.2;i+=0.1){ 
        series2.getData().add(new XYChart.Data(0.0,i)); 
      } 


      // Bed level with red color 
      // series2.add(0, 193.500); 

      // series3.add(1.92, 100.370); 
      series3.getData().add(new XYChart.Data(0.88, 0.900)); 
      series3.getData().add(new XYChart.Data(2.12, 0.900)); 
      series3.getData().add(new XYChart.Data(0.75, 0.900)); 
      series3.getData().add(new XYChart.Data(2.25, 0.900)); 
      series3.getData().add(new XYChart.Data(0.59, 0.300)); 
      series3.getData().add(new XYChart.Data(2.41, 0.300)); 
      series3.getData().add(new XYChart.Data(0.00, 0.300)); 
      series3.getData().add(new XYChart.Data(3.1, .300)); 

      series4.getData().add(new XYChart.Data(0.88, 0.304)); 
      series4.getData().add(new XYChart.Data(2.12, 0.304)); 

      series4.getData().add(new XYChart.Data(0.879, 0.876));// one decrease 
      series4.getData().add(new XYChart.Data(2.12, 0.876)); 
      series5.getData().add(new XYChart.Data(0.88, 0.876)); 
      series5.getData().add(new XYChart.Data(1.109, 0.876)); 
      series5.getData().add(new XYChart.Data(1.109, 0.376)); 

      series6.getData().add(new XYChart.Data(2.12, 0.876)); 
      series6.getData().add(new XYChart.Data(1.891, 0.876)); 
      series6.getData().add(new XYChart.Data(1.890, 0.376)); 
      series6.getData().add(new XYChart.Data(1.109, 0.376)); 

      series7.getData().add(new XYChart.Data(1.109, 0.876));// sement area by adding 0.1. 
      series7.getData().add(new XYChart.Data(1.119, 0.876)); 
      series7.getData().add(new XYChart.Data(1.119, 0.386)); 
      series7.getData().add(new XYChart.Data(1.881, 0.386)); 
      series7.getData().add(new XYChart.Data(1.881, 0.876)); 
      series7.getData().add(new XYChart.Data(1.891, 0.876)); 
      // series7.add(1.11, 0.); 

      //A vertical Box 
      VBox box=new VBox(); 
      //Add some example of button into 
      box.getChildren().addAll(new Button("Cut"), new Button("Copy"), new Button("Paste")); 
      lineChart.setCreateSymbols(false); 
      //Add all the series to the chart 
      lineChart.getData().addAll(series,series1,series2, series3,series4,series5,series6,series7); 
      //Add the chart in the center of the pane 
      pane.setCenter(lineChart); 
      //add the vbox (with the button) to the right 
      pane.setRight(box); 
      Scene scene = new Scene(pane,800,600); 

      primaryStage.setScene(scene); 
      primaryStage.show(); 

     primaryStage.setTitle("Test"); 
     primaryStage.centerOnScreen(); 
    } 

    public static void main (String[] args) { 
     launch(args); 
    } 
} 
+0

@ Azro ..感謝您建議使我的代碼清潔。我用於循環。但我仍然對添加圖形的額外信息感到困惑。我是新手,請你澄清一下。 –

+0

我試圖導入你的代碼,但沒有良好的庫(也許你的年齡更大),我會試着用另一種方式做同樣的工作 – azro

+0

Sure Azro ..請讓我知道,一旦你找到另一種方式解決我的問題。這對我非常有幫助。謝謝 –