0

I require approx. 20 keys from a data provided that contains 100+ keys.

Set<Entry<Double,PosTvector>> set = realPosDataMap.entrySet();

Iterator<Entry<Double, PosTvector>> i1 = set.iterator();

Map.Entry<Double,PosTvector> map;

while(i1.hasNext()) {
    map = i1.next();

    Double xvalue = map.getKey();
    PosTvector yvalue = map.getValue();
    PosTvector yvalue1 = map.getValue();
    PosTvector yvalue2 = map.getValue();                       
    Data<String, Number> data = new Data<String, Number>(xvalue.toString(), yvalue.state[0]);
    Data<String, Number> data1 = new Data<String, Number>(xvalue.toString(), yvalue1.state[1]);
    Data<String, Number> data2 = new Data<String, Number>(xvalue.toString(), yvalue2.state[2]);
  //Data<String, Number> verticalMarker = new Data<String, Number>(xvalue.toString(), 0);

    series.getData().add(data);   
    series1.getData().add(data1); 
    series2.getData().add(data2);


}   
series.setName("X-Position");
series1.setName("Y-Position");
series2.setName("Z-Position");


lineChart.getData().addAll(series, series1, series2);

I want to display it on line chart only 20 key and values not all the 100 values on the graph. can u help me out, how can we achieve this? Thanks in advance.

DVarga
  • 21,311
  • 6
  • 55
  • 60
Mysa Vijay
  • 11
  • 2

2 Answers2

0

I don't know that I understand it well so I will talk about the case when you want to decimate the data.

The best solution would be to resample your input data and time vector.

But if you want to be simple, you can try something like this:

  • Always add the starting end ending data point
  • For inner data points calculate the decimation

Example

This example will always produce a LineChart with 20 nodes independently from how many data points you input array has.

Note: this solution is far not the best you can have, and heavily relies on that the input data vectors has the same time vector. It is also influences the result that the step size of the time vector is fixed.

public class ChartingApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);

        // Size of the input data
        int dataCount = 777;

        // One X axis two Y axis
        double[] xValues = new double[dataCount];
        double[] y1Values = new double[dataCount];
        double[] y2Values = new double[dataCount];

        // Generate some initial data
        Random randomGenerator = new Random();

        for (int i = 0; i < dataCount; i++) {
            xValues[i] = i;
            y1Values[i] = randomGenerator.nextInt(200);
            y2Values[i] = randomGenerator.nextInt(200);
        }

        // Amount of nodes to display
        int valueCountToDisplay = 20;

        Series<Number, Number> series1 = new Series<Number, Number>();
        Series<Number, Number> series2 = new Series<Number, Number>();

        // The first element is always added
        series1.getData().add(new Data<Number, Number>(xValues[0], y1Values[0]));
        series2.getData().add(new Data<Number, Number>(xValues[0], y2Values[0]));

        // Generate the inner nodes
        double elementToPick = Math.floor((dataCount - 2) / (valueCountToDisplay - 2));

        for (int i = 1; i < dataCount - 1; i++) {
            if (i % elementToPick == 0) {
                series1.getData().add(new Data<Number, Number>(xValues[i], y1Values[i]));
                series2.getData().add(new Data<Number, Number>(xValues[i], y2Values[i]));
            }
        }

        // The last element is always added
        series1.getData().add(new Data<Number, Number>(xValues[dataCount - 1], y1Values[dataCount - 1]));
        series2.getData().add(new Data<Number, Number>(xValues[dataCount - 1], y2Values[dataCount - 1]));

        Scene scene = new Scene(lineChart, 800, 600);
        lineChart.getData().addAll(series1, series2);

        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

The result is:

enter image description here

The logic behind can be summarized like this:

double[] createSparseArray(double[] inputData, int arraySize) {
    List<Double> sparseList = new ArrayList<Double>();

    sparseList.add(inputData[0]);

    double elementToPick = Math.floor((inputData.length - 2) / (arraySize - 2));

    for (int i = 1; i < inputData.length - 1; i++) {
        if (i % elementToPick == 0)
            sparseList.add(inputData[i]);
    }
    sparseList.add(inputData[arraySize - 1]);
    return sparseList.stream().mapToDouble(d -> d).toArray();
}

The example uses arrays to store the data, but it is really similar with you data structure (using size of Set and looping with the good-old for loop).

DVarga
  • 21,311
  • 6
  • 55
  • 60
  • Hi, Thanks for the reply. I actually need the required first 20 values from the data instead of just showing 20 values from the whole data by diving the whole data with nodes. But this is also helpful in or some other way. Thanks again – Mysa Vijay Jun 20 '16 at 12:59
0

Well, just create a new variable which you increase on each iteration. When the value of this variable reaches 20, break out of the loop. Like this:

int amount = 0;
while(i1.hasNext()) {
    amount += 1;
    if(amount == 20) {
        break;
    }
    ...
}

If you don't know break: Its a keyword that leaves the containing loop.

EDIT

I misunderstood you first, here's the (hopefully) correct answer. Instead of the solution above, do this:

while(i1.hasNext()) {
    map = i1.next();
    if(map.getKey() > 20 || map.getKey() < 1) {
        continue;
    }
    ...
}

This code checks if the key of the entry is between 1 and 20. If not, the next entry will be checked. To do so, it uses continue - a keyword similar to break, but instead of ending the loop, it will just jump to the next iteration.

ZetQ
  • 51
  • 4
  • Hallo, Thanks for reply., I tried your code, it works but that thing is that it only display 20 value from the data but not the whole from 1 to 20. – Mysa Vijay Jun 20 '16 at 12:22
  • I get the required value when I do like this ´code `if(amount == 20){ lineChart.getData().addAll(series, series1, series2);break; } – Mysa Vijay Jun 20 '16 at 14:24