【问题标题】:JFreeChart in portletPortlet 中的 JFreeChart
【发布时间】:2011-09-06 13:25:23
【问题描述】:

我想通过 portlet(Vaadin / Liferay 门户)中的按钮或菜单选择来更改我的 Chart(Pie) 的颜色(背景颜色数据集颜色等..)。我有点不知道该怎么做这是我的 Servlet:

import org.jfree.data.jdbc.JDBCPieDataset;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;

public class PieChart extends HttpServlet {


  /**
     * 
     */
    private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Connection connection = null;
    try {
      Class.forName("org.postgresql.Driver").newInstance();
      try {
            connection = DriverManager.getConnection(
                    "jdbc:postgresql://localhost:5432/db", "user",
                    "password");
      } catch (SQLException e) {
        e.printStackTrace();
      }
    } 
    catch (InstantiationException e) {
      e.printStackTrace();
    } 
    catch (IllegalAccessException e) {
      e.printStackTrace();
    } 
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }

    JDBCPieDataset dataset = new JDBCPieDataset(connection);
    try {
      dataset.executeQuery("Select * From my_table");
      JFreeChart chart = ChartFactory.createPieChart("Pie Chart", dataset, true, false, false);
      if (chart != null) {
        response.setContentType("image/png");
        OutputStream out = response.
        getOutputStream();
        ChartUtilities.writeChartAsPNG(out, chart, 450, 400);
      }
    } 
    catch (SQLException e) {
      e.printStackTrace();
    }
    try {
      if(connection != null){connection.close();} 
    }
    catch (SQLException e) {e.printStackTrace();}
}

我想要做的是,将选定的颜色发送到 Servlet,以便当我单击菜单选择(已经有菜单)时颜色会发生变化。

任何指南或类似的都会很棒。


现在假设我想更改图表的背景颜色。我会使用 chart.setBackgroundPaint(Color.blue);在 servlet 中手动更改颜色。但我想从 portlet 中做到这一点,这是我试图做的:

PieChart pie;

在初始化方法中,我配置菜单并尝试在点击时发送颜色

final MenuBar.MenuItem config = menubar.addItem("Config", null);

 newItem.addItem("PieChart", new Command(){
       public void menuSelected(MenuItem selectedItem) {
           pie.chart.setBackgroundPaint(Color.blue);
       }
 });

我使用相同的方法从另一个类更改子窗口的背景颜色,它工作正常,但似乎在 servlet 中不起作用。

【问题讨论】:

  • 顺便说一句:你应该看看连接池的概念(javax.sql.DataSource)...

标签: java servlets liferay portlet jfreechart


【解决方案1】:

您需要了解 portlet 的操作阶段(在单击菜单时触发)和 servlet 请求是完全独立的请求。

我不知道 Vaadin 的复杂性,但我假设它是一个类似于 JSF 的服务器端组件模型。因此,在您的 menuSelected 方法中,您需要更改图表的颜色(我看到您通过修改您的问题来做到这一点)。这是正确的,但pie 及其数据如何与 servlet 共享?将使用该颜色的是 servlet,因此您需要以某种方式传递它。这可以通过多种方法中的一种来实现,这里列出了其中的几种:

  • 共享会话状态
  • 网址参数

通常,共享会话状态不是一个好主意,因为它将组件紧密耦合在一起。如果参数包含敏感数据,则 URL 参数可能是一个糟糕的选择。但是对于您的示例 URL 参数来说非常好。

因此,根据您在评论中所写的信息,您使用 Label 组件来呈现 <img> 标记。你这样说:

new Label("img link",Label.CONTEXT_XHTML));

所以现在可以将 URL 参数添加到 servlet,例如:

new Label("<img src='/path/to/servlet?background=blue'>",Label.CONTEXT_XHTML));

所以这个 URL 参数对 servlet 是可见的,它可以从请求参数中读取它(如下例所示)并在 chart 上设置颜色。

除了从用户那里获取输入的要求之外,我会重新排列您的代码。主要是将 JDBC 驱动程序初始化移动到 servlet 的 init() 方法,并确保连接在 finally 块中关闭。 @home 还给出了一个有效的注释,即理想情况下您将使用连接池来获取连接。

我添加了一些 cmets 来展示如何获取请求参数并将其与图表一起使用。 (编辑 - 并添加了一些用于设置颜色的基本逻辑)。

public class PieChart extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void init() throws ServletException {
        super.init();
        try {
            Class.forName("org.postgresql.Driver").newInstance();
        } catch (InstantiationException e) {
            throw new ServletException(e);
        } catch (IllegalAccessException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String background = request.getParameter("background");
        // do some checks on colour here
        // such as null check and checking it is a valid colour value for your
        // chart

        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/db", "user", "password");
            JDBCPieDataset dataset = new JDBCPieDataset(connection);
            dataset.executeQuery("Select * From my_table");
            JFreeChart chart = ChartFactory.createPieChart("Pie Chart", dataset, true, false, false);

            if ("blue".equals(background)) {
                chart.setBackgroundPaint(Color.blue)
            }

            // why the null test? can chart ever be (sensibly) null?
            if (chart != null) {
                response.setContentType("image/png");
                OutputStream out = response.getOutputStream();
                ChartUtilities.writeChartAsPNG(out, chart, 450, 400);
            }
        } catch (SQLException e) {
            log("Exception retrieving chart data", e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    log("Could not close Connection", e);
                }
            }
        }
    }
}

【讨论】:

  • 要在我的 portlet 中显示图像,我确实使用了 img 标签: (vaadin) Window window = new Window(new Label("img link",Label.CONTEXT_XHTML));对我来说很好(目前)。
  • 完美,正是我需要的,我欠你一个:)
  • 太棒了!感谢您的评论,如果您还有其他需要,请告诉我。
【解决方案2】:

org.jfree.chart.StandardChartTheme 类是“ChartTheme 接口的默认实现”。 source 是查看所有可能性的好方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2011-12-29
    • 2011-04-07
    相关资源
    最近更新 更多