halfhp / androidplot

Charts and plots for Android
http://androidplot.com
Apache License 2.0
505 stars 159 forks source link

Add new series and update the graph #97

Closed goodrickstar closed 4 years ago

goodrickstar commented 4 years ago

I've got more of a question than a problem. I'm creating an application with drop down spinners to select the time window and data set to be updated and displayed in a single XYplot view via a line chart.

I'll post the code but I'm basically failing to find in the API documentation the methods to update() or reset() the graph.

Collections.sort(statistics, (one, two) -> Integer.compare(Integer.parseInt(one.getId()), Integer.parseInt(two.getId())));
Number[] xVals = new Number[statistics.size()];
Number[] yVals = new Number[statistics.size()];
for (int i = 0; i < statistics.size(); i++) {
     Statistic statistic = statistics.get(i);
      xVals[i] = Integer.parseInt(statistic.getId());
      switch (dataMode) {
                case 0://Gross
                    yVals[i] = statistic.getGross();
                    break;
                case 1://Balance
                    yVals[i] = statistic.getBalance();
                    break;
                case 2://Miles
                    yVals[i] = statistic.getMiles();
                    break;
                case 3://Net CPM
                    yVals[i] = statistic.getNetCpm();
                    break;
                default://Fuel Price
                    yVals[i] = statistic.getFuelPrice();
                    break;
            }
        }
Logger.i("Xvals " + yVals.length, MainActivity.gson.toJson(xVals));
Logger.i("Yvals " + xVals.length, MainActivity.gson.toJson(yVals));
series = new SimpleXYSeries(Arrays.asList(xVals), Arrays.asList(yVals), "Series");
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
series1Format.setInterpolationParams(new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));
graphView.addSeries(series, series1Format);

An example of the x and y value arrays are as follows

Xvals 16 [9,11,22,31,35,47,54,62,82,94,101,107,115,131,137,152] Yvals 16 [8550,3700,6705,6000,5750,5600,5700,4400,5448,4350,8448,5250,4600,4750,4700,4975]

Using the spinners I'll run the same code above which results in alternative data

Xvals 16 [9,11,22,31,35,47,54,62,82,94,101,107,115,131,137,152] Yvals 16 [0.75,0.51,0.67,0.67,0.65,0.57,0.76,0.54,0.93,0.52,1.02,0.43,0.41,0.35,0.77,0.75]

My spinner OnItemSelectedListeners() are being triggered and the new data is logged and added to the graph. Ive tried both removeSeries() and clear() on the plot

@Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        graphView.removeSeries(series);
        graphView.clear();
        switch (adapterView.getId()) {
            case R.id.dataSet:
                dataMode = i;
                calculateAverages(false);
                break;
            case R.id.timeFrame:
                timeMode = i;
                calculateAverages(false);
                break;
        }
    }

What am I missing here? How to set the new series and update the graph?