home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (TestRendering.java)

This example Java source code file (TestRendering.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

accordion, also, awt, chart, component, componentdiv, default, exception, ignore, objectmapper, scatter, stringbuilder, stylechart, util

The TestRendering.java Java example source code

 *  * Copyright 2016 Skymind,Inc.
 *  *
 *  *    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 org.deeplearning4j.ui;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.deeplearning4j.ui.api.*;
import org.deeplearning4j.ui.api.Component;
import org.deeplearning4j.ui.components.chart.ChartHistogram;
import org.deeplearning4j.ui.components.chart.ChartLine;
import org.deeplearning4j.ui.components.chart.ChartScatter;
import org.deeplearning4j.ui.components.chart.ChartStackedArea;
import org.deeplearning4j.ui.components.chart.style.StyleChart;
import org.deeplearning4j.ui.components.component.ComponentDiv;
import org.deeplearning4j.ui.components.component.style.StyleDiv;
import org.deeplearning4j.ui.components.decorator.DecoratorAccordion;
import org.deeplearning4j.ui.components.decorator.style.StyleAccordion;
import org.deeplearning4j.ui.components.table.ComponentTable;
import org.deeplearning4j.ui.components.table.style.StyleTable;
import org.deeplearning4j.ui.components.text.ComponentText;
import org.deeplearning4j.ui.components.text.style.StyleText;
import org.junit.Ignore;
import org.junit.Test;

import java.awt.Color;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

 * This test: generated a HTML file that you can open to view some example graphs.
 * The generated HTML file should appear in the deeplearning4j-ui-components directory (TestRendering.html)
 * *** NOTE: Open this in IntelliJ: Right click on file -> Open In Browser ***
public class TestRendering {

    public void test() throws Exception {

        List<Component> list = new ArrayList<>();

        //Common style for all of the charts
        StyleChart s = new StyleChart.Builder()
                .width(640, LengthUnit.Px)
                .height(480, LengthUnit.Px)
                .margin(LengthUnit.Px, 100,40,40,20)
                .seriesColors(Color.GREEN, Color.MAGENTA)
                .titleStyle(new StyleText.Builder().font("courier").fontSize(16).underline(true).color(Color.GRAY).build())

        //Line chart with vertical grid
        Component c1 = new ChartLine.Builder("Line Chart!",s)
                .addSeries("series0", new double[]{0,1,2,3}, new double[]{0,2,1,4})
                .addSeries("series1", new double[]{0,1,2,3}, new double[]{0,1,0.5,2.5})
                .setGridWidth(1.0,null) //Vertical grid lines, no horizontal grid

        //Scatter chart
        Component c2 = new ChartScatter.Builder("Scatter!",s)
                .addSeries("series0", new double[]{0,1,2,3}, new double[]{0,2,1,4})

        //Histogram with variable sized bins
        Component c3 = new ChartHistogram.Builder("Histogram!",s)

        //Stacked area chart
        Component c4 = new ChartStackedArea.Builder("Area Chart!",s)
                .setXValues(new double[]{0,1,2,3,4,5})
                .addSeries("series0",new double[]{0,1,0,2,0,1})
                .addSeries("series1",new double[]{2,1,2,0.5,2,1})

        StyleTable ts = new StyleTable.Builder()
                .columnWidths(LengthUnit.Percent, 20,40,40)
                .width(500, LengthUnit.Px)
                .height(200, LengthUnit.Px)

        Component c5 = new ComponentTable.Builder(ts)
                .content(new String[][]{
                        {"row0col0", "row0col1", "row0col2"},
                        {"row1col0", "row1col1", "row1col2"}

        //Accordion decorator, with the same chart
        StyleAccordion ac = new StyleAccordion.Builder()

        Component c6 = new DecoratorAccordion.Builder(ac)
                .title("Accordion - Collapsed By Default!")

        //Text with styling
        Component c7 = new ComponentText.Builder("Here's some blue text in a green div!",
                new StyleText.Builder().font("courier").fontSize(30).underline(true).color(Color.BLUE).build()).build();

        //Div, with a chart inside
        Style divStyle = new StyleDiv.Builder()
        Component c8 = new ComponentDiv(divStyle, c7, new ComponentText("(Also: it's float right, 30% width, 200 px high )",null));

        //Generate HTML
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title\n" +
                "</head>\n" +
                "<body>\n" +
                "\n" +
                "    <div id=\"maindiv\">\n" +
                "\n" +
                "    </div>\n" +
                "\n" +
                "\n" +
                "    <script src=\"//code.jquery.com/jquery-1.10.2.js\">\n" +
                "    <script src=\"https://code.jquery.com/ui/1.11.4/jquery-ui.min.js\">\n" +
                "    <link rel=\"stylesheet\" href=\"//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css\">\n" +
                "    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js\">\n" +
                "    <script src=\"src/main/resources/assets/dl4j-ui.js\">\n" +
                "\n" +
                "    <script>\n");

        ObjectMapper om = new ObjectMapper();
        for(int i=0; i<list.size(); i++ ){
            Component component = list.get(i);
            sb.append("        ").append("var str").append(i).append(" = '")

            sb.append("        ").append("var obj").append(i).append(" = Component.getComponent(str").append(i).append(");\n");
            sb.append("        ").append("obj").append(i).append(".render($('#maindiv'));\n");

        sb.append("    </script>\n" +
                "\n" +
                "</body>\n" +

        FileUtils.writeStringToFile(new File("TestRendering.html"),sb.toString());


Other Java examples (source code examples)

Here is a short list of links related to this Java TestRendering.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.