All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.d3x.morpheus.viz.chart.ChartFactoryProxy Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*
 * 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.chart;

import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.d3x.morpheus.util.Collect;
import com.d3x.morpheus.viz.chart.pie.PiePlot;
import com.d3x.morpheus.viz.chart.xy.XyPlot;
import com.d3x.morpheus.viz.google.GChartFactory;
import com.d3x.morpheus.viz.jfree.JFChartFactory;
import com.d3x.morpheus.viz.js.JsCode;

/**
 * A ChartFactory that delegates all calls to some underlying factory that is implemented against a specific charting framework.
 *
 * @author Xavier Witdouck
 *
 * 

This is open source software released under the Apache 2.0 License

*/ public class ChartFactoryProxy implements ChartFactory { private ChartFactory defaultFactory = new JFChartFactory(); private ChartFactory swingFactory = new JFChartFactory(); private ChartFactory htmlFactory = new GChartFactory(); /** * Constructor */ ChartFactoryProxy() { super(); } /** * Switches this selector into HTML chart mode */ public void htmlMode() { this.defaultFactory = htmlFactory; } /** * Switches the selector into Swing chart mode */ public void swingMode() { this.defaultFactory = swingFactory; } /** * Returns the HTML based chart factory * @return the HTML based chart factory */ public ChartFactory asHtml() { return htmlFactory; } /** * Returns the Swing based chart factory * @return the Swing based chart factory */ public ChartFactory asSwing() { return swingFactory; } @Override public boolean isSupported(Chart chart) { return true; } @Override public String javascript(Chart... charts) { return this.javascript(Collect.asList(charts)); } @Override public String javascript(Iterable> charts) { if (isMixedCharts(charts)) { final List> chartList = Collect.asList(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) { final Iterator> iterator = charts.iterator(); if (iterator.hasNext()) { final Chart first = iterator.next(); if (htmlFactory.isSupported(first)) { htmlFactory.show(columns, charts); } else if (swingFactory.isSupported(first)) { swingFactory.show(columns, charts); } } } @Override public void show(int columns, Stream> charts) { final List> chartList = charts.collect(Collectors.toList()); final Iterator> iterator = chartList.iterator(); if (iterator.hasNext()) { final Chart first = iterator.next(); if (htmlFactory.isSupported(first)) { htmlFactory.show(columns, chartList); } else if (swingFactory.isSupported(first)) { swingFactory.show(columns, chartList); } } } @Override public Chart> ofXY(Class domainType, Consumer>> configurator) { return defaultFactory.ofXY(domainType, configurator); } @Override public Chart> ofPiePlot(boolean is3d, Consumer>> configurator) { return defaultFactory.ofPiePlot(is3d, configurator); } /** * Returns true if the charts are a mix of swing and html charts * @param charts the iterable of charts * @return true if mixed content */ private boolean isMixedCharts(Iterable> charts) { return containsHtmlCharts(charts) && containsSwingCharts(charts); } /** * Returns true if the charts include html charts * @param charts the iterable of charts * @return true if html charts */ private boolean containsHtmlCharts(Iterable> charts) { for (Chart chart : charts) { if (htmlFactory.isSupported(chart)) { return true; } } return false; } /** * Returns true if the charts include swing charts * @param charts the iterable of charts * @return true if swing charts */ private boolean containsSwingCharts(Iterable> charts) { for (Chart chart : charts) { if (swingFactory.isSupported(chart)) { return true; } } return false; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy