How can i display pie chart in jsp page? - java

<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%# page import="java.awt.*"%>
<%# page import="java.io.*"%>
<%# page import="org.jfree.chart.*"%>
<%# page import="org.jfree.chart.entity.*"%>
<%# page import="org.jfree.data.general.*"%>
<%
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));
JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart",pieDataset,true,true,false);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Pie Chart</title>
</head>
<body>
<IMG SRC="piechart.png" WIDTH="600" HEIGHT="400" BORDER="0"
USEMAP="#chart">
</body>
</html>
Output for this is a blank screen, It not thrown any exception..
How can i display pie chart in this page?
Thanks in advance.

after creating the chart save the chart as follow:
ChartUtilities.saveChartAsJPEG(new File(path/piechart.png"),chart,400, 300);
and then
use
<IMG SRC=path/"piechart.png" WIDTH="600" HEIGHT="400" BORDER="0"
USEMAP="#chart">
**Other way is as discussed in ** How to display line graph using JFreeChart in jsp?
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/png");
ServletOutputStream os = response.getOutputStream();
ImageIO.write(getChart(request), "png", os);
os.close();
}
private RenderedImage getChart(HttpServletRequest request) {
String chart = request.getParameter("chart");
// also you can process other parameters like width or height here
if (chart.equals("myDesiredChart1")) {
JFreeChart chart = [create your chart here];
return chart.createBufferedImage(width, height)
}
and display as
<img src="/ChartDrawerServlet?chart=myDesiredChart1&width=..and other processed parameters" ..>
see the answer of Martin Lazar here

Finally i got the answer....
.....In servlet....
public void getPieChart() {
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));
JFreeChart chart = ChartFactory.createPieChart("Discounts Used by Category ", data, true, true, false);
//chart.setBackgroundPaint(new Color(222, 222, 255));
final PiePlot plot = (PiePlot) chart.getPlot();
plot.setBackgroundPaint(Color.white);
plot.setCircular(true);
try {
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
final File file1 = new File(getServletContext().getRealPath(".") + "/images/charts/piechart.png");
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
} catch (Exception e) {
System.out.println(e);
}
}
.....in html page......
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Pie Chart</title>
</head>
<body>
<IMG SRC="piechart.png" WIDTH="600" HEIGHT="400" BORDER="0"
USEMAP="#chart">
</body>
</html>
............................................................................
......................Or using only jsp page........
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%# page import="java.awt.*"%>
<%# page import="java.io.*"%>
<%# page import="org.jfree.chart.*"%>
<%# page import="org.jfree.chart.entity.*"%>
<%# page import="org.jfree.data.general.*"%>
<%
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("JavaWorld", new Integer(75));
pieDataset.setValue("Other", new Integer(25));
JFreeChart chart = ChartFactory.createPieChart("Discounts Used by Category ", data, true, true, false);
//chart.setBackgroundPaint(new Color(222, 222, 255));
final PiePlot plot = (PiePlot) chart.getPlot();
plot.setBackgroundPaint(Color.white);
plot.setCircular(true);
try {
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
final File file1 = new File(getServletContext().getRealPath(".") + "/images/charts/piechart.png");
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
} catch (Exception e) {
System.out.println(e);
}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Pie Chart</title>
</head>
<body>
<IMG SRC="piechart.png" WIDTH="600" HEIGHT="400" BORDER="0"
USEMAP="#chart">
</body>
</html>

Related

WebView is not displaying correctly

The leftside is the html page loaded in Google Chrome and on the right side is the html page loaded in JavaFx WebView, The problem is that the navagation panel for Google Map is not displayed in the JavaFx WebView. Im using Java 7u79.
GoogleMap.java
webView = new WebView();
webEngine = webView.getEngine();
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>(){
#Override
public void changed(ObservableValue<? extends State> ov, State t, State t1) {
System.out.println("STATE : " + t1);
if(t1.equals(State.SUCCEEDED)){
root.getChildren().remove(indicator);
}
}
});
indicator = new ProgressIndicator();
indicator.setMaxSize(40, 40);
String htmlcode = getFileContent("GoogleMap.html");
webEngine.loadContent(htmlcode);
root = new StackPane();
root.setAlignment(Pos.CENTER);
root.getChildren().add(webView);
root.getChildren().add(indicator);
Scene scene = new Scene(root, 550, 400);
primaryStage.setTitle("Google Map");
primaryStage.setScene(scene);
primaryStage.show();
GoogleMap.html
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapProp = {
center : new google.maps.LatLng(51.508742,-0.120850),
zoom : 5,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
body
{
padding : 0;
margin : 0;
}
#googleMap
{
width : 550px;
height : 400px;
}
</style>
</head>
<body>
<div id="googleMap"></div>
</body>
</html>
I have all ok with it.
java 1.8.0_73 maybe you should try to update your java or get newer JavaFX?

Generated PDF from HTML, with itext (5.5.6), ignores font-size definition from CSS

I'm using itext 5.5.6 to generate PDF files from HTML documents built trough thymeleaf.
The html is displayed correctly in browsers.
However, when generating the PDF, it displays all the styles correctly, except the definition of "font-size".
public static void main(String[] args) throws IOException, DocumentException {
...
// itextpdf e itext tools
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, cvos);
document.open();
// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(cssFis);
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(bais);
document.close();
}
body{
font-family: Courier;
font-size: 8pt;
}
.tabela {
width: 90%;
border: 1px solid #000000;
}
.cabecalho {
background-color: #d3d3d3;
text-align: center;
font-weight: bolder;
}
.linha_numero {
text-align: center;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cadastros Veiculo</title>
<link rel="stylesheet" type="text/css" href="estilo.css" />
</head>
<body>
<table id="tabela" class="tabela">
<tr class="cabecalho">
<td rowspan="1" colspan="1">ID</td>
<td rowspan="1" colspan="1">PLACA</td>
<td rowspan="1" colspan="1">PROPRIETARIO</td>
<td rowspan="1" colspan="1">CPF</td>
<td rowspan="1" colspan="1">RENAVAM</td>
</tr>
<tr>
<td class="linha_numero" rowspan="1" colspan="1">0</td>
<td class="linha_numero" rowspan="1" colspan="1">X</td>
<td rowspan="1" colspan="1">X</td>
<td class="linha_numero" rowspan="1" colspan="1">0</td>
<td class="linha_numero" rowspan="1" colspan="1">0</td>
</tr>
</table>
</body>
</html>

Highcharts not displaying in jEditorPane

My Highchart is displaying fine in Chrome, however when I try to open it in Internet Explorer only the title and css border shows up. My main problem is that it doesn't show up when I'm trying to display it in a jEditorPane in a java application; it only shows the border. I can display simple HTML pages in the jEditorPane, but I'm guessing this has something to do with the javascript / jquery?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Chart Title</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<link href='http://fonts.googleapis.com/css?family=Oswald:400' rel='stylesheet' type='text/css'>
<script type="text/javascript">
$(function () {
Highcharts.setOptions({
colors: ['#1a1334','#26294a','#01545a','#017351','#03c383','#aad962','#fbbf45','#ef6a32','#ed0345','#a12a5e','#710162','#110141']
});
var chart;
$(document).ready(function () {
$('#container').highcharts({
chart: {
type: 'pie',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
backgroundColor: '#f5f5f5'
},
title: {
text: 'Chart Title',
style: {
fontSize: '35px',
fontWeight: 'bold',
fontFamily: 'Oswald'
}
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.y}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black',
fontSize: '15px'
}
},
showInLegend: true
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
y: 75,
backgroundColor: '#e4e4e4',
borderRadius: '5px',
itemStyle: {
fontSize: '15px',
fontWeight: 'light'
}
},
credits: {
enabled: false
},
series: [{
name: 'Chart Title',
innerSize: '40%',
data: [
['Work',11],
['Eat',2],
['Commute',2],
['Watch TV',2],
['Sleep',7],
['Other', 30],
['New', 45],
['Extra', 56], ]
}]
});
});
});
</script>
</head>
<body>
<div id="container" style="width: 945px; height: 525px; border-color: #333; border-style: solid; border-width: 10px 5px 5px 10px;"></div>
</body>
</html>
jEditorPane code:
jEditorPane1.setEditable(false);
try {
jEditorPane1.setPage(getClass().getResource("html/pie.html"));
} catch (IOException e) {
System.out.println(e);
}
Most of the solutions for Highcharts not displaying involved the ordering of the scripts, but I'm pretty sure that's not my problem.
Any ideas?
Try to use the WebView from JavaFX.
http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm
IIRC it is based on Webkit and should be able to render highcharts correctly

How to load jQuery dependent HTML with external JavaScript files to JavaFx WebView

