2012-04-18 28 views
3

enter image description here 目前,當我運行這段代碼時,它顯示了上圖所示的圖形,我想繪製一張我已經顯示的圖形。目前它繪製一個關於(0,0)對稱的圖表,我認爲從(0,-3)或(0,< -2)開始應該做這項工作。有沒有其他的方式來做到這一點?如何製作如圖所示的圖形?

目前代碼:

public class Profile { 



    double last=0; 
    ChartFrame frame1; 
    JToolBar jt=new JToolBar(); 
    public void generateProfile(int[] pointValue,double[] distance){ 
     ArrayList pv=new ArrayList(); 
     ArrayList dist=new ArrayList(); 

     pv.add(pointValue); 
     dist.add(distance); 
     int min=pointValue[0]; 
     for(int i=0;i<pv.size();i++){ 
      //System.out.print(pointValue[i]); 
      if(pointValue[i]<min){ 
       min=pointValue[i]; 
      } 
     } 
     for(int i=0;i<dist.size();i++){ 
      System.out.print(distance[i]); 
     } 


     XYSeries series = new XYSeries("Average Weight"); 
     for(int i=0;i<pointValue.length;i++){ 
      //if(pointValue[i]!=0){ 
       series.add(last,pointValue[i]); 
       last=distance[i]; 
      //} 
     } 


     XYDataset xyDataset = new XYSeriesCollection(series); 
     JFreeChart chart= ChartFactory.createXYAreaChart("Profile View Of Contour", "Distance", "Contour Value", xyDataset, PlotOrientation.VERTICAL, true, true, false); 
     ValueAxis rangeAxis = chart.getXYPlot().getRangeAxis(); 
     rangeAxis.setLowerBound(-3); 
     frame1=new ChartFrame("XYLine Chart",chart); 

     JButton saveimg=new JButton(new AbstractAction("Save as Image"){ 
      public void actionPerformed(ActionEvent e) { 
       //some other lines of code... 
      } 
     }); 
     jt.add(saveimg); 
     frame1.add(jt, BorderLayout.NORTH); 
     frame1.setVisible(true); 
     frame1.setSize(300,300); 
    } 



    public static void main(String ar[]){ 
     Profile pro=new Profile(); 
     int[] pv={2,3,0,5,-2,10}; 
     double[] dist={1,4,8,12,14,20}; 
     pro.generateProfile(pv, dist); 



    } 
} 
+2

使用這個新的渲染您可以適應的方法之一所示[這裏](http://stackoverflow.com/q/8866390/230513) 。 – trashgod 2012-04-19 01:06:37

回答

0

目前在XYAreaRenderer對此沒有支持。但是,實現這一點不應該太難。看看XYAreaRenderer.drawItem()方法的代碼。在此方法中,使用實際的零值(0.0)計算出局部變量transZero。如果你只是用其他值替換0.0,這可能會做你所需要的。

實現此目的的最簡單方法是創建自己的渲染器,該渲染器派生自XYAreaRenderer並重寫drawItem()。在這種方法中,複製原始代碼並按上面所述進行修改。

然後你只需要通過寫

XYAreaRenderer yourNewRenderer = ...; /* initialize here */ 
chart.getXYPlot().setRender(yourNewRenderer);