Usuário:SandroHcBot/Código Fonte/BarGraph
Classa para gerar gráficos de barras em html.
/*-------------------------------------------------------- * AmauriceBot - RuneScape Wikia update task robot * Copyright (c) 2009-2012 Maurice Abraham. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Contributors: * None *------------------------------------------------------*/ package amauricebot; import java.util.ArrayList; @Description(summary = "Classa para gerar gráficos de barras em html.") public class BarGraph { private ArrayList<Long> m_values = new ArrayList<Long>(); private long m_progressGoal = 0; // 0 = no particular goal private String m_barColor = "blue"; private String m_aboveColor = "green"; private String m_belowColor = "red"; private String m_minXLabel = ""; private String m_maxXLabel = ""; private final static int Y_MARKERS = 50; public BarGraph() { } public void addValue(long value) { m_values.add(value); } public void setProgressGoal(long goal) { m_progressGoal = goal; } public void setBarColor(String color) { m_barColor = color; } public void setAboveColor(String color) { m_aboveColor = color; } public void setBelowColor(String color) { m_belowColor = color; } public void setMinXLabel(String label) { m_minXLabel = label; } public void setMaxXLabel(String label) { m_maxXLabel = label; } public int getSize() { return m_values.size(); } public long getValue(int i) { return m_values.get(i); } public String getAsHtml() { long startValue = (m_values.isEmpty() ? 0 : m_values.get(0)); long minValue = startValue; long maxValue = startValue; long progressValue = startValue; for (Long value : m_values) { minValue = Math.min(minValue, Math.min(progressValue, value)); maxValue = Math.max(maxValue, Math.max(progressValue, value)); progressValue += m_progressGoal; } long ymax = maxValue - minValue; long scale = (ymax / 500) + 1; ymax /= scale; // create chart StringBuilder builder = new StringBuilder(); builder.append( "<div id='chart' style='position:relative;left:0px;top:0px;\n") .append(" width:562px;height:").append(ymax + 60) .append("px'>\n"); // add X labels builder.append("<div style='position:absolute;left:30px;top:") .append(ymax + 30).append("px'><small>").append(m_minXLabel) .append("</small></div>\n"); builder.append("<div style='position:absolute;right:30px;top:") .append(ymax + 30).append("px'><small>").append(m_maxXLabel) .append("</small></div>\n"); // add marker labels for (int y = 0; y < ymax + 20; y += Y_MARKERS) { builder.append("<div style='position:absolute;left:0px;top:") .append(ymax + 18 - y).append("px'><small>") .append(y * scale + minValue).append("</small></div>\n"); } // add chart area builder.append( "<div id='chartarea' style='position:absolute;left:30px;top:10px;\n") .append(" border:1px solid gray;background-color:white;width:502px;height:") .append(ymax + 20).append("px'>\n"); // add marker lines for (int y = Y_MARKERS; y < ymax + 20; y += Y_MARKERS) { builder.append("<div style='position:absolute;left:0px;bottom:") .append(y).append("px;width:502px;height:1px;") .append(" background-color:lightgray;'></div>\n"); } // add actual graph bars for (int colNum = 0; colNum < m_values.size(); colNum++) { long x = colNum * 10 + 2; long y1 = (startValue + (colNum * m_progressGoal) - minValue) / scale; String color1 = m_barColor; long y2 = (m_values.get(colNum) - minValue) / scale - y1; String color2 = m_aboveColor; if (y2 < 0) { y1 += y2; y2 = -y2; color2 = m_belowColor; } if (y1 > 0) { builder.append("<div style='position:absolute;left:").append(x) .append("px;bottom:0px;width:8px;height:").append(y1) .append("px; background-color:").append(color1) .append(";'></div>\n"); } if (y2 > 0) { builder.append("<div style='position:absolute;left:").append(x) .append("px;bottom:").append(y1) .append("px;width:8px;height:").append(y2) .append("px; background-color:").append(color2) .append(";'></div>\n"); } } builder.append("</div>\n"); // end chart area builder.append("</div>\n"); // end chart return builder.toString(); } }