Created
September 30, 2013 06:29
-
-
Save vegaasen/6760016 to your computer and use it in GitHub Desktop.
ChartUtils - working with the google-chart-tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class ChartTools { | |
private static final String EMPTY = ""; | |
private static final int HEIGHT = 300, WIDTH = 800; | |
private static final AxisStyle axisStyle = AxisStyle.newAxisStyle(Color.WHITE, 12, AxisTextAlignment.CENTER); | |
private ChartTools() { | |
} | |
public static String getLineChart(Map<String, List<Double>> input, final String title, final int days) { | |
if (input == null || input.isEmpty()) { | |
return EMPTY; | |
} | |
Line[] lines = new Line[input.size()]; | |
int c = 0; | |
for (Map.Entry<String, List<Double>> entry : input.entrySet()) { | |
Line line = Plots.newLine(Data.newData(entry.getValue()), Color.newColor("000000"), entry.getKey().toLowerCase()); | |
line.setLineStyle(LineStyle.newLineStyle(3, 1, 0)); | |
line.addShapeMarkers(Shape.DIAMOND, Color.newColor("000000"), 12); | |
line.addShapeMarkers(Shape.DIAMOND, Color.WHITE, 8); | |
lines[c] = line; | |
c++; | |
} | |
return getDefaultLineChart(title, getValue(input, true), getValue(input, false), days, lines).toURLString(); | |
} | |
private static LineChart getDefaultLineChart( | |
final String title, | |
final double highestRange, | |
final double lowestRange, | |
final int numOfPoints, | |
final Line... lines) { | |
final LineChart chart = GCharts.newLineChart(lines); | |
chart.setSize(WIDTH, HEIGHT); | |
chart.setTitle(title); | |
chart.setGrid(25, 25, 3, 2); | |
chart.setBackgroundFill(Fills.newSolidFill(Color.newColor("1F1D1D"))); | |
final LinearGradientFill fill = Fills.newLinearGradientFill(0, Color.newColor("363433"), 100); | |
fill.addColorAndOffset(Color.newColor("2E2B2A"), 0); | |
chart.setAreaFill(fill); | |
for (AxisLabels label : getYAxisLabels(highestRange, lowestRange)) chart.addYAxisLabels(label); | |
for (AxisLabels label : getXAxisLabels(numOfPoints)) chart.addXAxisLabels(label); | |
return chart; | |
} | |
private static AxisLabels[] getYAxisLabels(double highestRange, final double lowestRange) { | |
StringBuilder ss = new StringBuilder(); | |
do { | |
ss.append((int) highestRange / 2); | |
highestRange = highestRange / 2; | |
if (highestRange > lowestRange) { | |
ss.append(","); | |
} | |
if (highestRange < 1.0) { | |
break; | |
} | |
} while (highestRange > lowestRange); | |
AxisLabels xAxis = AxisLabelsFactory.newAxisLabels(reverse(ss.toString().split(","))); | |
xAxis.setAxisStyle(axisStyle); | |
AxisLabels xAxis2 = AxisLabelsFactory.newAxisLabels("Time (ms)", 50.0); | |
xAxis2.setAxisStyle(AxisStyle.newAxisStyle(Color.WHITE, 14, AxisTextAlignment.CENTER)); | |
xAxis2.setAxisStyle(axisStyle); | |
return new AxisLabels[]{xAxis, xAxis2}; | |
} | |
private static AxisLabels[] getXAxisLabels(final int numOfPoints) { | |
StringBuilder ss = new StringBuilder(); | |
for (int i = 0; i < numOfPoints; i++) { | |
ss.append(i); | |
if ((i + 1) <= numOfPoints) { | |
ss.append(","); | |
} | |
} | |
AxisLabels yAxis = AxisLabelsFactory.newAxisLabels(ss.toString().split(",")); | |
yAxis.setAxisStyle(axisStyle); | |
AxisLabels yAxis2 = AxisLabelsFactory.newAxisLabels("Factor", 50.0); | |
yAxis2.setAxisStyle(AxisStyle.newAxisStyle(Color.WHITE, 14, AxisTextAlignment.CENTER)); | |
yAxis2.setAxisStyle(axisStyle); | |
return new AxisLabels[]{yAxis, yAxis2}; | |
} | |
private static double getValue(final Map<String, List<Double>> input, final boolean highest) { | |
double top = 0d; | |
for (Map.Entry<String, List<Double>> entry : input.entrySet()) { | |
for (Double d : entry.getValue()) { | |
if (highest) { | |
if (d > top) { | |
top = d; | |
} | |
} else { | |
if (d < top) { | |
top = d; | |
} | |
} | |
} | |
} | |
return top; | |
} | |
public static String[] reverse(String[] arr) { | |
List<String> list = Arrays.asList(arr); | |
Collections.reverse(list); | |
return list.toArray(new String[list.size()]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment