portlet 中的 JFreeChart
我想通过 portlet(Vaadin / Liferay 门户)中的按钮或菜单选择来更改图表(饼图)的颜色(背景颜色数据集颜色等..)。我有点不知道如何做到这一点,这是我的 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;
在 init 方法中,我配置菜单并尝试发送单击时的颜色,
final MenuBar.MenuItem config = menubar.addItem("Config", null);
newItem.addItem("PieChart", new Command(){
public void menuSelected(MenuItem selectedItem) {
pie.chart.setBackgroundPaint(Color.blue);
}
});
我使用相同的方法从另一个类更改子窗口的背景颜色工作正常,但似乎不适用于 servlet 。
I want to change the color of my Chart(Pie) (background Color dataset colors and so on ..) via a button or menu selection klick from an portlet (Vaadin / Liferay portal) . i kinda have no clue how to do that Here is my 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();}
}
what i want to do is , to send the Selected Color to the Servlet so the Color changes when i klick on a menu selection ( already have the menu ).
Any guide or similar would be great.
now lets say i want to change the background color of the chart. i would use chart.setBackgroundPaint(Color.blue); in the servlet to change the color manualy . but i want to do it from the portlet this is what i have tryed to do:
PieChart pie;
in the init method i configure the menu and try to send the color on click
final MenuBar.MenuItem config = menubar.addItem("Config", null);
newItem.addItem("PieChart", new Command(){
public void menuSelected(MenuItem selectedItem) {
pie.chart.setBackgroundPaint(Color.blue);
}
});
i use the same to change the background color of a subwindow from another class it works fine but does not seem to work within a servlet .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要了解 portlet 的操作阶段(单击菜单时触发)和 servlet 请求是完全独立的请求。
我不知道 Vaadin 的复杂性,但我假设它是一个像 JSF 一样的服务器端组件模型。因此,在您的
menuSelected
方法中,您需要更改图表的颜色(我看到您通过修改问题来做到这一点)。这是正确的,但是pie
及其数据如何与 servlet 共享? servlet 将使用颜色,因此您需要以某种方式传递它。这可以通过多种方法之一实现,这里列出了其中的几种:通常,共享会话状态不是一个好主意,因为它将组件紧密耦合在一起。如果参数包含敏感数据,URL 参数可能不是一个好的选择。但对于您的示例,URL 参数完全没问题。
因此,根据您在评论中编写的信息,您使用 Label 组件来呈现
标签。您可以这样说:
所以现在可以向 servlet 添加 URL 参数,例如:
所以这个 URL 参数将对 servlet 可见,servlet 可以从请求参数中读取它(如下例所示)并设置颜色
图表
。除了需要从用户那里获取输入之外,我还会稍微重新安排您的代码。主要是将 JDBC 驱动程序初始化移至 servlet 的
init()
方法,并确保连接在finally
块中关闭。 @home 还做出了有效的评论,理想情况下您将使用连接池来获取连接。我添加了一些注释来展示如何获取请求参数并将其与图表一起使用。 (编辑 - 并添加了一些用于设置颜色的基本逻辑)。
You need to understand that the portlet's action phase (which gets fired when the menu is clicked) and the servlet request are entirely separate requests.
I don't know the intricacies of Vaadin, but I'm assuming it's a server-side component model like JSF. So in your
menuSelected
method you need to change the colour of your chart (which I see you do via an amendment to your question). This is correct, but how ispie
and its data shared with the servlet? It is the servlet that will be using the colour, so you need to pass it somehow. This can be via one of several methods, of which a couple are listed here:Ordinarily, shared session state is a bad idea because it tightly couples components together. And URL parameters can be a bad option if the parameters contain sensitive data. But for your example URL parameters are perfectly fine.
So going by the information you wrote in the comment, you use a Label component to render the
<img>
tag. You say something like:So this can now add URL parameters to the servlet, like:
So this URL parameter will be visible to the servlet, which can read it from the request parameters (as in the example below) and set the colour on the
chart
.Aside from the requirement of getting input from the user, I would rearrange your code a bit. Mostly to move the JDBC driver initialisation to the servlet's
init()
method, and to ensure the connection is closed in afinally
block. @home also makes a valid comment that ideally you would be using a connection pool to grab the connection.I've added some comments to show how you would grab a request parameter and use it with your chart. (EDIT - and added some rudimentary logic for setting the colour).
类
org.jfree.chart.StandardChartTheme< /code>
是“
ChartTheme
接口的默认实现”。 来源 是查看所有可能性的好方法。The class
org.jfree.chart.StandardChartTheme
is the "default implementation of theChartTheme
interface." The source is a good way to see all the possibilities.