2010-12-10 141 views
1


我有以下java(awt)代碼,我想將它轉換爲jsp.can中的任何一個告訴我如何執行此操作?
我的java類是StackedBarChart1.java如何將java(awt)代碼轉換爲jsp代碼

import java.awt.Color; 
import java.awt.GradientPaint; 
import java.awt.Paint; 
import java.awt.Rectangle; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.LegendItem; 
import org.jfree.chart.LegendItemCollection; 
import org.jfree.chart.axis.SubCategoryAxis; 
import org.jfree.chart.plot.CategoryPlot; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer; 
import org.jfree.data.KeyToGroupMap; 
import org.jfree.data.category.CategoryDataset; 
import org.jfree.data.category.DefaultCategoryDataset; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.GradientPaintTransformType; 
import org.jfree.ui.RefineryUtilities; 
import org.jfree.ui.StandardGradientPaintTransformer; 

public class StackedBarChart1 extends ApplicationFrame { 

    StackedBarChart1(String titel) { 
     super(titel); 

     final CategoryDataset dataset = createDataset(); 
     final JFreeChart chart = createChart(dataset); 
     final ChartPanel chartPanel = new ChartPanel(chart); 
     chartPanel.setPreferredSize(new java.awt.Dimension(800, 500)); 
     setContentPane(chartPanel); 
    } 

    private CategoryDataset createDataset() { 
     DefaultCategoryDataset result = new DefaultCategoryDataset(); 

     result.addValue(20.3, "Category 1 (D)", "Jan 08"); 
     result.addValue(27.2, "Category 1 (D)", "Feb 08"); 
     result.addValue(19.7, "Category 1 (D)", "Mar 08"); 
     result.addValue(20.7, "Category 1 (D)", "Apr 08"); 
     result.addValue(19.4, "Category 1 (E)", "Jan 08"); 
     result.addValue(10.9, "Category 1 (E)", "Feb 08"); 
     result.addValue(18.4, "Category 1 (E)", "Mar 08"); 
     result.addValue(12.4, "Category 1 (E)", "Apr 08"); 
     result.addValue(16.5, "Category 1 (W)", "Jan 08"); 
     result.addValue(15.9, "Category 1 (W)", "Feb 08"); 
     result.addValue(16.1, "Category 1 (W)", "Mar 08"); 
     result.addValue(14.4, "Category 1 (W)", "Apr 08"); 


     result.addValue(23.3, "Category 2 (D)", "Jan 08"); 
     result.addValue(16.2, "Category 2 (D)", "Feb 08"); 
     result.addValue(28.7, "Category 2 (D)", "Mar 08"); 
     result.addValue(22.7, "Category 2 (D)", "Apr 08"); 
     result.addValue(12.7, "Category 2 (E)", "Jan 08"); 
     result.addValue(17.9, "Category 2 (E)", "Feb 08"); 
     result.addValue(12.6, "Category 2 (E)", "Mar 08"); 
     result.addValue(14.6, "Category 2 (E)", "Mar 08"); 
     result.addValue(15.4, "Category 2 (W)", "Jan 08"); 
     result.addValue(21.0, "Category 2 (W)", "Feb 08"); 
     result.addValue(11.1, "Category 2 (W)", "Mar 08"); 
     result.addValue(16.1, "Category 2 (W)", "Apr 08"); 


     result.addValue(11.9, "Category 3 (D)", "Jan 08"); 
     result.addValue(31.0, "Category 3 (D)", "Feb 08"); 
     result.addValue(22.7, "Category 3 (D)", "Mar 08"); 
     result.addValue(18.7, "Category 3 (D)", "Apr 08"); 
     result.addValue(15.3, "Category 3 (E)", "Jan 08"); 
     result.addValue(14.4, "Category 3 (E)", "Feb 08"); 
     result.addValue(25.3, "Category 3 (E)", "Mar 08"); 
     result.addValue(16.3, "Category 3 (E)", "Apr 08"); 
     result.addValue(23.9, "Category 3 (W)", "Jan 08"); 
     result.addValue(19.0, "Category 3 (W)", "Feb 08"); 
     result.addValue(10.1, "Category 3 (W)", "Mar 08"); 
     result.addValue(18.1, "Category 3 (W)", "Apr 08"); 


     return result; 
    } 

