2011年9月13日火曜日

棒グラフ-IntervalXYDataset

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class swingTestSlider extends ApplicationFrame {

 private static class DemoPanel extends JPanel implements ChangeListener {

  JSlider slider;
  ValueAxis xAxis;
  XYSeries series;
  
  public DemoPanel() {
   super(new BorderLayout());
   
   IntervalXYDataset dataset = createDataset();
   JFreeChart chart = createChart(dataset);
   
   ChartPanel chartPanel = new ChartPanel(chart);
   chartPanel.setPreferredSize(new java.awt.Dimension(600, 270));

   add(chartPanel);

   JPanel panel = new JPanel(new BorderLayout());
   
   this.slider = new JSlider();
   slider.setMaximum(series.getItemCount());
   slider.setValue(series.getItemCount());
   
   this.slider.addChangeListener(this);
   panel.add(this.slider);
   add(panel, BorderLayout.SOUTH);
  }

  private JFreeChart createChart(IntervalXYDataset dataset) {
   final JFreeChart chart = ChartFactory.createXYBarChart(
     "IntervalXYDataset", "X", false, "Y", dataset,
     PlotOrientation.VERTICAL, true, true, false);
   
   XYPlot plot = (XYPlot) chart.getPlot();

   NumberAxis numberaxis = (NumberAxis) plot.getDomainAxis();
   numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
   
   xAxis = plot.getDomainAxis();
   xAxis.setAutoRange(false);
   xAxis.setRange(80, 100);
   
   plot.setRenderer(new XYBarRenderer());
   XYBarRenderer renderer = (XYBarRenderer) chart.getXYPlot().getRenderer();
   renderer.setMargin(0.1);;

   return chart;
  }
  /*
   * グラフデータを作る
   */
  private IntervalXYDataset createDataset() {
   
   series = new XYSeries("XYSeriesデータ");
   
   for ( int i = 1 ; i < 101 ; i++){
    series.add(i, 100+i);
   }

   final XYSeriesCollection dataset = new XYSeriesCollection(series);
   return dataset;
  }//グラフデータを作るここまで

  @Override
  public void stateChanged(ChangeEvent event) {
   xAxis.setRange(slider.getValue()-20, slider.getValue());
   //System.out.println(slider.getValue());
  }

 }//class MakePanel ここまで

 public swingTestSlider(String title) {
  super(title);
  setContentPane(new DemoPanel());
 }

 public static JPanel createDemoPanel() {
  return new DemoPanel();
 }

 public static void main(String[] args) {
  swingTestSlider demo = new swingTestSlider("XYBarChart");
  demo.pack();
  RefineryUtilities.centerFrameOnScreen(demo);
  demo.setVisible(true);
 }

}

0 件のコメント:

コメントを投稿