0

I'm trying to sort data string on both axis but it doesn't work. The code is below :

    //defining the axes
    final CategoryAxis xAxis = new CategoryAxis();
    final Axis<String> yAxis = new CategoryAxis();
    xAxis.setLabel("Day");
    yAxis.setLabel("Duration");
    //creating the chart
    LineChart<String, String> dataDiagram = new LineChart<String,String>(xAxis,yAxis);
    dataDiagram.setAnimated(false);

    dataDiagram.setTitle("Data");
    Iterator<String> i = this.choosenActivities.iterator();
    while (i.hasNext()){
        String choosenActivity= i.next();
        Activity activity = this.activities.get(choosenActivity);
        //defining a series
        ObservableList<Data<String, String>> data = FXCollections.observableArrayList() ;
        Iterator<Entry<Day,Long>> is = activity.durationPerDay.entrySet().iterator();
        while (is.hasNext()){
            Entry<Day,Long> e= is.next();
            //populating the series with data
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            Date date = new Date();
            date.setTime(e.getValue());
            data.add(new Data<String,String>(e.getKey().toString(), dateFormat.format(date)));

        }
        SortedList<Data<String, String>> sortedData = new SortedList<>(data, (data1, data2) -> 
        data1.getXValue().compareTo(data2.getXValue()));
        SortedList<Data<String, String>> sortedData2 = new SortedList<>(sortedData, (data1, data2) -> 
        data1.getYValue().compareTo(data2.getYValue()));
        dataDiagram .getData().add(new Series<>(activite.nom,sortedData2));
    }

Result is that xaxis is well sorted with the entries {2016/7/13 ; 2016/7/14 ; 2016/7/18 ; etc.} in this order, but yaxis gives {00:00:01 ; 00:00:23 ; 00:00:07}

I'm looking for another way of formatting time(datas are Long give by System.currentTimeMillis()) as numbers instead of strings, or a way of sorting string in my case. I have seen tickLabelFormatter but it only allows to provide a prefix and a suffix to the number, not to format it as a time.

1 Answers1

1

Can't you sort your time data before transforming them to a String ? In this case you could use

Collections.sort(list);

for the sorting because the natural order of longs. If it's really necessary to transform you data to String before sorting, you could try to write an custom Comparator to do the job.

How to use Comparator in Java to sort

https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Community
  • 1
  • 1
SilverMonkey
  • 1,003
  • 7
  • 16