Android - aChartEngine getCurrentSeriesAndPoint()

2019-05-26 04:09发布

问题:

I am plotting the wind direction as a xyseries in a TimeChartView using aChartEngine along with windspeed (windspeed is a timeseries using left y-axis, winddir is XYSeries using right y-axis).

The problem I am facing is that as the windirection passes north, my values shift between 0 and 360, creating an ugly vertical line in my graph. I would prefer that if the values increased above 360 the graph just got clipped and resumed at 0. By filtering the values and inserting MathHelper.NULL_VALUE in my XYSeries (see code) I managed to get the graph as I wanted

 double windDirTemp=0; 

...

    if (Math.abs(windDirValues[k] - windDirTemp)>300 ){
                windDirTemp = windDirValues[k];     
                windDirSeries.add(convertedDateinDouble[k], MathHelper.NULL_VALUE );                
            }
            else{
                windDirSeries.add(convertedDateinDouble[k], windDirValues[k]);
            }   

The problem now is that my OnClickListener doesn't work anymore. The app crashes on

SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();

I assume that inserting the null-values to my series makes getCurrentSeriesAndPoint unable to get the SeriesIndex and can have something to do with the graph no longer being connected.

I would greatly appreciate any help to solve this. Could I filter the values differently? Is there an alternative to getCurrentSeriesAndPoint() I could try?

Thanks, Christian

EDIT:

I am using achartengine-1.1.0-rc2

My OneClickListener:

/**
         * Setting a click event listener for the graph
         */
        mChart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Format formatter = new SimpleDateFormat("HH:mm");

                SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();                

                if (seriesSelection != null) {
                    int seriesIndex = seriesSelection.getSeriesIndex();
                    String selectedSeries = "Vindhastighet";
                    String unit = " m/s";

                    if (seriesIndex == 0)
                        selectedSeries = "Vindhastigheten";
                    else if (seriesIndex == 1)
                        selectedSeries = "Vindkast";
                    else {
                        selectedSeries = "Vindretningen";
                        unit = "\u00B0";
                    }

                    // Getting the clicked Date ( x value )
                    long clickedDateSeconds = (long) seriesSelection
                            .getXValue();
                    Date clickedDate = new Date(clickedDateSeconds);
                    String strDate = formatter.format(clickedDate);

                    // Getting the y value
                    int amount = (int) seriesSelection.getValue();

                    // Displaying Toast Message
                    Toast.makeText(
                            getBaseContext(),
                            selectedSeries + " kl " + strDate + " var "
                                    + amount + unit, Toast.LENGTH_LONG)
                            .show();
                }                   
            }
        });

My logcat:

04-02 16:05:18.575: E/AndroidRuntime(29182): FATAL EXCEPTION: main 04-02 16:05:18.575: E/AndroidRuntime(29182): java.lang.NullPointerException 04-02 16:05:18.575: E/AndroidRuntime(29182): at org.achartengine.chart.XYChart.getSeriesAndPointForScreenCoordinate(XYChart.java:845) 04-02 16:05:18.575: E/AndroidRuntime(29182): at org.achartengine.GraphicalView.getCurrentSeriesAndPoint(GraphicalView.java:137) 04-02 16:05:18.575: E/AndroidRuntime(29182): at com.example.hellogooglemaps.WindChartActivity$1.onClick(WindChartActivity.java:116) 04-02 16:05:18.575: E/AndroidRuntime(29182): at android.view.View.performClick(View.java:4211) 04-02 16:05:18.575: E/AndroidRuntime(29182): at android.view.View$PerformClick.run(View.java:17267) 04-02 16:05:18.575: E/AndroidRuntime(29182): at android.os.Handler.handleCallback(Handler.java:615) 04-02 16:05:18.575: E/AndroidRuntime(29182): at android.os.Handler.dispatchMessage(Handler.java:92) 04-02 16:05:18.575: E/AndroidRuntime(29182): at android.os.Looper.loop(Looper.java:137) 04-02 16:05:18.575: E/AndroidRuntime(29182): at android.app.ActivityThread.main(ActivityThread.java:4898) 04-02 16:05:18.575: E/AndroidRuntime(29182): at java.lang.reflect.Method.invokeNative(Native Method) 04-02 16:05:18.575: E/AndroidRuntime(29182): at java.lang.reflect.Method.invoke(Method.java:511) 04-02 16:05:18.575: E/AndroidRuntime(29182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 04-02 16:05:18.575: E/AndroidRuntime(29182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 04-02 16:05:18.575: E/AndroidRuntime(29182): at dalvik.system.NativeStart.main(Native Method)

回答1:

This was a bug in AChartEngine and is now fixed. You can download a version including this fix here.