sap
sap

Reputation: 1141

GWT - motionchart /date object

I'm having problem with creating a GWT MotionChart. I want the chart to look like following: http://code.google.com/apis/chart/interactive/docs/gallery/motionchart.html#notes

Following is my code:

package component.client.widgets;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.gwt.visualization.client.visualizations.MotionChart;
import com.google.gwt.visualization.client.visualizations.MotionChart.Options;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;

import component.client.SQLRunner;
import component.client.SQLRunnerAsync;

public class DashboardWidget 
{
    private HorizontalPanel containerPanel=null;
    private DataTable data=null;

    public DashboardWidget()
    {
        containerPanel = new HorizontalPanel();
    }
    public HorizontalPanel getContainerPanel()
    {
        SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
        AsyncCallback<ArrayList<String[]>> callback = new AsyncCallback<ArrayList<String[]>>()
        {
            @Override
            public void onFailure(Throwable caught) 
            {
            }

            @Override
            public void onSuccess(final ArrayList<String[]> result)
            {
                Runnable onLoadCallback = new Runnable() 
                {
                    public void run() 
                    {                           
                        String date1="1988,0,1";
                        String date2="1988,0,1";
                        String date3="1988,0,1";
                        String date4="1988,1,1";
                        String date5="1988,1,1";
                        String date6="1988,1,1";

                        DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy,M,d");
                        Date d1 = fmt.parse(date1);
                        Date d2 = fmt.parse(date2);
                        Date d3 = fmt.parse(date3);
                        Date d4 = fmt.parse(date4);
                        Date d5 = fmt.parse(date5);
                        Date d6 = fmt.parse(date6);


                        data.addRows(6);
                        data.addColumn(ColumnType.STRING,"Fruit");
                        data.addColumn(ColumnType.DATE, "Date");
                        data.addColumn(ColumnType.NUMBER, "Sales");
                        data.addColumn(ColumnType.NUMBER, "Expenses");
                        data.addColumn(ColumnType.STRING, "Location");
                        data.setValue(0, 0, "Apples");
                        data.setValue(0, 1, d1);
                        data.setValue(0, 2, 1000);
                        data.setValue(0, 3, 300);
                        data.setValue(0, 4, "East");
                        data.setValue(1, 0, "Oranges");
                        data.setValue(1, 1, d2);
                        data.setValue(1, 2, 950);
                        data.setValue(1, 3, 200);
                        data.setValue(1, 4, "West");
                        data.setValue(2, 0, "Bananas");
                        data.setValue(2, 1, d3);
                        data.setValue(2, 2, 300);
                        data.setValue(2, 3, 250);
                        data.setValue(2, 4, "West");

                        data.setValue(3, 0, "Apples");
                        data.setValue(3, 1, d4);
                        data.setValue(3, 2, 1200);
                        data.setValue(3, 3, 400);
                        data.setValue(3, 4, "East");
                        data.setValue(4, 0, "Oranges");
                        data.setValue(4, 1, d5);
                        data.setValue(4, 2, 900);
                        data.setValue(4, 3, 150);
                        data.setValue(4, 4, "West");
                        data.setValue(5, 0, "Bananas");
                        data.setValue(5, 1, d6);
                        data.setValue(5, 2, 788);
                        data.setValue(5, 3, 617);
                        data.setValue(5, 4, "West");

                        Options options = Options.create();
                        options.setWidth(1000);
                        options.setHeight(1000);

                        MotionChart chart = new MotionChart(data, options);
                        containerPanel.add(chart);
                    }
                };
                VisualizationUtils.loadVisualizationApi(onLoadCallback, MotionChart.PACKAGE);               
            }
        };
        service.getSomeStuffFromDB("","","","", callback);
        return containerPanel;
    }
}

However I'm getting error:

java.lang.IllegalArgumentException: 1988,0,1 at com.google.gwt.i18n.client.DateTimeFormat.parse(DateTimeFormat.java:1579) at com.google.gwt.i18n.client.DateTimeFormat.parse(DateTimeFormat.java:1049) at component.client.widgets.DashboardWidget$1$1.run(DashboardWidget.java:89) at com.google.gwt.ajaxloader.client.ExceptionHelper.runProtected(ExceptionHelper.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 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:167) at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:281) at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:531) at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352) at java.lang.Thread.run(Unknown Source)

I'm assuming it has to do with the date object. Can someone please point out what exactly I'm doing wrong?

Thanks so much in advance.

Upvotes: 0

Views: 380

Answers (1)

filip-fku
filip-fku

Reputation: 3265

The exception says it all: there is an unexpected argument passed to the DateTimeFormat.parse() method. You are passing dates like "1988,0,1" for a format of "yyyy,M,d" however according to the documentation for DateTimeFormat, the month code "M" spans the values 1-12.

In short, the month value in "1988,0,1" is in the wrong format. January is the lowest, and has a value of 1, not 0. Try changing the 0 to 1 for month.

Upvotes: 1

Related Questions