ro.nextreports.integration.ChartDemo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nextreports-engine Show documentation
Show all versions of nextreports-engine Show documentation
NextReports Engine is a lightweight Java platform development library which
can be used to run NextReports inside your applications.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 ro.nextreports.integration;
import winstone.Launcher;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.sql.Connection;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import ro.nextreports.engine.chart.Chart;
import ro.nextreports.engine.chart.ChartRunner;
/**
* User: mihai.panaitescu Date: 17-Feb-2010 Time: 14:58:20
*/
public class ChartDemo {
private final int serverPort = 9099;
// place where html, swf and json files are
private final String WEB_ROOT = "chart-webroot";
private Launcher server;
public static void main(String[] args) {
new ChartDemo().previewChartAsFlash();
//new ChartDemo().previewChartAsImage();
//new ChartDemo().previewChartAsData();
}
private ChartRunner createChartRunner(Chart chart , Connection connection, String format) {
ChartRunner runner = new ChartRunner();
runner.setChart(chart);
runner.setFormat(format);
runner.setConnection(connection);
runner.setQueryTimeout(60);
return runner;
}
public void previewChartAsFlash() {
startServer();
Connection connection = null;
OutputStream outputStream = null;
try {
Chart chart = DemoUtil.loadDemoChart();
connection = DemoUtil.createDemoConnection();
ChartRunner runner = createChartRunner(chart, connection, ChartRunner.GRAPHIC_FORMAT);
new File(WEB_ROOT).mkdir();
outputStream = new FileOutputStream(WEB_ROOT + File.separatorChar + "data.json");
boolean result = runner.run(outputStream);
outputStream.close();
if (result) {
String url = "http://localhost:" + serverPort + "/chart.html?ofc=data.json";
show("\r\n To preview the chart open in browser the following url:\r\n\r\n " + url, 400, 200);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DemoUtil.closeConnection(connection);
DemoUtil.closeStream(outputStream);
}
}
public void previewChartAsImage() {
Connection connection = null;
try {
Chart chart = DemoUtil.loadDemoChart();
connection = DemoUtil.createDemoConnection();
ChartRunner runner = createChartRunner(chart, connection, ChartRunner.IMAGE_FORMAT);
runner.setImageWidth(350);
runner.setImageHeight(200);
runner.run();
show(new ShowImagePanel(runner.getChartImageAbsolutePath()), 360,240);
} catch (Exception e) {
e.printStackTrace();
} finally {
DemoUtil.closeConnection(connection);
}
}
public void previewChartAsData() {
Connection connection = null;
try {
Chart chart = DemoUtil.loadDemoChart();
connection = DemoUtil.createDemoConnection();
ChartRunner runner = createChartRunner(chart, connection, ChartRunner.TABLE_FORMAT);
runner.run();
List tableHeader = runner.getTableData().getHeader();
for (String header : tableHeader) {
System.out.print(String.format("%-" + 30 + "s", header));
}
System.out.println();
System.out.println("----------------------------------------");
List> data = runner.getTableData().getData();
for (List