具有不同Y轴AChartEngine的CombinedChart中的LineChart和BarChart

时间:2014-09-23 14:46:22

标签: android achartengine

我尝试使用LineChart和带有两个不同Y轴的BarChart运行CombinedChart, 我查看了AChartEngine演示中的MultipleTemperatureChart示例,并以相同的方式进行了基本编程。
但在我的情况下,我有两个不同的图表(线和条)。

我总是得到同样的例外,不管我尝试什么。
是否可以在组合图表中设置不同的y轴? 这是我的代码:

    public Intent getIntent(Context context,double[] personCount, Date[] timeValue) {

        /*
        * Create a Series for Lightshine Values
        */
        XYValueSeries sunSeries = new XYValueSeries("Sunshine hours");
        for (int i = 1; i <= timeValue.length; i++) {
            if (i % 2 == 0) {
                sunSeries.add((double)i, 8, 4.3);
            }
        }

        /*
        * Create XYSeries for last 24(Hours) int Values for X-Axis and PersonCount for Y-Axis
        */
        XYSeries personSeries = new XYSeries("Person",0);
        for (int i = 0; i < timeValue.length; i++) {
            personSeries.add(i, personCount[i]);
        }

        /*
         * Create Series for Temperature with own y Axis 
         */
        XYSeries temperatureSeries= new XYSeries("Temperature",1);
        double[]temp =new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
                13.9, 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
                13.9 };

        for (int i = 0; i < timeValue.length; i++) {
            temperatureSeries.add(i, temp[i]);
        }

        // Creating a dataset to hold each series
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(personSeries);
        dataset.addSeries(sunSeries);
        dataset.addSeries(temperatureSeries);

        /* 
         * Create the Series renderer, for each series an Own renderer
         * The Order must be as Same as the different series that are created
         */     
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setColor(Color.RED);
        XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
        lightRenderer.setColor(Color.YELLOW);
        XYSeriesRenderer tempRenderer = new XYSeriesRenderer();
        tempRenderer.setColor(0xFFFF3300);

        // 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(lightRenderer);
        mRenderer.addSeriesRenderer(tempRenderer);
        renderer.setPointStyle(PointStyle.SQUARE);
        renderer.setFillPoints(true);
        lightRenderer.setPointStyle(PointStyle.SQUARE);
        lightRenderer.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.setBarSpacing(0.5);

        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setChartTitle("Persons");

//      // Set Options for Y-Axis on left site
        mRenderer.setYTitle("Person",0);                                        
        mRenderer.setYAxisAlign(Align.RIGHT, 0);
        mRenderer.setYLabelsAlign(Align.LEFT,0);
        mRenderer.setYLabelsColor(0, Color.RED);

        // Set Y-Axis Options for the right Site
        mRenderer.setYTitle("Grad",1);                                      
        mRenderer.setYAxisAlign(Align.RIGHT, 1);
        mRenderer.setYLabelsAlign(Align.LEFT,1);
        mRenderer.setYLabelsColor(1, 0xFFFF3300);


        XYCombinedChartDef[] types = { new XYCombinedChartDef(BarChart.TYPE, 0),
                new XYCombinedChartDef(BubbleChart.TYPE, 1), new XYCombinedChartDef(LineChart.TYPE, 2) };
        Intent intent = ChartFactory.getCombinedXYChartIntent(context, dataset, mRenderer, types,
                    "Weather parameters");



以下是抛出的异常:

