How to create Pie Chart in GWT using gwt-visualization api? - gwt

I'm trying to build a pie-chart. But i get two different kinds of charts with different jars, gwt-charts & gwt-visualization.
. charts image http://gwt-google-apis.googlecode.com/svn/wiki/SimpleVizQuery-1.png
This is generated using gwt-visualization.jar. But it only displays the detail when I click on it and not when i hover on it. And it has a few more flaws.
.
This is generated using gwt-charts.jar and this is what i want.
Problem is that I need to use gwt-visualization.jar, but I want the look and feel of gwt-charts.jar..
Is there any way for that?????
This is the code example:
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.Selection;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.events.SelectHandler;
import com.google.gwt.visualization.client.visualizations.PieChart;
import com.google.gwt.visualization.client.visualizations.PieChart.Options;
public class SimpleViz implements EntryPoint {
public void onModuleLoad() {
// Create a callback to be called when the visualization API
// has been loaded.
Runnable onLoadCallback = new Runnable() {
public void run() {
Panel panel = RootPanel.get();
// Create a pie chart visualization.
PieChart pie = new PieChart(createTable(), createOptions());
panel.add(pie);
}
};
// Load the visualization api, passing the onLoadCallback to be called
// when loading is done.
VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);
}
private Options createOptions() {
Options options = Options.create();
options.setWidth(400);
options.setHeight(240);
options.set3D(true);
options.setTitle("My Daily Activities");
return options;
}
private AbstractDataTable createTable() {
DataTable data = DataTable.create();
data.addColumn(ColumnType.STRING, "Task");
data.addColumn(ColumnType.NUMBER, "Hours per Day");
data.addRows(2);
data.setValue(0, 0, "Work");
data.setValue(0, 1, 14);
data.setValue(1, 0, "Sleep");
data.setValue(1, 1, 10);
return data;
}
}

Both gwt-charts and gwt-visualization are simply GWT wrappers for the google chart javascript library.
gwt-visualization is the official library but it hasn't been updated for a long time.
gwt-chartsis a third party library and is more feature complete wrapper and has been updated to support the recent additions to the google chart javascript library.
But you can easily achieve the same thing with gwt-visualization using JSNI.
In the end it doesn't really matter which library you use. You should be able to achieve the same looks and behavior with both libraries.
One important thing is to load the CoreChart.PACKAGEinstead of the PieChart.PACKAGE and use the com.google.gwt.visualization.client.visualizations.corechart.PieChart instead of the com.google.gwt.visualization.client.visualizations.PieChart:
VisualizationUtils.loadVisualizationApi(onLoadCallback, CoreChart.PACKAGE);

Related

Issue in customizing default tooltip text of PieChart using gwt-visualization 1.1.2 API

I am having difficulty in customizing default tool tip text of PieChart.Here are the details of of API: gwt-visualization 1.1.2, Gwt2.0 and Gxt 2.1.1. Here is the link what i have followed , but there is no luck.
Code:
JSNI:
private native DataTable addTooltipColumn(DataTable data) /*-{
data.addColumn({type: 'string', role: 'tooltip'});
return data;
}-*/;
DataTable:
private AbstractDataTable createTable() {
DataTable data = DataTable.create();
data.addColumn(ColumnType.STRING, "Task");
data.addColumn(ColumnType.NUMBER, "Hours per Day");
//Assigning the variable
data = addTooltipColumn(data);
List<Integer> asList = Arrays.asList(34,12,34,32,67,21,2,45,2,4,28,5,78);
data.addRows(asList.size());
int i=0;
for (Integer integer : asList) {
data.setValue(i, 0, "Work"+i);
data.setValue(i, 1, integer);
data.setValue(i, 2, "....Tool Tip Txt...");
i++;
}
return data;
}
It seems that tooltips are currently not supported in Piecharts (see here for reference).
I tested it also in pure javascript and tools are not displayed in Piecharts:
http://jsfiddle.net/d9tezLL3/
If you change in the above example from PieChart to ColumnChart it will display the tooltips.
All you can do is create a static tooltip actions with PieChart.
In general I would recommend to use the inofficial gwt-charts instead of the official gwt-visualization library because the former is updated and supports recent features and charts out of the box (DataRoles, tooltips, etc)
I found this link https://gist.github.com/alexrainman/bb8d49357250df0859c0 to customize my tooltip and it worked out perfectly.
Hope this helps someone to save their time!!

