com.d3x.morpheus.viz.google.GChartFactory Maven / Gradle / Ivy
/*
* Copyright (C) 2014-2018 D3X Systems - All Rights Reserved
*
* 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.d3x.morpheus.viz.google;
import java.awt.*;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Stream;
import com.d3x.morpheus.util.Collect;
import com.d3x.morpheus.viz.chart.Chart;
import com.d3x.morpheus.viz.chart.ChartFactory;
import com.d3x.morpheus.viz.chart.pie.PiePlot;
import com.d3x.morpheus.viz.chart.xy.XyPlot;
import com.d3x.morpheus.viz.html.HtmlCode;
import com.d3x.morpheus.viz.js.JsCode;
/**
* A ChartEngine implementation used to create Google chart instances based on the Morpheus charting API.
*
* @link https://developers.google.com/chart/
*
* @author Xavier Witdouck
*
* This is open source software released under the Apache 2.0 License
*/
public class GChartFactory implements ChartFactory {
/**
* Constructor
*/
public GChartFactory() {
super();
}
@Override
public boolean isSupported(Chart> chart) {
return chart instanceof GChart;
}
@Override
public String javascript(Iterable> charts) {
return javascript(Collect.asStream(charts).toArray(Chart[]::new));
}
@Override
public String javascript(Chart... charts) {
return JsCode.create(jsCode -> {
jsCode.newLine().write("google.charts.load('current', {'packages':['corechart']});");
jsCode.newLine().write("google.charts.setOnLoadCallback(%s);", "drawCharts");
jsCode.newLine();
jsCode.newFunction("drawCharts", init -> {
for (int i=0; i> charts) {
show(columns, Collect.asIterable(charts));
}
@Override
public void show(int columns, Iterable> charts) {
try {
final HtmlCode htmlCode = new HtmlCode();
final AtomicInteger chartIndex = new AtomicInteger(-1);
htmlCode.newElement("html", html -> {
html.newElement("head", head -> {
head.newElement("script", script -> {
script.newAttribute("type", "text/javascript");
script.newAttribute("src", "https://www.gstatic.com/charts/loader.js");
});
head.newElement("script", script -> {
script.newAttribute("type", "text/javascript");
script.text(javascript(charts));
});
});
final String width = String.valueOf((int)(100d / columns)) + "%";
final String height = String.valueOf((int)(100d / columns * 0.9d)) + "%";
html.newElement("body", body -> {
chartIndex.set(-1);
charts.forEach(chart -> {
body.newElement("div", div -> {
div.newAttribute("id", String.format("chart_%s", chartIndex.incrementAndGet()));
div.newAttribute("style", String.format("width:%s;height:%s;float:left;", width, height));
});
});
});
});
final File dir = new File(System.getProperty("user.home"), ".morpheus/charts");
final File file = new File(dir, UUID.randomUUID().toString() + ".html");
if (file.getParentFile().mkdirs()) System.out.println("Created directory: " + dir.getAbsolutePath());
System.out.println(htmlCode.toString());
htmlCode.flush(file);
Desktop.getDesktop().browse(file.toURI());
} catch (Exception ex) {
throw new RuntimeException("Failed to generate Google chart", ex);
}
}
@Override
public Chart> ofXY(Class domainType, Consumer>> configurator) {
final Chart> chart = new GChart<>(new GXyPlot<>());
if (configurator != null) {
configurator.accept(chart);
}
return chart;
}
@Override
public Chart> ofPiePlot(boolean is3d, Consumer>> configurator) {
final Chart> chart = new GChart<>(new GPiePlot<>(is3d));
if (configurator != null) {
configurator.accept(chart);
}
return chart;
}
}