09-23 16:20:44.715: E/AndroidRuntime(2975): java.lang.RuntimeException: An error occured while executing doInBackground()
09-23 16:20:44.715: E/AndroidRuntime(2975):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at java.lang.Thread.run(Thread.java:841)
09-23 16:20:44.715: E/AndroidRuntime(2975): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
09-23 16:20:44.715: E/AndroidRuntime(2975):     at org.achartengine.renderer.XYMultipleSeriesRenderer.setYTitle(XYMultipleSeriesRenderer.java:250)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at de.iRooms.Chart.LineGraph.getIntent(LineGraph.java:119)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at de.iRooms.Activities.DBTestsActivity$FillArrays.doInBackground(DBTestsActivity.java:149)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at de.iRooms.Activities.DBTestsActivity$FillArrays.doInBackground(DBTestsActivity.java:1)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-23 16:20:44.715: E/AndroidRuntime(2975):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-23 16:20:44.715: E/AndroidRuntime(2975):     ... 4 more
09-23 16:20:44.745: E/ErrPost(2975): java.lang.NullPointerException: println needs a message
09-23 16:20:53.750: I/Choreographer(2975): Skipped 531 frames!  The application may be doing too much work on its main thread.
09-23 16:20:54.290: E/WindowManager(2975): Activity de.iRooms.Activities.DBTestsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42707868 V.E..... R......D 0,0-684,192} that was originally added here
09-23 16:20:54.290: E/WindowManager(2975): android.view.WindowLeaked: Activity de.iRooms.Activities.DBTestsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42707868 V.E..... R......D 0,0-684,192} that was originally added here
09-23 16:20:54.290: E/WindowManager(2975):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
09-23 16:20:54.290: E/WindowManager(2975):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
09-23 16:20:54.290: E/WindowManager(2975):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
09-23 16:20:54.290: E/WindowManager(2975):  at android.app.Dialog.show(Dialog.java:287)
09-23 16:20:54.290: E/WindowManager(2975):  at de.iRooms.Activities.DBTestsActivity$FillArrays.onPreExecute(DBTestsActivity.java:82)
09-23 16:20:54.290: E/WindowManager(2975):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
09-23 16:20:54.290: E/WindowManager(2975):  at android.os.AsyncTask.execute(AsyncTask.java:534)
09-23 16:20:54.290: E/WindowManager(2975):  at de.iRooms.Activities.DBTestsActivity.sentRequest(DBTestsActivity.java:54)
09-23 16:20:54.290: E/WindowManager(2975):  at java.lang.reflect.Method.invokeNative(Native Method)
09-23 16:20:54.290: E/WindowManager(2975):  at java.lang.reflect.Method.invoke(Method.java:525)
09-23 16:20:54.290: E/WindowManager(2975):  at android.view.View$1.onClick(View.java:3833)
09-23 16:20:54.290: E/WindowManager(2975):  at android.view.View.performClick(View.java:4475)
09-23 16:20:54.290: E/WindowManager(2975):  at android.view.View$PerformClick.run(View.java:18786)
09-23 16:20:54.290: E/WindowManager(2975):  at android.os.Handler.handleCallback(Handler.java:730)
09-23 16:20:54.290: E/WindowManager(2975):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 16:20:54.290: E/WindowManager(2975):  at android.os.Looper.loop(Looper.java:176)
09-23 16:20:54.290: E/WindowManager(2975):  at android.app.ActivityThread.main(ActivityThread.java:5419)
09-23 16:20:54.290: E/WindowManager(2975):  at java.lang.reflect.Method.invokeNative(Native Method)
09-23 16:20:54.290: E/WindowManager(2975):  at java.lang.reflect.Method.invoke(Method.java:525)
09-23 16:20:54.290: E/WindowManager(2975):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
09-23 16:20:54.290: E/WindowManager(2975):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
我慢慢绝望,希望有人能帮助我 非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)


我已经弄明白了我的问题,并想向您介绍我的解决方案 首先回答问题是否可以使用具有不同Y轴AChartEngine的CombinedChart:
是的,即使在CombinedChart中也有可能有两个不同的Y轴!

如果你想要两个不同的Y轴,你必须做到以下几点:

    /*
    * Create XYSeries for PersonCount with own Y-Axis 
    */
    XYSeries personSeries = new XYSeries("Person",0);
    for (int i = 0; i < timeValue.length; i++) {
        personSeries.add(i, personCount[i]);
    }

    /*
     * Create Series for Temperature with own y Axis 
     */
    XYSeries temperatureSeries= new XYSeries("Temperature",1);
    double[]temp =new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
            13.9, 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
            13.9 };

    // Creating the renderer that controls the full charts 
    // IMPORTANT FOR 2 Y-AXIS -> new XYMultipleSeriesRenderer(2)
    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(2);
    mRenderer.addSeriesRenderer(renderer);
    mRenderer.addSeriesRenderer(lightRenderer);
    mRenderer.addSeriesRenderer(tempRenderer);

    // Set Options for Y-Axis on left site
    mRenderer.setYTitle("Personen");                                        
    mRenderer.setYAxisAlign(Align.LEFT,0);
    mRenderer.setYLabelsAlign(Align.LEFT,0);
    mRenderer.setYLabelsColor(0, Color.BLUE);

    // Set Y-Axis Options for the right Site
    mRenderer.setYTitle("Grad",1);                                      
    mRenderer.setYAxisAlign(Align.RIGHT, 1);
    mRenderer.setYLabelsAlign(Align.LEFT,1);
    mRenderer.setYLabelsColor(1, Color.YELLOW);



在我的案例中我有3个不同的图表(BarChart,BubbleChart和LineChart)
在这样做之后它工作正常,我希望它可以帮助你