0

I have a graph showing the lines in 3 colours (red, green, blue), like screenshot.

When I press one of the colours, i.e. red, I want to show graph with only red colour. And if I press green then only green. But if I press nothing then it should show all.

Below my code for creating and drawing the lines.

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

   }   



    }

And below the code for the combobox.

![//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 Answers1

1

As shown here, you can control the visibility of individual series using the renderer's setSeriesVisible() method. The example cited uses checkboxes for control, but your combo's action listener can invoke the same method. ChartFactory.createXYLineChart() uses an instance of XYLineAndShapeRenderer; you can get a reference from the chart's plot, as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for this. Could you also give me some hints how to place this into my code? – michaelsteven May 30 '14 at 10:10
  • The `setSeriesVisible()` call goes in your combo's action listener; please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that shows your revised approach; in your example, access posted images via `URL`, as shown [here](http://stackoverflow.com/a/10862262/230513); use synthetic images as shown [here](http://stackoverflow.com/a/15982915/230513); or use `UIManager` icons, as shown [here](http://stackoverflow.com/a/12228640/230513).. – trashgod May 30 '14 at 10:21
  • Thx for the hint. But I still do not know how to implement the setSeriesVisible() into my code. Could you please help. I really need your help. – michaelsteven Jun 05 '14 at 12:45
  • I'm not sure where you're stuck. Please edit your question to include an [mcve](http://stackoverflow.com/help/mcve) that shows your current approach. – trashgod Jun 05 '14 at 13:51
  • Sorry, but how can I place a further code in here? I already put my code on top of this query. – michaelsteven Jun 05 '14 at 14:13