ComboBox选择

时间:2014-05-30 07:25:38

标签: combobox jfreechart

我有一个图表,显示3种颜色(红色,绿色,蓝色)的线条,如截图。

当我按下其中一种颜色,即红色时,我想显示只有红色的图形。如果我按绿色然后只有绿色。但如果我什么都不按,那么它应该显示出来。

在我的代码下面,用于创建和绘制线条。

import java.awt.Color;
import java.io.FileInputStream;

import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;

import java.io.*; 

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.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

class LineGraph extends ApplicationFrame {



       private JFreeChart createChart(final XYDataset dataset) {

            // Graph wird erstellt...

       final JFreeChart chart = ChartFactory.createXYLineChart("","X","Y",dataset,PlotOrientation.VERTICAL,true,false,false);


            final XYPlot plot = chart.getXYPlot();

            plot.setBackgroundPaint(Color.lightGray);
            plot.setDomainGridlinePaint(Color.white);
            plot.setRangeGridlinePaint(Color.white);

            NumberAxis xAxis = (NumberAxis) plot.getRangeAxis();
            xAxis.setRange (0, 7500);

            NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
            yAxis.setRange(0, 270);


            return chart;


          }   





    public LineGraph(final String title) {

        super(title);
    }

   public ChartPanel createPanel(final String title){

       final XYDataset dataset = createDataset();
       final JFreeChart chart = createChart(dataset);
       ChartPanel chartPanel = new ChartPanel(chart);
       chartPanel.setPreferredSize(new java.awt.Dimension(950, 570));
       //chartPanel.setSize(50, 50);
       //setContentPane(chartPanel);

       return chartPanel;
   }

   private XYSeriesCollection createDataset() {

       try {

       final int pictureWidth = 7500;
       final int pictureHeight = 5;
       int pictureArray[] = new int[pictureWidth * pictureHeight];
       int pixArray[] = new int[pictureWidth];
       int R, G, B, U;
       int averageX, averageY;

      FileInputStream inp = new FileInputStream("d:\\DO_18-02-2014_19-59-05-756_00010.img");

       int k = 0;

       XYSeries RED = new XYSeries("R");   
       XYSeries GREEN = new XYSeries("G");  
       XYSeries BLUE = new XYSeries("B");
       XYSeries  UNDEFINED = new XYSeries("U");


       for (int j = 0; j < pictureHeight; j++) 
        {for (int i = 0; i < pictureWidth; i++) 
           {   R = inp.read();
               G = inp.read();
               B = inp.read();
               U = inp.read();
               Color c = new Color(R, G, B, U);
               pictureArray[k++] = c.getRGB();

               System.out.println((new Integer(i)).toString()+" "+(new Integer(j)).toString());

               RED.add(i, R);
               GREEN.add(i, B);
               BLUE.add(i, G);
               UNDEFINED.add(i, U);

           }
       }
       inp.close();

       final XYSeriesCollection dataset = new XYSeriesCollection();
       dataset.addSeries(RED);
       dataset.addSeries(GREEN);
       dataset.addSeries(BLUE);
       dataset.addSeries(UNDEFINED);

       return dataset;
       }
       catch (Exception e) {
           JOptionPane.showMessageDialog(null, e, "Exception Raised",
                   JOptionPane.INFORMATION_MESSAGE);
           return null;
       }

   }   



    }

低于组合框的代码。

![//Create ComboBox for RGB Choice
String ChannelStrings\[\] = { "All", "Red", "Green", "Blue"};
JComboBox ChannelChoice = new JComboBox(ChannelStrings);
ChannelChoice.setFont((new Font ("Arial", Font.BOLD, 13)));
panelButton.add(ChannelChoice);][1]

1 个答案:

答案 0 :(得分:1)

如图所示here,您可以使用渲染器的setSeriesVisible()方法控制各个系列的可见性。引用的示例使用复选框进行控制,但您的组合操作侦听器可以调用相同的方法。 ChartFactory.createXYLineChart()使用XYLineAndShapeRenderer的实例;您可以从图表的图中获得参考,如here所示。

image