gpt4 book ai didi

java - 如何将java(awt)代码转换成jsp代码

转载 作者:行者123 更新时间:2023-11-29 06:17:46 25 4
gpt4 key购买 nike


我有以下 java (awt) 代码,我想在 jsp 中转换它。谁能告诉我该怎么做?
我的 java 类是 StackedBarChart1.java

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);
}
}

谢谢
尤加尔

最佳答案

在 JSP 中,您可以在 <% .. %> 内编写任何 Java 代码.但你最好不要。重用您当前的类(class)。您可以使用 <%@ page import=".." %> 导入它.但同样 - 您应该避免在 JSP 中编写任何 Java 代码。所以你最好在 servlet 中执行此操作(你可以在其中导入现有类)

关于java - 如何将java(awt)代码转换成jsp代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4406871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com