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

[UI] Charts Enhancements: Themes, DPI, legend hiding #4291

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -496,6 +496,7 @@ private WidgetDTO createWidgetBean(String sitemapName, Widget widget, boolean dr
Chart chartWidget = (Chart) widget;
bean.service = chartWidget.getService();
bean.period = chartWidget.getPeriod();
bean.legend = chartWidget.getLegend();
if (chartWidget.getRefresh() > 0) {
bean.refresh = chartWidget.getRefresh();
}
Expand Down
Expand Up @@ -45,6 +45,7 @@ public class WidgetDTO {
public String encoding;
public String service;
public String period;
public Boolean legend;

public EnrichedItemDTO item;
public PageDTO linkedPage;
Expand Down
Expand Up @@ -58,7 +58,7 @@ Video:

Chart:
'Chart' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & ('icon=' icon=Icon)? &
('service=' service=(STRING))? & ('refresh=' refresh=INT)? & ('period=' period=ID) &
('service=' service=(STRING))? & ('refresh=' refresh=INT)? & ('period=' period=ID) & ('legend=' legend=BOOLEAN_OBJECT)? &
('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? &
('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? &
('visibility=[' (Visibility+=VisibilityRule (',' Visibility+=VisibilityRule)* ']'))?);
Expand Down Expand Up @@ -160,4 +160,6 @@ terminal ID:

terminal FLOAT returns ecore::EBigDecimal:
INT '.' INT;


BOOLEAN_OBJECT returns ecore::EBooleanObject:
'true' | 'false';
Expand Up @@ -9,7 +9,7 @@
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" immediate="true" name="org.eclipse.smarthome.ui.chart.defaultprovider">
<implementation class="org.eclipse.smarthome.ui.internal.chart.DefaultChartProvider"/>
<implementation class="org.eclipse.smarthome.ui.internal.chart.defaultchartprovider.DefaultChartProvider"/>
<reference bind="setItemUIRegistry" cardinality="1..1" interface="org.eclipse.smarthome.ui.items.ItemUIRegistry" name="ItemUIRegistry" policy="dynamic" unbind="unsetItemUIRegistry"/>
<reference bind="addPersistenceService" cardinality="0..n" interface="org.eclipse.smarthome.core.persistence.PersistenceService" name="PersistenceService" policy="dynamic" unbind="removePersistenceService"/>
<service>
Expand Down
Expand Up @@ -18,6 +18,7 @@
* chart servlet and returns a chart image object (PNG).
*
* @author Chris Jackson
* @author Holger Reichert - Support for themes, DPI, legend hiding
*
*/
public interface ChartProvider {
Expand Down Expand Up @@ -52,6 +53,10 @@ public interface ChartProvider {
* The items to display on the chart
* @param groups
* The groups to display on the chart
* @param dpi
* The DPI (dots per inch) value, can be <code>null</code>
* @param legend
* Show the legend? If <code>null</code>, the ChartProvider should make his own decision.
*
* @return BufferedImage object if the chart is rendered correctly,
* otherwise null.
Expand All @@ -60,7 +65,7 @@ public interface ChartProvider {
* @throws IllegalArgumentException if an invalid argument is passed
*/
BufferedImage createChart(String service, String theme, Date startTime, Date endTime, int height, int width,
String items, String groups) throws ItemNotFoundException;
String items, String groups, Integer dpi, Boolean legend) throws ItemNotFoundException;

/**
* Gets the type of data that will be written by the chart.
Expand Down
Expand Up @@ -24,6 +24,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.BooleanUtils;
import org.eclipse.smarthome.core.items.ItemNotFoundException;
import org.eclipse.smarthome.ui.chart.ChartProvider;
import org.eclipse.smarthome.ui.items.ItemUIRegistry;
Expand All @@ -43,12 +44,15 @@
* <li>items: A comma separated list of item names to display</li>
* <li>groups: A comma separated list of group names, whose members should be displayed</li>
* <li>service: The persistence service name. If not supplied the first service found will be used.</li>
* <li>theme: The chart theme to use. If not supplied the chart provider uses a default theme.</li>
* <li>dpi: The DPI (dots per inch) value. If not supplied, a default is used.</code></li>
* <li>legend: Show the legend? If not supplied, the ChartProvider should make his own decision.</li>
* </ul>
*
* @author Chris Jackson
* @author Holger Reichert - Support for themes, DPI, legend hiding
*
*/

public class ChartServlet extends HttpServlet {

private static final long serialVersionUID = 7700873790924746422L;
Expand Down Expand Up @@ -249,11 +253,30 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
throw new ServletException("Could not get chart provider.");
}

// Read out the parameter 'dpi'
Integer dpi = null;
if (req.getParameter("dpi") != null) {
try {
dpi = Integer.valueOf(req.getParameter("dpi"));
} catch (NumberFormatException e) {
throw new ServletException("dpi parameter is invalid");
}
if (dpi <= 0) {
throw new ServletException("dpi parameter is <= 0");
}
}

// Read out parameter 'legend'
Boolean legend = null;
if (req.getParameter("legend") != null) {
legend = BooleanUtils.toBoolean(req.getParameter("legend"));
}

// Set the content type to that provided by the chart provider
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, req.getParameter("theme"), timeBegin, timeEnd,
height, width, req.getParameter("items"), req.getParameter("groups"), dpi, legend);
ImageIO.write(chart, provider.getChartType().toString(), res.getOutputStream());
} catch (ItemNotFoundException e) {
logger.debug("{}", e.getMessage());
Expand Down
@@ -0,0 +1,134 @@
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.ui.internal.chart.defaultchartprovider;

import java.awt.Color;
import java.awt.Font;

/**
* Chart styling theme for the {@link DefaultChartProvider}.
*
* @author Holger Reichert - Initial contribution
*
*/
public interface ChartTheme {

/**
* Theme name. Has to be unique across all themes.
*
* @return theme name
*/
public String getThemeName();

/**
* Background color, plot area.
*
* @return background color, plot area
*/
public Color getPlotBackgroundColor();

/**
* Color for the grid lines.
*
* @return color for the grid lines
*/
public Color getPlotGridLinesColor();

/**
* Return the width of the grid lines.
*
* @param dpi DPI dots per inch to calculate the width
* @return width of the grid lines
*/
public double getPlotGridLinesWidth(int dpi);

/**
* Return the dash spacing for the grid lines.
*
* @param dpi DPI dots per inch to calculate the width
* @return dash spacing for the grid lines
*/
public double getPlotGridLinesDash(int dpi);

/**
* Background color, legend area.
*
* @return background color, legend area
*/
public Color getLegendBackgroundColor();

/**
* Background color, whole chart
*
* @return background color, whole chart
*/
public Color getChartBackgroundColor();

/**
* Font color, legend and general use.
*
* @return
*/
public Color getChartFontColor();

/**
* Return a color for the given series number.
*
* @param series series number
* @return color for the given series numer
*/
public Color getLineColor(int series);

/**
* Return the width of the series lines.
*
* @param dpi DPI dots per inch to calculate the width
* @return width of the series lines
*/
public double getLineWidth(int dpi);

/**
* Color for the axis labels.
*
* @return
*/
public Color getAxisTickLabelsColor();

/**
* Font for the axis labels.
* The font size gets calculated with the dpi parameter.
*
* @param dpi the DPI to calculate the font size
* @return {@link Font} for the axis labels.
*/
public Font getAxisTickLabelsFont(int dpi);

/**
* Font for the legend text.
* The font size gets calculated with the dpi parameter.
*
* @param dpi the DPI to calculate the font size
* @return {@link Font} for the legend text
*/
public Font getLegendFont(int dpi);

/**
* Padding of the chart.
*
* @return padding of the chart
*/
public int getChartPadding();

/**
* Length of the line markers in the legend, in px.
*
* @return length of the line markers in the legend, in px
*/
public int getLegendSeriesLineLength();

}
@@ -0,0 +1,114 @@
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.ui.internal.chart.defaultchartprovider;

import java.awt.Color;
import java.awt.Font;

/**
* Implementation of the black {@link ChartTheme chart theme}.
*
* @author Holger Reichert - Initial contribution
*
*/
public class ChartThemeBlack implements ChartTheme {

private static final String THEME_NAME = "black";

private Color[] LINECOLORS = new Color[] { //
new Color(244, 67, 54), // red
new Color(76, 175, 80), // green
new Color(63, 81, 181), // blue
new Color(156, 39, 176), // magenta/purple
new Color(255, 152, 0), // orange
new Color(0, 188, 212), // cyan
new Color(233, 30, 99), // pink
Color.WHITE, // white
new Color(255, 235, 59) // yellow
};

private static final String FONT_NAME = "SansSerif";

@Override
public String getThemeName() {
return THEME_NAME;
}

@Override
public Color getPlotBackgroundColor() {
return new Color(15, 15, 26);
}

@Override
public Color getPlotGridLinesColor() {
return Color.WHITE.darker();
}

@Override
public double getPlotGridLinesWidth(int dpi) {
return Math.max(1.0, dpi / 64.0);
}

@Override
public double getPlotGridLinesDash(int dpi) {
return Math.max(3.0f, dpi / 32.0);
}

@Override
public Color getLegendBackgroundColor() {
return new Color(30, 30, 50, 160);
}

@Override
public Color getChartBackgroundColor() {
return new Color(0, 0, 0);
}

@Override
public Color getChartFontColor() {
return new Color(255, 255, 255, 255);
}

@Override
public Color getLineColor(int series) {
return LINECOLORS[series % LINECOLORS.length];
}

@Override
public double getLineWidth(int dpi) {
return Math.max(1.0, dpi / 64.0);
}

@Override
public Color getAxisTickLabelsColor() {
return getChartFontColor();
}

@Override
public Font getAxisTickLabelsFont(int dpi) {
int fontsize = (int) Math.max(8, Math.round(dpi / 8.5));
return new Font(FONT_NAME, Font.PLAIN, fontsize);
}

@Override
public Font getLegendFont(int dpi) {
int fontsize = (int) Math.max(8, Math.round(dpi / 9.6));
return new Font(FONT_NAME, Font.PLAIN, fontsize);
}

@Override
public int getChartPadding() {
return 5;
}

@Override
public int getLegendSeriesLineLength() {
return 10;
}

}