如何使用JChart2D制作柱形图?

时间:2013-10-19 16:49:34

标签: jchart2d

示例:添加(0,y1)点和(0,0)然后(20,0)和(20,y2)和(20,0),然后(30,0)和(30,y3)和(30,0)等 所以总是返回到x轴和第一步。

还有其他/更好的方式吗?

1 个答案:

答案 0 :(得分:1)

将图表的默认TracePainter更改为TracePainterVerticalBar。

试试这个示例代码:

public class MinimalStaticChart {

  MinimalStaticChart() {
    super();
  }

  public static void main(String[]args){
    // Create a chart:  
    Chart2D chart = new Chart2D();
    // Create an ITrace: 
    ITrace2D trace = new Trace2DSimple();   
    // change to vertical bar diagram
    trace.setTracePainter(new info.monitorenter.gui.chart.traces.painters.TracePainterVerticalBar(chart));   
    // Add the trace to the chart. This has to be done before adding points (deadlock prevention): 
    chart.addTrace(trace);    
    // Add all points, as it is static: 
    Random random = new Random();
    for(int i=10;i>=0;i--){
      trace.addPoint(i,random.nextDouble()*10.0+i);
    }
    // Make it visible:
    // Create a frame.
    JFrame frame = new JFrame("MinimalStaticChart");
    // add the chart to the frame: 
    frame.getContentPane().add(chart);
    frame.setSize(400,300);
    // Enable the termination button [cross on the upper right edge]: 
    frame.addWindowListener(
        new WindowAdapter(){
          public void windowClosing(WindowEvent e){
              System.exit(0);
          }
        }
      );
    frame.setVisible(true);
  }
}