Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Make chart servlet size configurable. Change legend background to tra…
Browse files Browse the repository at this point in the history
…nsparent.
  • Loading branch information
cdjackson committed Jan 22, 2014
1 parent 7aac2c5 commit 2bcc99b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@
public class ChartServlet extends HttpServlet implements ManagedService {

private static final long serialVersionUID = 7700873790924746422L;

private static final Integer CHART_HEIGHT = 240;
private static final Integer CHART_WIDTH = 480;

private static final Logger logger = LoggerFactory.getLogger(ChartServlet.class);

protected String providerName = "default";
protected Integer defaultHeight = CHART_HEIGHT;
protected Integer defaultWidth = CHART_WIDTH;
protected Double scale = 1.0;

// The URI of this servlet
public static final String SERVLET_NAME = "/chart";
Expand Down Expand Up @@ -145,16 +150,26 @@ protected void deactivate() {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
logger.debug("Received incoming chart request: ", req);

int width = 480;
int width = defaultWidth;

try {
width = Integer.parseInt(req.getParameter("w"));
String w = req.getParameter("w");
if(w != null) {
Double d = Double.parseDouble(w) * scale;
width = d.intValue();
}
} catch (Exception e) {
}
int height = 240;
int height = defaultHeight;
try {
height = Integer.parseInt(req.getParameter("h"));
String h = req.getParameter("h");
if(h != null) {
Double d = Double.parseDouble(h) * scale;
height = d.intValue();
}
} catch (Exception e) {
}

Long period = PERIODS.get(req.getParameter("period"));
if (period == null) {
// use a day as the default period
Expand All @@ -168,13 +183,14 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
String serviceName = req.getParameter("service");

ChartProvider provider = getChartProviders().get(providerName);
if(provider == null)
if (provider == null)
throw new ServletException("Could not get chart provider.");

// Set the content type to that provided by the chart provider
res.setContentType("image/"+provider.getChartType());
res.setContentType("image/" + provider.getChartType());
try {
BufferedImage chart = provider.createChart(serviceName, null, timeBegin, timeEnd, height, width, req.getParameter("items"), req.getParameter("groups"));
BufferedImage chart = provider.createChart(serviceName, null, timeBegin, timeEnd, height, width,
req.getParameter("items"), req.getParameter("groups"));
ImageIO.write(chart, provider.getChartType().toString(), res.getOutputStream());
} catch (ItemNotFoundException e) {
logger.debug("Item not found error while generating chart.");
Expand Down Expand Up @@ -228,10 +244,21 @@ public void updated(Dictionary<String, ?> properties) throws ConfigurationExcept

if(properties == null)
return;

if(properties.get("provider") != null) {
providerName = (String) properties.get("provider");
}
if(properties.get("defaultHeight") != null) {
defaultHeight = Integer.parseInt((String)properties.get("defaultHeight"));
}
if(properties.get("defaultWidth") != null) {
defaultWidth = Integer.parseInt((String)properties.get("defaultWidth"));
}
if(properties.get("scale") != null) {
scale = Double.parseDouble((String)properties.get("scale"));
if(scale < 0.5)
scale = 1.0;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ else if(period <= 604800) { // 1 week
chart.getStyleManager().setDatePattern(pattern);
chart.getStyleManager().setAxisTickLabelsFont(new Font("SansSerif", Font.PLAIN, 11));
chart.getStyleManager().setChartPadding(5);
chart.getStyleManager().setLegendBackgroundColor(Color.LIGHT_GRAY);
chart.getStyleManager().setChartBackgroundColor(Color.LIGHT_GRAY);
Color bkgndLegend = new Color(192,192,192,160);
chart.getStyleManager().setLegendBackgroundColor(bkgndLegend);
Color bkgndChart = new Color(192,192,192,224);
chart.getStyleManager().setChartBackgroundColor(bkgndChart);

chart.getStyleManager().setXAxisMin(startTime.getTime());
chart.getStyleManager().setXAxisMax(endTime.getTime());
Expand Down

0 comments on commit 2bcc99b

Please sign in to comment.