Read the data from the database and integrate it into a 3D pie chart to show details in jsp. 3 djsp
Preface
This article mainly introduces the content about reading database data, integrating it into a 3D pie chart and displaying it in jsp, and sharing it for your reference, let's take a look at the detailed introduction:
The method is as follows:
First, I will write the pie chart generation method into a PieChar. java class independently. The detailed code is as follows: (the database needs to be built by myself, if necessary)
Import java. io. IOException; import java. SQL. SQLException; import org. jfree. chart. chartFactory; import org. jfree. chart. JFreeChart; import org. jfree. data. general. defaultPieDataset; public class PieChart {public JFreeChart chart = null; public PieChart () throws IOException, SQLException {defapipiedataset data = getDataset (); chart = ChartFactory. createPieChart3D ("commodity quantity pie chart", data, true, false, false); // create a pie chart} // custom method, generate pie chart dataset private static defapipiedataset getDataset () throws SQLException {defapipiedataset dabaset = new partition (); ConnDB conn = new ConnDB (); String SQL = "select p_type, count (p_type) as p_type_count from product group by (p_type) "; conn. rs = conn. doQuery (SQL); // read database data and generate a dataset while (conn. rs. next () {dabaset. setValue (conn. rs. getString ("p_type"), conn. rs. getInt ("p_type_count");} return dabaset ;}}
Then, call the java class in the jsp file. The detailed code is as follows:
<Jsp: useBean id = "pies" class = "bean. pieChart "> </jsp: useBean> <body> <% String filename = ServletUtilities. saveChartAsPNG (pies. chart, 500,300, null, session); // generate the image path String graphURL = request. getContextPath () + "/servlet/DisplayChart? Filename = "+ filename; // The complete path of the generated image in the project: System. out. println (graphURL); %> <div align = "center"> </div> </body>
This is not enough. You must configure the web. xml file to display the image. The detailed configuration is as follows:
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class></servlet><servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern></servlet-mapping>
Complete servlet ing.
The final result is as follows:
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message to us. Thank you for your support.