JFreeChart in the portlet

I want to change the color of my chart (Pie) (colors of a color set of colors, etc.) using a button or selecting the klick menu from the portlet (Vaadin / Liferay portal). I do not know 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 send the selected color to the servlet so that the color changes when I select the menu selection (there is already a menu).

Any guidance or the like would be great.


Now we can say that I want to change the background color of the chart. I would use chart.setBackgroundPaint (Color.blue); in the servlet to change the color manually. but I want to do this from the portlet, this is what I tried to do:

 PieChart pie; 

in the init method, I customize the menu and try to send the color when clicked

 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 in a subsequence from another class, it works fine, but doesn't seem to work in the servlet.

0
source share
2 answers

You need to understand that the phase of the portlet (which starts when you click on the menu) and the servlet request are completely separate requests.

I do not know the intricacies of Vaadin, but I assume that it is a server-side component such as JSF. Therefore, in your menuSelected method menuSelected you need to change the color of the chart (what I see, you are doing with the correction to your question). That's right, but how did pie and its data share with the servlet? This is a servlet that will use color, so you need to somehow convey it. This may be one of several methods, of which a couple is given here:

  • shared session state
  • URL parameter

Generally, the overall state of a session is a bad idea because it closely connects the components together. And URL parameters can be a bad option if the parameters contain sensitive data. But for your example, the URL parameters are fine.

So, after going over the information that you indicated in the comment, you use the Label component to render the <img> . You say something like:

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

So, now you can add the URL parameters to the servlet, for example:

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

Thus, 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 color to chart .

In addition to requiring user input, I would modify your code a bit. Basically, to move the JDBC driver initialization to the init() servlet method and make sure the connection is closed in the finally block. @home also makes the right comment that ideally you will use the connection pool to capture the connection.

I added a few comments to show how you take the query parameter and use it in your chart. (EDIT - and added some rudimentary logic to set the color).

 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); } } } } } 
+2
source

The org.jfree.chart.StandardChartTheme class is the "default implementation of the ChartTheme interface ChartTheme " The source is a good way to see all the possibilities.

+2
source

Source: https://habr.com/ru/post/902371/


All Articles