PDA

View Full Version : ChartListener for Stacked bar chart



kamisama
28 Jan 2010, 3:18 AM
Hi, I tried to add chart listener to the stacked bar chart as shown in following codes:



StackedBarChart barChart = new StackedBarChart();


List<StackedBarChart.StackValue> data = new
ArrayList<StackedBarChart.StackValue>();
data.add(new StackedBarChart.StackValue(100, "#AF1E2D", "Text 1"));
data.add(new StackedBarChart.StackValue(200, "#006B54", "Text 2"));
data.add(new StackedBarChart.StackValue(400, "#003F87", "Text 3"));
barChart.addStack(data);

data = new ArrayList<StackedBarChart.StackValue>();
data.add(new StackedBarChart.StackValue(200, "#AF1E2D", "Text 1"));
data.add(new StackedBarChart.StackValue(400, "#006B54", "Text 2"));
data.add(new StackedBarChart.StackValue(100, "#003F87", "Text 3"));
barChart.addStack(data);

barChart.addChartListener(new ChartListener() {
public void chartClick(ChartEvent ce) {
Info.display("Chart Clicked", "Yes");

}
});




And I got following exception:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.extjs.gxt.charts.client.model.charts.DataConfig

Can someone help me to figure out how chart listener works for stacked bar chart?

Arno.Nyhm
29 Jan 2010, 11:35 AM
and in which line in your example code the error was thrown?

kamisama
1 Feb 2010, 3:22 AM
barChart.addChartListener(new ChartListener() {
public void chartClick(ChartEvent ce) {
Info.display("Chart Clicked", "Yes");

}
});

This part is the exception occurs, I checked GXT source code and compared different
JSON data from the ChartModel class, I think there is a bug to handle the stacked
bar chart click event in GXT.

kamisama
3 Feb 2010, 7:24 AM
Anyone has a idea about this?

Arno.Nyhm
3 Feb 2010, 9:01 AM
can you post the full stacktrace of the error?

kamisama
3 Feb 2010, 12:38 PM
Here is the full sample code:



import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.event.ChartEvent;
import com.extjs.gxt.charts.client.event.ChartListener;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.ToolTip;
import com.extjs.gxt.charts.client.model.ToolTip.MouseStyle;
import com.extjs.gxt.charts.client.model.axis.Keys;
import com.extjs.gxt.charts.client.model.axis.XAxis;
import com.extjs.gxt.charts.client.model.axis.YAxis;
import com.extjs.gxt.charts.client.model.charts.StackedBarChart;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;

public class GXTChartSample implements EntryPoint {

public void onModuleLoad() {
RootPanel.get().add(new VerticalStackedChart());
}

class VerticalStackedChart extends ContentPanel {

public VerticalStackedChart() {
setBorders(false);
setBodyBorder(false);
setHeaderVisible(false);
setHeight(500);
setWidth(400);
}

@Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
XAxis xAxis = new XAxis();
xAxis.setMin(0);
xAxis.setMax(1);
xAxis.setSteps(1);

YAxis yAxis = new YAxis();
yAxis.setMin(0);
yAxis.setMax(100);
yAxis.setSteps(10);

StackedBarChart barChart = new StackedBarChart();
barChart.setTooltip("#key# - #val#");
Keys key1 = new Keys("Key 1", "#AF1E2D", 13);
Keys key2 = new Keys("Key 1", "#006B54", 13);
barChart.setKeys(key1,key2);
List<StackedBarChart.StackValue> data = new ArrayList<StackedBarChart.StackValue>();
data.add(new StackedBarChart.StackValue(10, "#AF1E2D"));
data.add(new StackedBarChart.StackValue(20, "#006B54"));
data.add(new StackedBarChart.StackValue(40, "#AF1E2D"));
barChart.addStack(data);

barChart.addChartListener(new ChartListener() {

@Override
public void chartClick(ChartEvent ce) {

Info.display("####3", "****");

}

});

ChartModel model = new ChartModel();
model.setXAxis(xAxis);
model.setYAxis(yAxis);
model.setTooltipStyle(new ToolTip(MouseStyle.NORMAL));
model.addChartConfig(barChart);

Chart chart = new Chart("resources/chart/open-flash-chart.swf");

chart.setChartModel(model);
add(chart);
}
}
}
Here is the full stacktrace error output:



[ERROR] [gxtchartsample] Unable to load module entry point class com.chartsample.client.GXTChartSample (see associated exception for details)
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.extjs.gxt.charts.client.model.charts.DataConfig
at com.extjs.gxt.charts.client.Chart.processModel(Chart.java:238)
at com.extjs.gxt.charts.client.Chart.setChartModel(Chart.java:168)
at com.chartsample.client.GXTChartSample$VerticalStackedChart.onRender(GXTChartSample.java:81)
at com.extjs.gxt.ui.client.widget.Component.render(Component.java:971)
at com.extjs.gxt.ui.client.widget.Component.onAttach(Component.java:1545)
at com.extjs.gxt.ui.client.widget.Container.onAttach(Container.java:471)
at com.google.gwt.user.client.ui.Widget.setParent(Widget.java:417)
at com.google.gwt.user.client.ui.Panel.adopt(Panel.java:119)
at com.google.gwt.user.client.ui.ComplexPanel.add(ComplexPanel.java:93)
at com.google.gwt.user.client.ui.AbsolutePanel.add(AbsolutePanel.java:75)
at com.chartsample.client.GXTChartSample.onModuleLoad(GXTChartSample.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:369)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:185)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:380)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
at java.lang.Thread.run(Thread.java:619)
This only happens while I add ChartListener to the stacked bar chart. The chart listener works for all type of charts except stacked one. I am wondering there is a trick for stacked bar chart listener. Anyone get the chart listener working on stacked bar chart?

Arno.Nyhm
4 Feb 2010, 7:55 AM
your example code works well to reproduce the error. i got the same error as you.



and: i get it working with this (maybe bad) hack.

and i dont know if it is the right direction and if there are some bad side effencts with this patch:


i replaced this line:


StackedBarChart barChart = new StackedBarChart();
with this code:



StackedBarChart barChart = new StackedBarChart() {

@Override
public List<DataConfig> getValues() {
List<ArrayList<StackValue>> stackValues = getStackValues();
ArrayList<DataConfig> newValues = new ArrayList<DataConfig>();

for (ArrayList<StackValue> arrayList : stackValues) {
for (StackValue stackValue : arrayList) {
newValues.add(stackValue);
}

}
return newValues;
}
};

kamisama
4 Feb 2010, 12:39 PM
Yep, it is working with chart listener now, thx.
So, is it bug or sth else?