JFreeChart,如何重新缩放y轴

时间:2012-08-31 07:29:59

标签: java jfreechart


我想重新缩放图表,但我真的不知道这样做。我设置缩放但它解决了我的问题。
实际上,图中的数据以小数据显示 代码如下:

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轴值,线图本身保持稳定。
如何重新调整图表以使其具有良好的可见性? 感谢

1 个答案:

答案 0 :(得分:0)

我通过使用这些代码行来解决这个问题

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

尤其是最后一行。

感谢所有