开发者

GWT Visualization problem creating a graph with data from database

I'm experiencing problem creating a graph. The data for the graph is coming from database as a HashMap. Following is my code:

public void onModuleLoad()
{

    Runnable onLoadCallback = new Runnable() 
    {
        public void run() 
        {
            AbstractDataTable data = createLineTable();
            Options options = createLineOptions();
            LineChart line = new LineChart(data, options);
            vPanelWithGraph.add(line);
            vPanelWithGraph.add(closeGraphPopUp);
    };
    VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
    popUpGraph.add(vPanelWithGraph);
}

private LineChart.Options createLineOptions() 
{
    Options options = Options.create();
    options.setWidth(400);
    options.setHeight(240);
    options.setTitle("Graph Name");
    return options;
}

private AbstractDataTable createLineTable() 
{       
    DataTable data = DataTable.create();
    data.addColumn(ColumnType.STRING, "Date");
    data.addColumn(ColumnType.NUMBER, "Number");

    HashMap<String,String> map = getDateAndWarningCount();
    data.addRows(2);

    String [] arrDate = new String [2];
    int  [] arrWarCount = new int [2];
    int i=0;
    Iterator it = map.entrySet().iterator();

    while (it.hasNext()) 
    {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
        arrDate[i]=""+pairs.getKey();
        arrWarCount[i]=Integer.parseInt((String)pairs.getValue());
     开发者_C百科   i++;
    }
    data.setValue(0, 0, arrDate[0]);// row column
    data.setValue(0, 1, 5);

    data.setValue(1, 0, arrDate[1]);
    data.setValue(1, 1, 6);

    return data;
}

private HashMap<String,String> getDateAndWarningCount()
{
    HashMap<String,String> map = null;
    SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
    AsyncCallback<HashMap<String,String>>  callback = new AsyncCallback<HashMap<String,String>>()
    {
        @Override
        public void onFailure(Throwable caught) 
        {}

        @Override
        public void onSuccess(HashMap<String,String> result)
        {
            map  = (HashMap<String,String>)result;
        }
    };
    service.getHashMapValues(callback); 
    return map;
    }

}

The app is creating a pop-up with a graph. When the pop-up graph come up, it shows all 0. I printed out the values of the hashmap, the hashmap is getting populated with the correct data. Can't figure out what I'm doing wrong here. Any help will be appreciated. Thanks

Following is my edited code....it works...thanks everyone!

public DialogBox getPopUpWarningGraph()
    {               
        SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
        AsyncCallback<LinkedHashMap<String, Integer>> callback = new AsyncCallback<LinkedHashMap<String, Integer>>()
        {
            @Override
            public void onFailure(Throwable caught) 
            {
            }

            @Override
            public void onSuccess(LinkedHashMap<String, Integer> result)
            {
                map = new LinkedHashMap<String,Integer>(result);

                Runnable onLoadCallback = new Runnable() 
                {
                    public void run() 
                    {
                        AbstractDataTable data = createLineTable(map);
                        Options options = createLineOptions();
                        LineChart lineChart = new LineChart(data, options);
                        vPanelWithGraph.add(lineChart);
                        vPanelWithGraph.add(closeGraphPopUp);
                    }
                };
                VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);             
            }
        };
        service.getDateAndWarningCount("",callback);
        popUpGraph.add(vPanelWithGraph);
        return popUpGraph;
    }

    private LineChart.Options createLineOptions() 
    {
        Options options = Options.create();
        options.setWidth(400);
        options.setHeight(240);
        options.setTitle("");
        return options;
    }

    private AbstractDataTable createLineTable(HashMap<String,Integer> map ) 
    {
        DataTable data = DataTable.create();
        data.addColumn(ColumnType.STRING, "Task");
        data.addColumn(ColumnType.NUMBER, "");

        data.addRows(map.size());

        Iterator it = map.entrySet().iterator();
        int i=0;
        while (it.hasNext()) 
        {
            Map.Entry pairs = (Map.Entry)it.next();
            data.setValue(i,0,pairs.getKey()+"");
            data.setValue(i,1,Integer.parseInt(pairs.getValue()+""));
            i++;
        }
        return data;
    }
}

so basically, getPopUpWarningGraph() function can be called from any class and it will return a popup with a graph!


Your getDateAndWarningCount() returns immediately, before data is loaded - and it always returns null.

In GWT (and javascript) you have to program asynchronously - execute your code when system calls you.

In your case you have to execute drawing when onSuccess() is called, because this is when you have the data available.

public void onModuleLoad()
{

    Runnable onLoadCallback = new Runnable() 
    {
        public void run() {
            // first get the data
            getDateAndWarningCount()
        }          
    };
    VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
    popUpGraph.add(vPanelWithGraph);
}

private LineChart.Options createLineOptions() 
{
    Options options = Options.create();
    options.setWidth(400);
    options.setHeight(240);
    options.setTitle("Graph Name");
    return options;
}

private AbstractDataTable createLineTable(HashMap<String,String> map) 
{       
    DataTable data = DataTable.create();
    data.addColumn(ColumnType.STRING, "Date");
    data.addColumn(ColumnType.NUMBER, "Number");

    data.addRows(2);

    String [] arrDate = new String [2];
    int  [] arrWarCount = new int [2];
    int i=0;
    Iterator it = map.entrySet().iterator();

    while (it.hasNext()) 
    {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
        arrDate[i]=""+pairs.getKey();
        arrWarCount[i]=Integer.parseInt((String)pairs.getValue());
        i++;
    }
    data.setValue(0, 0, arrDate[0]);// row column
    data.setValue(0, 1, 5);

    data.setValue(1, 0, arrDate[1]);
    data.setValue(1, 1, 6);

    return data;
}

private HashMap<String,String> getDateAndWarningCount()
{
    HashMap<String,String> map = null;
    SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
    AsyncCallback<HashMap<String,String>>  callback = new AsyncCallback<HashMap<String,String>>()
    {
        @Override
        public void onFailure(Throwable caught) 
        {}

        @Override
        public void onSuccess(HashMap<String,String> result)
        {
                            Runnable onLoadCallback = new Runnable() 
            {
                public void run() 
                {
                    AbstractDataTable data = createLineTable(map);
                    Options options = createLineOptions();
                    LineChart lineChart = new LineChart(data, options);
                    vPanelWithGraph.add(lineChart);
                    vPanelWithGraph.add(closeGraphPopUp);
                }
            };
            VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE); 
        }
    };
    service.getHashMapValues(callback); 
    return map;
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