我只是试图在CombinedXYChart中运行一个与BarChart相结合的BubbleChart,但我总是得到一个例外。
这是我的代码:
public class LineGraph extends AbstractDemoChart {
private static final long HOUR = 3600 * 1000;
private static final long DAY = HOUR * 24;
private static final int HOURS = 24;
public Intent getIntent(Context context,double[] personCount, Date[] timeValue) {
XYValueSeries sunSeries = new XYValueSeries("Sunshine hours");
/*Create XYSeries and fill it with 24 int Values for X-Axis and PersonCount for Y-Axis*/
XYSeries series = new XYSeries("Test");
for (int i = 0; i < timeValue.length; i++) {
series.add(i, personCount[i]);
sunSeries.add(i, 15, 4.3);
}
XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
lightRenderer.setColor(Color.YELLOW);
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset =new XYMultipleSeriesDataset();
dataset.addSeries(series);
dataset.addSeries(0, sunSeries);
// Now I create the Series renderer
XYSeriesRenderer renderer =new XYSeriesRenderer();
renderer.setColor(Color.WHITE);
//last step, Creating the renderer that controls the full charts and add the single renderer for each series
XYMultipleSeriesRenderer mRenderer =new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(0,lightRenderer);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
// Use DateFormatter for having X Labels in the form of HH:mm
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
for (int i = 0; i < timeValue.length; i++) {
// add custom Labels with correct format
if(i%2==0){
mRenderer.addXTextLabel(i, formatter.format(timeValue[i]));
}
}
mRenderer.setYLabels(10);
mRenderer.setShowGrid(true);
mRenderer.setXLabelsAlign(Align.CENTER);
mRenderer.setYLabelsAlign(Align.RIGHT);
// disable the default labels
mRenderer.setXLabels(0);
mRenderer.setShowCustomTextGrid(true);
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPanLimits(new double[] { -10, 20, -10, 40 });
mRenderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.BLACK);
mRenderer.setChartTitle("Persons");
Intent intent = ChartFactory.getCombinedXYChartIntent(context, dataset, mRenderer, new String[]{BarChart.TYPE, BubbleChart.TYPE},
"Weather parameters");
return intent;
}
}
我得到的例外情况如下:
09-18 15:05:58.780: D/AndroidRuntime(14721): Shutting down VM
09-18 15:05:58.780: W/dalvikvm(14721): threadid=1: thread exiting with uncaught exception (group=0x41a55700)
09-18 15:05:58.790: E/AndroidRuntime(14721): FATAL EXCEPTION: main
09-18 15:05:58.790: E/AndroidRuntime(14721): java.lang.ClassCastException: org.achartengine.model.XYSeries cannot be cast to org.achartengine.model.XYValueSeries
09-18 15:05:58.790: E/AndroidRuntime(14721): at org.achartengine.chart.BubbleChart.drawSeries(BubbleChart.java:75)
09-18 15:05:58.790: E/AndroidRuntime(14721): at org.achartengine.chart.XYChart.drawSeries(XYChart.java:518)
09-18 15:05:58.790: E/AndroidRuntime(14721): at org.achartengine.chart.CombinedXYChart.drawSeries(CombinedXYChart.java:140)
09-18 15:05:58.790: E/AndroidRuntime(14721): at org.achartengine.chart.XYChart.draw(XYChart.java:311)
09-18 15:05:58.790: E/AndroidRuntime(14721): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:168)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.draw(View.java:14853)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13744)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13786)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.draw(View.java:14563)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13739)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13786)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.draw(View.java:14563)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13739)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13786)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.draw(View.java:14563)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.draw(View.java:14856)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.widget.FrameLayout.draw(FrameLayout.java:467)
09-18 15:05:58.790: E/AndroidRuntime(14721): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2621)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13744)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.View.getDisplayList(View.java:13786)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1411)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1359)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2672)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2538)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2154)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.Choreographer.doFrame(Choreographer.java:561)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.os.Handler.handleCallback(Handler.java:730)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.os.Looper.loop(Looper.java:176)
09-18 15:05:58.790: E/AndroidRuntime(14721): at android.app.ActivityThread.main(ActivityThread.java:5419)
09-18 15:05:58.790: E/AndroidRuntime(14721): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 15:05:58.790: E/AndroidRuntime(14721): at java.lang.reflect.Method.invoke(Method.java:525)
09-18 15:05:58.790: E/AndroidRuntime(14721): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
09-18 15:05:58.790: E/AndroidRuntime(14721): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
09-18 15:05:58.790: E/AndroidRuntime(14721): at dalvik.system.NativeStart.main(Native Method)
我不明白为什么总会抛出这种异常,
我试着像在AChartEngine的演示中那样做。
非常感谢您的帮助
答案 0 :(得分:2)
尝试更改
XYSeries series = new XYSeries("Test");
到
XYValueSeries series = new XYValueSeries("Test");
答案 1 :(得分:0)
@keshav,它运作正常,非常感谢! 重要的是我使用的是aChartEngine 1.1.0,但是这个版本没有XYCombinedChartDef。 我不得不下载aChartEngine V 1.2.0并且使用这个版本一切正常