本文介绍了如何在freechart Java中添加多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个Java应用程序,需要在上面添加图形.我可以这样做,但是我只能向每个图形添加一个产品(线).

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.

这是我的代码

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

图表----

图表中显示的记录---------

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

我要在图表中显示的记录---------

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

我编辑一个新查询:

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

结果是:

 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

但是结果又是一行:S

But the result is one line again :S

推荐答案

JDBCCategoryDataset上的Javadoc所述:

As from the Javadoc on the JDBCCategoryDataset:

因此,如果您将第二个产品的价格放在第三栏中,则应该可以正常工作.

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. 使用DefaultCategoryDataset作为最接近JDBCCategoryDataset的对象,并手动填充它.
  2. 您似乎正在创建时间序列图(针对日期绘制的值).因此,尝试将TimeSeriesCollection作为数据集是合理的.它是Time Series的集合(惊奇!),每个Time Series是(日期,值)点的集合,这些点将在图形上绘制为一条线.
  3. 更通用的方法是使用Abstract Dataset并提供渲染器,但这需要大量的背景知识,并且在您需要非常不琐碎的内容时保留. (但对于学习JFreeChart很有用)
  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)

我将使用选项2.其摘要可能如下所示:

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>

这篇关于如何在freechart Java中添加多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:31