Help please

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Guest

Help please

Post by Guest » Sun Mar 20, 2005 11:29 pm

Hello, everybody.

I want to resolve next issue:
I have JPanel and want to locate on this panel XYBarChart and XYLineChart.

Which data model I must use? Can I resolve this task using JFreeChart?
When I have learned Delphi in lyceum (11th form, and now I'm student of forth course) I remembered that built-in Delphi TChart component can do this (locate all types of data models in one component TChart).

Thanks a lot for any answering.

Best regards, Alex

P.S. Am I right using next code to resolve problem.

Code: Select all

public class BarChart extends JPanel {
    
       public BarChart(LinkedList classes) {
          XYBarDataset dataset = createDataset(classes);
          JFreeChart chart = createChart(dataset);
          chart = customizeChart(chart);
          ChartPanel chartPanel = new ChartPanel(chart);
          chartPanel.setPreferredSize(new Dimension(500, 270));
          add(chartPanel);
          setVisible(true);
       }
       
       private XYBarDataset createDataset(LinkedList classes) {
           XYSeriesCollection coll = new XYSeriesCollection();
           XYSeries series = new XYSeries("Relative Frequencies");
           for (int i = 0; i < classes.size(); i++) {              
               Class c = (Class)classes.get(i);
               series.add(c.getBegin(), c.getRelativeFrequency());
           }
           XYSeries relFreqSeries = new XYSeries("Next series");
           relFreqSeries.add(134, 123);
           relFreqSeries.add(135, 122);
           relFreqSeries.add(136, 121);
           relFreqSeries.add(137, 120);
           relFreqSeries.add(138, 124);
           relFreqSeries.add(139, 125);
           relFreqSeries.add(130, 126);
           
           coll.addSeries(series);
           coll.addSeries(relFreqSeries);
           
           XYBarDataset dataset = new XYBarDataset(coll,  ((Class)classes.get(0)).getEnd() - ((Class)classes.get(0)).getBegin());                     
          return dataset;
       }

       private JFreeChart createChart(final IntervalXYDataset dataset){
          JFreeChart chart = ChartFactory.createXYBarChart(
             "Class frequency", // chart title
             "Elements", // domain axis label
             /*"Frequencies"*/false, // range axis label
             "Frequencies",
             dataset, // data
             PlotOrientation.VERTICAL, // orientation
             true, // include legend
             true, // tooltips?
             false // URLs?
             );
          return chart;
       }

       private JFreeChart customizeChart(final JFreeChart chart){
          return chart;
       }
}

Alex Manoylenko
Posts: 18
Joined: Fri Mar 11, 2005 9:41 pm
Location: Ukraine
Contact:

Post by Alex Manoylenko » Sun Mar 20, 2005 11:36 pm

Help please!

Birgi
Posts: 21
Joined: Mon Mar 14, 2005 9:09 pm

Post by Birgi » Mon Mar 21, 2005 11:05 am

Alex,

You can create two ChartPanel's with your charts as parameters, and then add these to a JPanel.

example:

Code: Select all

ChartPanel cp1 = new ChartPanel(chart1);
ChartPanel cp2 = new ChartPanel(chart2);
...
JPanel panel = new JPanel();
... // panel properties
panel.add(cp1);
panel.add(cp2);
...

Alex Manoylenko
Posts: 18
Joined: Fri Mar 11, 2005 9:41 pm
Location: Ukraine
Contact:

Post by Alex Manoylenko » Mon Mar 21, 2005 11:12 am

Thanks Birgi.

Maybe I haven't explained my problem simple.

I didn't want create two JPanel's I want to create one JPanel where I can build both bar chart and simple function (y = f(x)). AT THE SAME JPanel !!!

Is there way to do it?

Thanks a lot.

Best regards, Alex

Alex Manoylenko
Posts: 18
Joined: Fri Mar 11, 2005 9:41 pm
Location: Ukraine
Contact:

Post by Alex Manoylenko » Mon Mar 21, 2005 7:55 pm

Hi, everybody.

These are the issues that I want to resolve using JFreeChart library.
Can I do this?

a) I want to build bar chart and some functions (y1=f(x1), y2=g(x2), etc.) in one JFreeChart

Image

b) I want to build step function (arrows on picture) and some functions

Image

Any answer will be very appreciated.

Best regards, Alex

Alex Manoylenko
Posts: 18
Joined: Fri Mar 11, 2005 9:41 pm
Location: Ukraine
Contact:

Post by Alex Manoylenko » Tue Mar 22, 2005 2:01 pm

Help please!

I need your help.

A lot of thanks, Alex

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Mar 22, 2005 6:41 pm

JFreeChart doesn't support functions, so you first need to convert your functions into a dataset containing (x, y) coordinates sampled from your function.

Once that is done, the general approach to overlaying two different types of charts on top of one another is to use multiple datasets. Each dataset has a separate renderer. Your first example could use two dataset, and an XYLineAndShapeRenderer to draw the 3 series that are represented as lines, and an XYBarRenderer to draw the series that is represented as a bar.

For your second example, again you would use 2 datasets and 2 renderers, and you would replace the XYBarRenderer with an XYStepRenderer (probably you would need to modify the source code to draw the arrows).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Alex Manoylenko
Posts: 18
Joined: Fri Mar 11, 2005 9:41 pm
Location: Ukraine
Contact:

Post by Alex Manoylenko » Wed Mar 23, 2005 2:47 pm

Hello, David.

Thanks a lot for your rapid answer.

I try to do this.
I think JFreeChart makes my life more buitiful :)

Best regards,
Alex

Alex Manoylenko
Posts: 18
Joined: Fri Mar 11, 2005 9:41 pm
Location: Ukraine
Contact:

Post by Alex Manoylenko » Wed Mar 30, 2005 4:11 pm

Hello, everybody!

I have some problem creating XYStepChart.

Here is source code:

Code: Select all

public class StepFunctionChart extends JPanel {
    
       public StepFunctionChart(LinkedList classes) {                                 
            XYSeriesCollection dataset = createDataset(classes);
            JFreeChart chart = createChart(dataset);
            chart = customizeChart(chart);
            ChartPanel chartPanel = new ChartPanel(chart);
            chartPanel.setPreferredSize(new Dimension(500, 270));
            add(chartPanel);
            setVisible(true);
       }
       
       private XYSeriesCollection createDataset(LinkedList classes) {
           
            XYSeries series = new XYSeries("Sum Polygon", false, true);
           
            for (int i = 0; i < classes.size(); i++) {              
                Class c = (Class)classes.get(i);
                XYDataItem data = new XYDataItem(c.getBegin(), c.getSumPolygon());
                series.add(data);
                data = new XYDataItem(c.getEnd(), c.getSumPolygon());
                series.add(data);
            }
           
            XYSeriesCollection xy = new XYSeriesCollection();
            xy.addSeries(series);  
            return xy;
       }

       private JFreeChart createChart(final XYSeriesCollection dataset){
           NumberAxis xAxis = new NumberAxis("Elements");
           NumberAxis yAxis = new NumberAxis("Sum polygon");
           
           XYStepRenderer renderer = new XYStepRenderer(new StandardXYToolTipGenerator(), null);
           XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
           plot.setOrientation(PlotOrientation.VERTICAL);
           JFreeChart ch = new JFreeChart("Sum polygon", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
          return ch;
       }

       private JFreeChart customizeChart(final JFreeChart chart){
          return chart;
       }
}
and I have next code that draws bar chart, both classes use specific data for building dataset, but I don't understand why first of this classes doesn't reproduse any result as you can see on the picture (second JPanel)

Code: Select all

 public class BarChart extends JPanel {
    
       public BarChart(VariationalSeries vs) {
          XYBarDataset dataset = createDataset(vs.getClasses());
          JFreeChart chart = createChart(dataset, vs);
          chart = customizeChart(chart);
          ChartPanel chartPanel = new ChartPanel(chart);
          chartPanel.setPreferredSize(new Dimension(500, 270));
          add(chartPanel);
          setVisible(true);
       }
       
       private XYBarDataset createDataset(LinkedList classes) {
            XYSeriesCollection coll = new XYSeriesCollection();
            XYSeries series = new XYSeries("Relative Frequencies");
            for (int i = 0; i < classes.size(); i++) {              
                Class c = (Class)classes.get(i);
                series.add(c.getBegin(), c.getRelativeFrequency());
            }
            coll.addSeries(series);
            XYBarDataset dataset = new XYBarDataset(coll, ((Class)classes.get(0)).getEnd() - ((Class)classes.get(0)).getBegin());
            return dataset;
       }

       private JFreeChart createChart(final IntervalXYDataset dataset, VariationalSeries vs/*LinkedList classes*/) {
          JFreeChart chart = ChartFactory.createXYBarChart(
             "Class frequency", // chart title
             "Elements", // domain axis label
             false, // range axis label
             "Frequencies",
             dataset, // data
             PlotOrientation.VERTICAL, // orientation
             true, // include legend
             true, // tooltips?
             false // URLs?
             );
             
            XYPlot plot = chart.getXYPlot();// .getCategoryPlot(); 
           XYSeries series = new XYSeries("", false, true);
           int temp = 0;
           
           for (int i = 0; i < vs.getElements().size(); i++) {
               double element = ((Double)(vs.getElements().get(i))).doubleValue();
               double estimatedRelFreq = ((Double)(vs.getEstimatedRelativeFrequencies().get(i))).doubleValue();
               XYDataItem data = new XYDataItem(element, estimatedRelFreq);
               series.add(data);      
           }
           
           System.out.println("POINTS:" + series.getItemCount());
           XYSeriesCollection xy = new XYSeriesCollection();
           xy.addSeries(series);  

            
            plot.setDataset(1, xy);
            
            XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true,false); 
            plot.setRenderer(1, lineRenderer); 
            plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);                                                     
          return chart;
       }

       private JFreeChart customizeChart(final JFreeChart chart){
          return chart;
       }
}
Picture (http://chartproject.nm.ru/toSend.jpg) :

Image

Can somebody explain me, am I right to use this dataset (XYSeriesCollection) for creating right step chart.

Thanks a lot for any answer.

Best regards,
Alex

Alex Manoylenko
Posts: 18
Joined: Fri Mar 11, 2005 9:41 pm
Location: Ukraine
Contact:

Post by Alex Manoylenko » Fri Apr 01, 2005 5:40 pm

Hello, everybody!!!

Can somebody explain me which dataset I MUST use to create right step chart?

I use XYSeriesCollection but I can't see anything on my JPanel.
Am I right using this kind of dataset?

Thanks a lot.

I need your help.

Best regards,
Alex

Locked