Skip to content
Snippets Groups Projects
Commit 2337b4dc authored by Tim Molter's avatar Tim Molter
Browse files

removed servlet dependency

parent 2d943400
No related branches found
No related tags found
No related merge requests found
......@@ -42,16 +42,6 @@
<url>http://ci.xeiam.com/</url>
</ciManagement>
<dependencies>
<!-- only needed if using XCharts as part of a webapp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Ensure compilation is done under Java 6 in all environments -->
......
/**
* Copyright 2011-2013 Xeiam LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
/**
* A helper class to be used in conjuction with a Servlet for streaming a Chart to an HTTP client
*
* @author timmolter
*/
public final class ServletEncoder {
/**
* Constructor - Private constructor to prevent instantiation
*/
private ServletEncoder() {
}
/**
* Streams a chart as a PNG file
*
* @param out
* @param chart
* @throws IOException
*/
public static void streamPNG(ServletOutputStream out, Chart chart) throws IOException {
BufferedImage lBufferedImage = new BufferedImage(chart.width, chart.height, BufferedImage.TYPE_INT_RGB);
Graphics2D lGraphics2D = lBufferedImage.createGraphics();
chart.paint(lGraphics2D);
ImageIO.write(lBufferedImage, "png", out);
out.close();
}
}
/**
* Copyright 2011-2013 Xeiam LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.example;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.QuickChart;
import com.xeiam.xchart.ServletEncoder;
/**
* Generates, stores, and serves charts
*
* @author timmolter
*/
@javax.servlet.annotation.WebServlet(name = "ChartServlet", urlPatterns = { "/chart" }, asyncSupported = true)
public class ChartServletExample extends HttpServlet {
private static Map<String, Chart> chartMap = new HashMap<String, Chart>();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
String id = request.getParameter("id");
Chart chart = chartMap.get(id);
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
try {
ServletEncoder.streamPNG(out, chart);
} catch (Exception e) {
e.printStackTrace();
}
out.close();
chart = null;
chartMap.remove(id);
}
/**
* Generates a chart and puts it in chartMap
*
* @return UUID uniquely identifying chart in the chartMap
*/
public static String generateDummyChart() {
Chart chart = QuickChart.getChart("Sample Chart", "X", "Y", null, null, new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
String uuid = UUID.randomUUID().toString();
chartMap.put(uuid, chart);
return uuid;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment