簡體   English   中英

JFreeChart,如何重新縮放y軸

[英]JFreeChart, how to rescale the y-axis


我想重新縮放圖表,但我真的不知道該怎么做。 我設置為縮放,但可以解決我的問題。
實際上,圖中的數據以小數據顯示。
代碼如下:

public class DrawChart {

private static final Random random = new Random();
RefreshGraph refresh;

public DrawChart(JPanel panel){
    this.refresh = new RefreshGraph();
    this.displayChart (panel);
}

private void displayChart(JPanel jpanel){
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    Timer timer = new Timer(1000, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            refresh.getRefresh (dataset);
        }
    });
    timer.start ();

    JFreeChart chart = createChart(dataset);

    ChartPanel panel=new ChartPanel(chart);
    panel.setPreferredSize (new Dimension(700,300));
    jpanel.setLayout(new java.awt.BorderLayout());
    jpanel.add (panel, BorderLayout.CENTER);
    jpanel.setVisible(true);
}

private JFreeChart createChart(final CategoryDataset dataset){
    final JFreeChart result = ChartFactory.createLineChart ("", null, null, dataset, PlotOrientation.VERTICAL, true, true, false);

    //Set the background color for the chart
    result.setBackgroundPaint (Color.white);

    //get the plot for customization
    final CategoryPlot plot = result.getCategoryPlot ();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.zoom (0.5);

    CategoryAxis domainAxis = plot.getDomainAxis ();
    final IntervalMarker target = new IntervalMarker(0.25, 1.0);

    //This thread will read the tempory file every minutes and refresh the graph
    Timer timer = new Timer(5000, new ActionListener(){
        public void actionPerformed(ActionEvent e){
            boolean eof_=false;
            try{
                File labelingraph = new File(System.getProperty("user.dir")+"\\period.in");
                if(labelingraph.exists ()){
                    BufferedReader readPeriod = new BufferedReader(new FileReader(labelingraph));
                    while(eof_!=true){
                        String _line = readPeriod.readLine ();
                        if(_line!=null){
                            target.setLabel("Period : "+_line);
                        }else{eof_=true;}
                    }
                }
            }
            catch(IOException io){System.out.println ("Error while accessing the file : "+io.getMessage());}
        }
    });
    timer.start ();

    target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11));
    target.setLabelAnchor(RectangleAnchor.LEFT);
    target.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
    target.setPaint(new Color(222, 222, 255, 128));
    plot.addRangeMarker(target, Layer.BACKGROUND);

    return result;
}}

縮放僅縮小y軸值,並且線圖本身保持穩定。
如何重新縮放圖形以使其具有良好的可見性? 謝謝

我通過使用以下幾行代碼解決了這個問題

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange (1.200, 1.320);
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits ());
rangeAxis.setTickUnit (new NumberTickUnit(0.005, df, 0));

特別是最后一行。

謝謝大家

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM