-5

I am developing application on GWT . i want to generate pie charts on gwt composite. i am using
Google's Chart Tools API Library for GWT for it . i have also imported gwt visualization jar in the project. i also have inherited it in gwt.xml.

i am using this code for generating charts

class SimpleViz extends Composite {

VerticalPanel vPanel = new VerticalPanel();

public SimpleViz() {
    // Create a callback to be called when the visualization API
    // has been loaded.


    final PieChart pie = new PieChart(createTable(), createOptions());

    Runnable onLoadCallback = new Runnable() {
        public void run() {
            // Panel panel = RootPanel.get();

            // Create a pie chart visualization.

            pie.addSelectHandler(createSelectHandler(pie));
            // panel.add(pie);

        }
    };


    VisualizationUtils.loadVisualizationApi(onLoadCallback,
            PieChart.PACKAGE);

    vPanel.add(pie);
    initWidget(vPanel);
}

private Options createOptions() {
    Options options = Options.create();
    options.setWidth(400);
    options.setHeight(240);
    options.set3D(true);
    options.setTitle("My Daily Activities");
    return options;
}

private SelectHandler createSelectHandler(final PieChart chart) {
    return new SelectHandler() {
        @Override
        public void onSelect(SelectEvent event) {
            String message = "";

            // May be multiple selections.
            JsArray<Selection> selections = chart.getSelections();

            for (int i = 0; i < selections.length(); i++) {
                // add a new line for each selection
                message += i == 0 ? "" : "\n";

                Selection selection = selections.get(i);

                if (selection.isCell()) {
                    // isCell() returns true if a cell has been selected.

                    // getRow() returns the row number of the selected cell.
                    int row = selection.getRow();
                    // getColumn() returns the column number of the selected
                    // cell.
                    int column = selection.getColumn();
                    message += "cell " + row + ":" + column + " selected";
                } else if (selection.isRow()) {
                    // isRow() returns true if an entire row has been
                    // selected.

                    // getRow() returns the row number of the selected row.
                    int row = selection.getRow();
                    message += "row " + row + " selected";
                } else {
                    // unreachable
                    message += "Pie chart selections should be either row selections or cell selections.";
                    message += "  Other visualizations support column selections as well.";
                }
            }

            Window.alert(message);
        }
    };
}

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;
}

}

this is gwt.xml code :

<?xml version="1.0" encoding="UTF-8"?>
  <module rename-to='tbrsmssurveygwt'>
<inherits name='com.google.gwt.user.User'/>
   <inherits name='com.google.gwt.visualization.Visualization'/>
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
  <inherits name="com.googlecode.gwt.charts.Charts"/> 

following are the exceptions :

15:22:49.039 [ERROR] [tbrsmssurveygwt] Uncaught exception escaped

com.google.gwt.core.client.JavaScriptException: (TypeError) @com.google.gwt.core.client.impl.Impl::apply(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)([JavaScript object(290), JavaScript object(289), JavaScript object(296)]): Cannot read property 'visualization' of undefined at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249) at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576) at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284) at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91) at com.google.gwt.core.client.impl.Impl.apply(Impl.java) at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:347) at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293) at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547) at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364) at java.lang.Thread.run(Thread.java:722)

1 Answers1

1

It seems that the visualization API needs to be explicitly loaded in GWT for it to work.

// Load the visualization api, passing the onLoadCallback to be called
// when loading is done.
VisualizationUtils.loadVisualizationApi(onLoadCallback, OrgChart.PACKAGE);

or

VisualizationUtils.loadVisualizationApi(onLoadCallback);

if the first does not work.

This question may be then a duplicate of this: GWT with Charts API example from Google not working

Also, check this, same question on google groups: https://groups.google.com/forum/#!topic/google-web-toolkit/yyxzWBUdBrk

Does the example application of the above link work in your case? Could you try the proposed solution on these links and let us know?

Community
  • 1
  • 1
Diego
  • 462
  • 10
  • 26
  • i already have checked these links and yes the example application works fine but when i try to generate charts on composite it gives above exceptions . the example application is running on entry point . – Muhammad Ahmed Sep 03 '14 at 03:50
  • OK, if you are sure that the APIs are loaded before trying to draw anything, may I ask if you have a labelled legend feature? If you do try turning off that feature. – Diego Sep 03 '14 at 15:01