Skip to content
Snippets Groups Projects
Commit 567ef79e authored by timmolter's avatar timmolter
Browse files

Issue #2: Remove dependency on servlet jar when saving a png

parent 51d9cef2
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.xeiam.xchart; package com.xeiam.xchart.io;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
...@@ -21,7 +21,8 @@ import java.io.FileOutputStream; ...@@ -21,7 +21,8 @@ import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import com.xeiam.xchart.Chart;
/** /**
* @author timmolter * @author timmolter
...@@ -46,19 +47,4 @@ public class BitmapEncoder { ...@@ -46,19 +47,4 @@ public class BitmapEncoder {
out.close(); out.close();
} }
/**
* Streams a chart as a PNG file
*
* @param out
* @param chart
*/
public static void streamPNG(ServletOutputStream out, Chart chart) throws Exception {
BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D lGraphics2D = lBufferedImage.createGraphics();
chart.paint(lGraphics2D);
ImageIO.write(lBufferedImage, "png", out);
out.close();
}
} }
/**
* Copyright 2011-2012 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.io;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import com.xeiam.xchart.Chart;
/**
* @author timmolter
*/
public class ServletEncoder {
/**
* Streams a chart as a PNG file
*
* @param out
* @param chart
*/
public static void streamPNG(ServletOutputStream out, Chart chart) throws Exception {
BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D lGraphics2D = lBufferedImage.createGraphics();
chart.paint(lGraphics2D);
ImageIO.write(lBufferedImage, "png", out);
out.close();
}
}
...@@ -25,9 +25,9 @@ import javax.servlet.http.HttpServlet; ...@@ -25,9 +25,9 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.xeiam.xchart.BitmapEncoder;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
import com.xeiam.xchart.QuickChart; import com.xeiam.xchart.QuickChart;
import com.xeiam.xchart.io.ServletEncoder;
/** /**
* Generates, stores, and serves charts * Generates, stores, and serves charts
...@@ -50,7 +50,7 @@ public class ChartServletExample extends HttpServlet { ...@@ -50,7 +50,7 @@ public class ChartServletExample extends HttpServlet {
ServletOutputStream out = response.getOutputStream(); ServletOutputStream out = response.getOutputStream();
try { try {
BitmapEncoder.streamPNG(out, chart); ServletEncoder.streamPNG(out, chart);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -18,8 +18,8 @@ package com.xeiam.xchart.example; ...@@ -18,8 +18,8 @@ package com.xeiam.xchart.example;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import com.xeiam.xchart.BitmapEncoder;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
import com.xeiam.xchart.io.BitmapEncoder;
/** /**
* Creates a simple charts and saves it as aPNG image file. * Creates a simple charts and saves it as aPNG image file.
...@@ -28,7 +28,7 @@ import com.xeiam.xchart.Chart; ...@@ -28,7 +28,7 @@ import com.xeiam.xchart.Chart;
*/ */
public class Example1 { public class Example1 {
public static void main(String[] args) { public static void main(String[] args) throws Exception {
Collection<Number> xData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0 }); Collection<Number> xData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0 });
Collection<Number> yData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0 }); Collection<Number> yData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0 });
...@@ -40,10 +40,7 @@ public class Example1 { ...@@ -40,10 +40,7 @@ public class Example1 {
chart.setYAxisTitle("Y"); chart.setYAxisTitle("Y");
chart.addSeries("y(x)", xData, yData); chart.addSeries("y(x)", xData, yData);
try { BitmapEncoder.savePNG(chart, "./Sample_Chart.png");
BitmapEncoder.savePNG(chart, "./Sample_Chart.png");
} catch (Exception e) {
e.printStackTrace();
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment