1

I did a Java application and need to add a graphics to that. I could do this, but I can only add a product (line) to each graph.

I wish I could add more.

Here is my code

String query="select date,price from produtcs where idProduct like 'Prod1'";
            JDBCCategoryDataset dataset = new JDBCCategoryDataset (CriaConexao.getConexao(),query);
            JFreeChart chart = ChartFactory.createLineChart(Record of Price", "Date", "Price", dataset, PlotOrientation.VERTICAL, false, true, true);
            BarRenderer renderer = null;
            CategoryPlot plot= null;
            renderer=new BarRenderer();
            ChartFrame frame = new ChartFrame("Record of Price", chart);
            frame.setVisible(true);
            frame.setSize(400,650);

Chart----

enter image description here

The record that show in chart---------

idProduct             Date  Price
Pro01           2014-05-29  19.1
Pro01           2014-05-29  18.8
Pro01           2014-05-29  18.7
Pro01           2014-05-29  18.9
Pro01           2014-05-29  18.7
Pro01           2014-05-29  18.5

The record that I want show in chart---------

idProduct             Date  Price
Pro01           2014-05-29  19.1
Pro01           2014-05-29  18.8
Pro02           2014-05-29  18.7
Pro02           2014-05-29  18.9
Pro03           2014-05-29  18.7
Pro03           2014-05-29  18.5

I try this query, but only show one line

String query="select date,price from produtcs where idProduct like 'Prod%'";

EDIT

I edit a new query:

SELECT p1.`Date`
     , p1.`Price` as `Price of Prod01`
     , p2.`Price` as `Price of Prod02`
     , concat(p1.idProduct, ' / ', p2.idProduct) idProduct
FROM   (SELECT idProduct, Date, Price
        FROM products
        WHERE idProduct LIKE 'Pro01') p1
       LEFT  JOIN (SELECT idProduct, Date, Price
                   FROM products
                   WHERE idProduct LIKE 'Pro02') p2
       ON p1.Date = p2.Date

And the result is:

 Date       Price of Prod01     Price of Prod02      Products
2014-05-29    23.8                  23.0                 BrgTH001 / BrgTH002
2014-05-29    23.8                  23.1               BrgTH001 / BrgTH002
2014-05-29    23.8                  22.6               BrgTH001 / BrgTH002
2014-05-29    23.8                  22.5               BrgTH001 / BrgTH002
2014-05-29    23.8                  22.8               BrgTH001 / BrgTH002
2014-05-29    23.8                  23.1               BrgTH001 / BrgTH002

But the result is one line again :S

rpirez
  • 431
  • 2
  • 8
  • 20

1 Answers1

1

As from the Javadoc on the JDBCCategoryDataset:

The first column will be the category name and remaining columns values (each column represents a series).

Hence if you put the price of your second product in the third column, that should work fine.

If, however, you cannot do that because of schema or data restrictions or what not, you have several other options available to you:

  1. Use a DefaultCategoryDataset as the closest thing to JDBCCategoryDataset and populate it manually.
  2. You seem to be creating a time series graph (values plotted against dates). As such, it is only reasonable to try a TimeSeriesCollection as your dataset. It is a collection of Time Series (surprise!), and each Time Series is a collection of (date, value) points that will be plotted on your graph as a line.
  3. A more general approach is to use Abstract Dataset and provide a renderer, but that requires quite a bit of background knowledge and is reserved for when you need something very non trivial. (But is useful to learn JFreeChart)

I would use option 2. A snippet of it may look like this:

TimeSeriesCollection timeSeries = new TimeSeriesCollection();
TimeSeries product1 = new TimeSeries("product1");
new JDBCTemplate("query", params, new ResultSetExtractor<Void>() {  
    public Void extractData(ResultSet rs) throws SQLException {
        product1.addOrUpdate(new FixedMillisecond(rs.getLong(1)), rs.getDouble(2));
        return null;
    }
}
<other products to follow>
Ordous
  • 3,844
  • 15
  • 25
  • Very thanks for reply Sir. I think I understand your answer, but i dont no how do that :S I'm a noob sorry. could you do me a favour and show me that code with mine? – rpirez May 29 '14 at 10:54
  • @user3686971 What is it that you do not know? How to add a third column to your SQL result? OR how to query things with JDBC (Admittedly I used Spring JDBCTemplate, not raw)? – Ordous May 29 '14 at 10:56
  • My sql result already has third column. 1-idProduct 2-Date 3-Price. If I could change the dataset in order to have more than one product and therefore more lines on the chart was ideal, was not it? – rpirez May 29 '14 at 11:03
  • And apologize for my ignorance Sir – rpirez May 29 '14 at 11:03
  • Sorry, I meant fourth column, first would be id, second date, third product1 price, fourth - product2 price – Ordous May 29 '14 at 11:14
  • Thanks again for reply. You are being very helpfull, but how can I put the price of my second product in the fourht column? – rpirez May 29 '14 at 11:18
  • You could do an outer join on the date, or use union of two separate queries. As I said in the answer, if you're not too good at SQL you might prefer loading everything into Java with separate queries and construct your own datasets, that might be easier. – Ordous May 29 '14 at 11:21
  • @Ordus I edit the question, with a new query, but the result is the same :S – rpirez May 29 '14 at 13:21
  • @user3686971 category datasets are very sensitive to column order and to what exactly you put there. I'm not going to write the code for you, especially since I've already given a fairly comprehensive snippet on another way of doing it. If you want to pursue the category route further - read the code, read the docs, it's not all that difficult to understand what it is doing with the query results – Ordous May 29 '14 at 16:25