JFreeChart,如何重新缩放y轴

时间:2022-10-19 15:02:54


I want to rescale a chart but I don't really know to do it. I set to zoom but it is solving my problem.
In fact the data on the plot is displayed in small data.
The code looks like:

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

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;
}}

The zoom only zooms out the y-axis values and the line graph itself remains steady.
How can I rescale the graph to make have a good visibility ? Thanks

缩放仅缩小y轴值,线图本身保持稳定。如何重新调整图表以使其具有良好的可见性?谢谢

1 个解决方案

#1


0  

I solved this by using these lines of codes

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

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

especially the last line.

特别是最后一行。

Thanks to all

谢谢大家

#1


0  

I solved this by using these lines of codes

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

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

especially the last line.

特别是最后一行。

Thanks to all

谢谢大家