How can I load JavaScript to a JavaFx Webview after $(document).ready(function() ie. after the webview loads and without waiting for events like onButtonClick action, for example?
The answer to this would answer my main question, How to add jQuery into a webview?.
I've been trying to make the following work but I've been unsuccessful at integrating other similar solutions online onto my problem.
The HTML file I'd like to add onto a webview is as follows:
<!DOCTYPE html>
<html>
<head>
<link href='../fullcalendar/fullcalendar.css' rel='stylesheet' />
<link href='../fullcalendar/fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='../lib/jquery.min.js'></script>
<script src='../lib/jquery-ui.custom.min.js'></script>
<script src='../fullcalendar/fullcalendar.min.js'></script>
<script>
$(document).ready(function() {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$('#calendar').fullCalendar({
editable: true,
events: [
{
title: 'All Day Event',
start: new Date(y, m, 1)
},
{
title: 'Long Event',
start: new Date(y, m, d-5),
end: new Date(y, m, d-2)
},
{
id: 999,
title: 'Repeating Event',
start: new Date(y, m, d-3, 16, 0),
allDay: false
},
{
id: 999,
title: 'Repeating Event',
start: new Date(y, m, d+4, 16, 0),
allDay: false
},
{
title: 'Meeting',
start: new Date(y, m, d, 10, 30),
allDay: false
},
{
title: 'Lunch',
start: new Date(y, m, d, 12, 0),
end: new Date(y, m, d, 14, 0),
allDay: false
},
{
title: 'Birthday Party',
start: new Date(y, m, d+1, 19, 0),
end: new Date(y, m, d+1, 22, 30),
allDay: false
},
{
title: 'Click for Google',
start: new Date(y, m, 28),
end: new Date(y, m, 29),
url: 'http://google.com/'
}
]
});
});
</script>
<style>
body {
margin-top: 40px;
text-align: center;
font-size: 14px;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
}
#calendar {
width: 900px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id='calendar'></div>
</body>
</html>
This is what I've been trying to do so far:
public class WebViewSample extends Application {
private Scene scene;
#Override
public void start(Stage stage) {
// create the scene
stage.setTitle("Web View");
scene = new Scene(new Browser(), 750, 500, Color.web("#666970"));
stage.setScene(scene);
scene.getStylesheets().add("WebViewSample/fullcalendar-1.6.4/fullcalendar/fullcalendar.css");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
public Browser() {
//apply the styles
getStyleClass().add("browser");
// load the web page
webEngine.load("WebViewSample/fullcalendar-1.6.4/demos/default.html");
//add the web view to the scene
getChildren().add(browser);
}
private Node createSpacer() {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}
#Override
protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
}
#Override
protected double computePrefWidth(double height) {
return 750;
}
#Override
protected double computePrefHeight(double width) {
return 500;
}
}
All I get from this is a blank white window, instead of something like the jQuery calendar plugin FullCalender being drawn on the window.
Thank you in advance.
Try using this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="layout" content="main">
<meta name="viewport" content="width=device-width"/>
<title>Calendar</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<link rel='stylesheet' href="fullcalendar.css" />
<script src="jquery.min.js"></script>
<script src="moment.min.js"></script>
<script src="fullcalendar.min.js"></script>
<script>
$(document).ready(function() {
// page is now ready, initialize the calendar...
$('#calendar').fullCalendar({
//events:"%EVENT_URL%",
allDaySlot: false,
header:
{
left: 'today prev next',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView:'agendaDay',
firstHour:'9',
minTime:'8:00',
weekends: true
});
});
</script>

jQuery timepicker addon not work with struts2-jquery

I'm working with Struts 2 and jQuery. The plugin of timepicker doesn't work.
My JSP:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>TestPicker</title>
<sj:head jqueryui="true" locale="es" jquerytheme="redmond" />
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
<style type="text/css">
.ui-timepicker-div .ui-widget-header { margin-bottom: 8px; }
.ui-timepicker-div dl { text-align: left; }
.ui-timepicker-div dl dt { float: left; clear:left; padding: 0 0 0 5px; }
.ui-timepicker-div dl dd { margin: 0 10px 10px 45%; }
.ui-timepicker-div td { font-size: 90%; }
.ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; }
.ui-timepicker-rtl{ direction: rtl; }
.ui-timepicker-rtl dl { text-align: right; padding: 0 5px 0 0; }
.ui-timepicker-rtl dl dt{ float: right; clear: right; }
.ui-timepicker-rtl dl dd { margin: 0 45% 10px 10px; }
</style>
<script type="text/javascript">
$(function(){
$('#txtTime').timepicker({});
});
</script>
</head>
<body>
<input id="txtTime" type="text" name="txtTime" value="" ></input>
</body>
</html>
It generates this HTML:
<script type="text/javascript" src="/Asistencia/struts/js/base/jquery-1.10.2.min.js">
</script>
<script type="text/javascript"
src="/Asistencia/struts/js/base/jquery.ui.core.min.js?s2j=3.6.1"></script>
<script type="text/javascript"
src="/Asistencia/struts/js/plugins/jquery.subscribe.min.js?s2j=3.6.1"></script>
<script type="text/javascript"
src="/Asistencia/struts/js/struts2/jquery.struts2.min.js?s2j=3.6.1"></script>
<script type="text/javascript">
$(function() {
jQuery.struts2_jquery.version="3.6.1";
jQuery.scriptPath = "/Asistencia/struts/";
jQuery.struts2_jquery.local = "es";
jQuery.struts2_jquery.gridLocal = "es";
jQuery.struts2_jquery.timeLocal = "es";
jQuery.ajaxSettings.traditional = true;
jQuery.ajaxSetup ({
cache: false
});
jQuery.struts2_jquery.require("js/struts2/jquery.ui.struts2.min.js?s2j=3.6.1");
});
</script>
<link id="jquery_theme_link" rel="stylesheet"
href="/Asistencia/struts/themes/redmond/jquery-ui.css?s2j=3.6.1"
type="text/css"/>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
I think this code ?s2j=3.6.1 gets the problem.
Page of TimePicker addon
Now, I will try with ama3+anytime plugin.
This parameter is not the problem, it is just for internal use and tells the plugin about version of plugin. The problem is in your jQuery code because you are trying to bind a timepicker on the element before this element is created. Try
$(document).ready(function(){
$('#txtTime').timepicker({
timeFormat: "hh:mm tt"
});
});

Categories

Resources