Disabling toolbar from icePDF viewer

I am trying a sample with icePDF . Everything is working fine but i need to disable the toolbar which appears at the top. i tried few things but its not working. Can some body please help me out with it. Below is my code.
//package XML.test;
package applet;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.icepdf.ri.common.ComponentKeyBinding;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import org.icepdf.ri.util.PropertiesManager;
import org.icepdf.core.pobjects.fonts.*;
import org.icepdf.core.views.DocumentViewController;
import org.icepdf.core.*;
public class ViewerComponentExample
{
static void buildFrame(String filepath)
{
System.getProperties().put("org.icepdf.core.scaleImages", "false");
System.getProperties().put("org.icepdf.core.imageReference","smoothScaled");
System.getProperties().put("org.icepdf.core.target.dither", "VALUE_DITHER_DISABLE");
System.getProperties().put("org.icepdf.core.target.fractionalmetrics", "VALUE_FRACTIONALMETRICS_OFF");
System.getProperties().put("org.icepdf.core.target.interpolation", "VALUE_INTERPOLATION_NEAREST_ NEIGHBOR");
System.getProperties().put("org.icepdf.core.screen.interpolation", "VALUE_INTERPOLATION_NEAREST_NEIGHBOR");
System.getProperties().put("org.icepdf.core.awtFontLoading","true");
SwingController controller = new SwingController();
PropertiesManager properties = new PropertiesManager(System.getProperties(), ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
properties.setBoolean(PropertiesManager.PROPERTY_SHOW_TOOLBAR_ANNOTATION, Boolean.FALSE);
properties.setBoolean(PropertiesManager.PROPERTY_SHOW_TOOLBAR_FIT, Boolean.FALSE);
// Build a SwingViewFactory configured with the controller
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewerComponentPanel = factory.buildViewerPanel();
// add copy keyboard command
ComponentKeyBinding.install(controller, viewerComponentPanel);
// add interactive mouse link annotation support via callback
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
// Use the factory to build a JPanel that is pre-configured
//with a complete, active Viewer UI.
// Create a JFrame to display the panel in
JFrame window = new JFrame("Metrics Wizard Help");
window.getContentPane().add(viewerComponentPanel);
window.pack();
window.setVisible(true);
controller.setPageFitMode(DocumentViewController.PAGE_FIT_WINDOW_WIDTH, false);
controller.openDocument(filepath);
}
public static void main(String args[])
{
String filepath = "C:/Users/vishalt/Workspaces/Eclipse 4.2 Java/htmltopdf/src/XML/output/SCB_TEST.pdf";
buildFrame(filepath);
}
}
private SwingController controller;
controller = new SwingController();
SwingViewBuilder viewBuilder = new SwingViewBuilder(controller, properties);
JPanel panel = viewBuilder.buildViewerPanel();
controller.setToolBarVisible(false);
You have to set the toolbar invisible because icePdf looks in the PDF-document for the property and overwrites your setting with default when there is no document opened!
There are two ways to this.
1) Follow this example to set all the toolbars to false.
http://www.icesoft.org/JForum/posts/list/17673.page#sthash.48ICrL2A.dpbs
2) You can modify or remove the toolbar by editing the source code for SwingViewBuilder.
Here is a link to the code: http://sventon.icesoft.org/svn/repos/repo/show//icepdf/trunk/icepdf/viewer/src/org/icepdf/ri/common/SwingViewBuilder.java?revision=34004
You probably want to comment out lines 481 - 483.
481 JToolBar toolBar = buildCompleteToolBar(embeddableComponent);
482 if (toolBar != null)
483 cp.add(toolBar, BorderLayout.NORTH)
Remove your import for SwingViewBuilder and create your own class with those lines commented out.

Add JARs to NetBeans project (answer doesn't seem to work)

I'm trying to add JARs to a NetBeans project.
I've right-clicked on Libraries then selected Add JAR/Folder and selected the JARs, but the imports aren't recognised.
I know the imports are in those JARs because it works in Eclipse.
For example, the import statement
import org.jfree.chart.ChartFactory;
is underlined in red and has this error message:
cannot find symbol
symbol: class ChartFactory
location: package org.jfree.chart
----
(Alt-Enter shows hints)
EDIT:
I've been using NetBeans 7.3.1 and JDK 7 u 25 (64-bit).
A zip file containing the project I get the above error with is (hopefully) available here: https://app.promobucket.com/the-church/spine-panel/a-sample-collection-1/splinepanel
The code is here:
package splinepanel;
import java.awt.*; // Using AWT containers and components
import javax.swing.*; // Using Swing components and containers
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.ChartPanel;
/**
*
* #author User
*/
public class SplinePanel{
public JPanel createContentPane(){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
XYSeries series = new XYSeries("MyGraph");
series.add(0, 1);
series.add(1, 2);
series.add(2, 5);
series.add(7, 8);
series.add(9, 10);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(
"XY Chart",
"x-axis",
"y-axis",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
ChartPanel chartPanel = new ChartPanel(chart);
//chart.getXYPlot().setRenderer(new XYSplineRenderer());
panel.add(chartPanel);
panel.setOpaque(true);
return panel;
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");
//Create and set up the content pane.
SplinePanel demo = new SplinePanel();
frame.setContentPane(demo.createContentPane());
// The other bits and pieces that make our program a bit more stable.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300, 650);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
EDIT 2:
The project is now available here:
https://github.com/bluesh34/SplinePanel/
Using absolute paths to jars doesn't seem correct.
Proposal:
create a lib folder within your project
copy all the required jars to this lib folder
remove all wrong registered libs from project properties
re-add all the jars

How to customize style sheet of Apache wicket HeadersToolbar?

I've got problem with rendering table's header .it is not display any color on table.
I used HeadersToolbar with my data some thing like below.
import org.apache.wicket.Component;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.HeadersToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigationToolbar;
import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.OddEvenItem;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
public class CustomTable extends DataTable {
private static final long serialVersionUID = 5912216230302446976L;
public CustomTable(String id, List columns,
ISortableDataProvider dataProvider, int rowsPerPage) {
this(id, (IColumn[]) columns.toArray(new IColumn[columns.size()]),
dataProvider, rowsPerPage);
}
public CustomTable(String id, IColumn[] columns,
ISortableDataProvider dataProvider, int rowsPerPage) {
super(id, columns, dataProvider, rowsPerPage);
// super(id, columns, dataProvider, rowsPerPage);
addTopToolbar(new NavigationToolbar(this));
addTopToolbar(new HeadersToolbar(this, dataProvider));
}
protected Item newRowItem(String id, int index, IModel model) {
return new OddEvenItem(id, index, model);
}
}
I saw HeaderToolbar in details it will generate CSS class "headers" .And then i see in View Source . i have got css class in name "headers".
How to do customize HeaderToolbar to get correctly table row header display color or Can i create new one Css class instead ?
Any one can help me to solve this problem ? :)
I don't understand your goal. Do you want the color of the table header to change dynamically, or are you just trying to set the color of the header statically?
If setting the color statically, you can just use css th.header {background-color: #7FFF00;} for html: <th class="header">...</th>.
If you want it dynamically set I have had success with leaving the class out of the html & injecting it with wicket when condition is met:
if(...) {
header.add(new AttributeModifier("class", "header")`;
}
With more information I may be able to provide a more complete answer. Let me know if you have additional questions.

Using Google Visualization in GWT 2.0

I'm working on learning GWT (total newb) and have a question regarding the Visualiztion API provided by Google. This page:
http://code.google.com/p/gwt-google-apis/wiki/VisualizationGettingStarted
Describes getting started with a pie chart (which is what I need). However I'm trying to do this in a composite UI using UiBinder. To that end I don't know how to handle the callback correctly that is shown:
public class SimpleViz implements EntryPoint {
public void onModuleLoad() {
// Create a callback to be called when the visualization API
// has been loaded.
Runnable onLoadCallback = new Runnable() {
public void run() {
Panel panel = RootPanel.get();
// Create a pie chart visualization.
PieChart pie = new PieChart(createTable(), createOptions());
pie.addSelectHandler(createSelectHandler(pie));
panel.add(pie);
}
};
// Load the visualization api, passing the onLoadCallback to be called
// when loading is done.
VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);
}
My First assumption is this would go in the UiBinder constructor, correct? Yet this assumes that I want to place the element in the RootLayoutPanel, and I don't. I can't see an elegant and obvious way of placing it in the binder. I submit that even this guess may be wrong. Any ideas from the experts?
EDIT:
I should make clear my attempt:
public GraphPanel() {
initWidget(uiBinder.createAndBindUi(this));
Runnable onLoadCallback = new Runnable() {
public void run() {
//LayoutPanel panel = RootPanel.
// Create a pie chart visualization.
PieChart pie = new PieChart(createPieTable(), createPieOptions());
pie.addSelectHandler(createSelectHandler(pie));
mySelf.getElement().appendChild(pie.getElement()); // .add(pie);
}
};
// Load the visualization api, passing the onLoadCallback to be called
// when loading is done.
VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);
}
When run I get the following in the Composites DIV:
<div class="gwt-viz-container"></div>
But I see no graph using the code from the above page.
EDIT 2:
This link may provide additional information. However, the solution suggested is not optimal since the app then needs to know more about the widget (and if the widget is even there).
http://vaadin.com/forum/-/message_boards/message/97850
EDIT 3:
It shouldn't matter, but just in case, I'm running FF on Linux. Some articles I've read have implied that this is a problem.
EDIT 4:
Adding:
pie.draw(createPieTable(), createPieOptions());
after the append child gets the graph to display. This implies that the ordering of the example is wrong. If so what is the optimum?
It is also important to know that although the GWT JRE Emulation library supports the Runnable interface, it can't really be used for parallel processing in a separate thread, as the code is compiled into JavaScript which in turn runs single-threaded in the browser. Same goes for the synchronized keyword.
I also would recommend doing all preparation logic in the Widget/Composite's constructor but any actual drawing in the onLoad callback, which you need to override. This callback is called when the widget is loaded in the browser document, it is only then that you can perform any page/layout interaction, like enabling/disabling controls or requesting focus.
Either way you suggest would work. If the Visualization API is used by a bunch of different widgets on the page, then it might be simpler to put the loadVisualizationApi call in the EntryPoint class - an example of this is below.
You can write the Composite like so:
public MyPieChartContainer extends Composite {
interface MyUiBinder extends UiBinder<Widget, MyPieChartContainer>;
private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
#UiField Panel panel;
public MyPieChartContainer() {
initWidget(uiBinder.createAndBindUi(this));
PieChart pie = new PieChart(createTable(), createOptions());
pie.addSelectHandler(createSelectHandler(pie));
panel.add(pie);
}
}
And then do this in the EntryPoint:
public class SimpleViz implements EntryPoint {
public void onModuleLoad() {
// Create a callback to be called when the visualization API
// has been loaded.
Runnable onLoadCallback = new Runnable() {
public void run() {
Panel panel = RootPanel.get();
MyPieChartContainer myPieChartContainer = new MyPieChartContainer();
panel.add(myPieChartContainer);
}
};
// Load the visualization api, passing the onLoadCallback to be called
// when loading is done.
VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);
}