    private JFreeChart createChart(final CategoryDataset dataset) { 

     final JFreeChart chart = ChartFactory.createStackedBarChart(
       "Referral Click Analysis (Stacked Bar Chart)", "Category", "Value", dataset, 
       PlotOrientation.VERTICAL, true, true, false); 

     chart.setBackgroundPaint(new Color(249, 231, 236)); 

     GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer(); 
     KeyToGroupMap map = new KeyToGroupMap("G1"); 

     map.mapKeyToGroup("Category 1 (D)", "G1"); 
     map.mapKeyToGroup("Category 1 (E)", "G1"); 
     map.mapKeyToGroup("Category 1 (W)", "G1"); 


     map.mapKeyToGroup("Category 2 (D)", "G2"); 
     map.mapKeyToGroup("Category 2 (E)", "G2"); 
     map.mapKeyToGroup("Category 2 (W)", "G2"); 


     map.mapKeyToGroup("Category 3 (D)", "G3"); 
     map.mapKeyToGroup("Category 3 (E)", "G3"); 
     map.mapKeyToGroup("Category 3 (W)", "G3"); 

     renderer.setSeriesToGroupMap(map); 


     renderer.setItemMargin(0.0); 
     Paint p1 = new GradientPaint(
       0.0f, 0.0f, new Color(16, 89, 172), 0.0f, 0.0f, new 
               Color(201, 201, 244)); 
     renderer.setSeriesPaint(0, p1); 
     renderer.setSeriesPaint(3, p1); 
     renderer.setSeriesPaint(6, p1); 

     Paint p2 = new GradientPaint(
       0.0f, 0.0f, new Color(10, 144, 40), 0.0f, 0.0f, new 
               Color(160, 240, 180)); 
     renderer.setSeriesPaint(1, p2); 
     renderer.setSeriesPaint(4, p2); 
     renderer.setSeriesPaint(7, p2); 

     Paint p3 = new GradientPaint(
       0.0f, 0.0f, new Color(255, 35, 35), 0.0f, 0.0f, new 
               Color(255, 180, 180)); 
     renderer.setSeriesPaint(2, p3); 
     renderer.setSeriesPaint(5, p3); 
     renderer.setSeriesPaint(8, p3); 


     renderer.setGradientPaintTransformer(
       new StandardGradientPaintTransformer 
         (GradientPaintTransformType.HORIZONTAL)); 

     SubCategoryAxis domainAxis = new SubCategoryAxis("Category/Month"); 
     domainAxis.setCategoryMargin(0.05); 
     domainAxis.addSubCategory("Category 1"); 
     domainAxis.addSubCategory("Category 2"); 
     domainAxis.addSubCategory("Category 3"); 

     CategoryPlot plot = (CategoryPlot) chart.getPlot(); 
     plot.setDomainAxis(domainAxis); 
     plot.setRenderer(renderer); 
     plot.setFixedLegendItems(createLegendItems()); 
     return chart; 
    } 

    private LegendItemCollection createLegendItems() { 
     LegendItemCollection result = new LegendItemCollection(); 
     LegendItem item1 = new LegendItem("D", "D", "D", "D", 
       new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f, 
       new Color(16, 89, 172), 0.0f, 0.0f, new Color(201, 201, 244))); 
     LegendItem item2 = new LegendItem("E", "E", "E", "E", 
       new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f, 
       new Color(10, 144, 40), 0.0f, 0.0f, new Color(160, 240, 180))); 
     LegendItem item3 = new LegendItem("W", "W", "W", "W", 
       new Rectangle(10, 10), new GradientPaint(0.0f, 0.0f, 
       new Color(255, 35, 35), 0.0f, 0.0f, new Color(255, 180, 180))); 

     result.add(item1); 
     result.add(item2); 
     result.add(item3); 
     return result; 
    } 

    public static void main(final String[] args) { 
     final StackedBarChart1 demo = new StackedBarChart1("Referral Click Analysis "); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 
} 

感謝
Yugal

回答

2

在JSP中,你可以內<% .. %>編寫任何Java代碼。但你最好不要。重新使用您當前的課程。你可以用<%@ page import=".." %>導入它。但是,再次 - 你應該避免在JSP中編寫任何Java代碼。所以你最好在一個servlet中完成這個任務(你可以導入你現有的類)

0

其實非常簡單..,將生成的JFreeChart對象作爲圖像(gif,jpg)保存在webapp上下文路徑中,然後發送來自你的jsp的圖像。

你完成了。

1

嚴格地說,Bozho的答案是正確的,但這不會神奇地轉換成一個桌面應用程序AWT成一個Web應用程序。沒有簡單的方法將AWT桌面應用轉換爲Web應用。

你真的有兩個選擇:

1)尋找到小程序,以此來嵌入你的應用程序到網頁。

2)翻譯您的AWT代碼到一些web應用程序的用戶界面技術,如谷歌Web工具包。