0

It is possible to create a loop dataset in jfreechart ?. I have a List that contains data. Here is the code

List<ReportChart> theDatas;
public CategoryDataset createDataset() {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    for (ReportChart reportChart : theDatas) {                      
        dataset.setValue(reportChart.getLevensthein(), reportChart.getMainFileName(), reportChart.getComparingFileName());
        dataset.setValue(reportChart.getJaccard(), reportChart.getMainFileName(), reportChart.getComparingFileName());
        dataset.setValue(reportChart.getCosine(), reportChart.getMainFileName(), reportChart.getComparingFileName());


    }
    return dataset;
}

In code above, I just get the last data from my List ? It so appreciated for the help...

edit =============

I try some changes, but I dont know how to get this solved.

public CategoryDataset createDataset() {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    for (ReportChart reportChart : theDatas) {

        final String nameOfMainFile = reportChart.getMainFileName();
        final String nameOfComparingFile = reportChart.getComparingFileName();
        final double scoreOfLevenstheins = reportChart.getLevensthein();
        final double scoreOfJaccard = reportChart.getJaccard();
        final double scoreOfCosine = reportChart.getCosine();
        final String algoritma1 = "Levenstheins";
        final String algoritma2 = "Jaccard";
        final String algoritma3 = "Cosine";

        System.out.println(algoritma1 + " : " + "<" + nameOfMainFile + " || " + nameOfComparingFile + ">" + " : " + scoreOfLevenstheins);
        System.out.println(algoritma2 + "      : " + "<" + nameOfMainFile + " || " + nameOfComparingFile + ">" + " : " + scoreOfJaccard);
        System.out.println(algoritma3 + "       : " + "<" + nameOfMainFile + " || " + nameOfComparingFile + ">" + " : " + scoreOfCosine);

        dataset.addValue(scoreOfLevenstheins, algoritma1, nameOfComparingFile);
        dataset.addValue(scoreOfJaccard, algoritma2, nameOfComparingFile);
        dataset.addValue(scoreOfCosine, algoritma3, nameOfComparingFile);

    }

    return dataset;
}

This is the first screenshot of my application when I am choose the data : https://www.dropbox.com/s/t46mzo42of7hdyl/failed1.png and this is the graph to represented the similiar of both code https://www.dropbox.com/s/a5dpzf9j22tfqdh/failed2.png

Mr .TrashGod. this is the complete problem ... failed to loop List of Data in JfreeChart

==== edit again ====

I ve been try new experiment, dataset.addvalue need unique ID and value in comparable column key. the code like this :

DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();

    for (ReportChart object : theDatas) {
        String algoritma1 = "Levenstheins";
        String algoritma2 = "Jaccard";
        String algoritma3 = "Cosine";
        String nameOfComparingFile = object.getComparingFileName();
        double scoreLv = object.getLevensthein();
        double scoreJc = object.getJaccard();
        double scoreCo = object.getCosine();


        defaultcategorydataset.addValue(scoreLv, algoritma1, nameOfComparingFile);
        defaultcategorydataset.addValue(scoreJc, algoritma2, nameOfComparingFile);
        defaultcategorydataset.addValue(scoreCo, algoritma3, nameOfComparingFile);

        String s = "First";
        String s1 = "Second";
        String s2 = "Third";
        String s3 = "Category 1";
        String s4 = "Category 2";
        String s5 = "Category 3";
        String s6 = "Category 4";
        String s7 = "Category 5";

        defaultcategorydataset.addValue(scoreLv, algoritma1, s3);
        defaultcategorydataset.addValue(scoreJc, algoritma2, s4);
        defaultcategorydataset.addValue(scoreCo, algoritma3, s5);
        defaultcategorydataset.addValue(scoreLv, algoritma1, s6);
        defaultcategorydataset.addValue(scoreJc, algoritma2, s7);
        defaultcategorydataset.addValue(scoreCo, algoritma3, s3);
        defaultcategorydataset.addValue(scoreLv, algoritma1, s4);
        defaultcategorydataset.addValue(scoreJc, algoritma2, s5);
        defaultcategorydataset.addValue(scoreCo, algoritma3, s6);
        defaultcategorydataset.addValue(scoreLv, algoritma1, s7);
        defaultcategorydataset.addValue(scoreJc, algoritma2, s3);
        defaultcategorydataset.addValue(scoreCo, algoritma3, s4);
        defaultcategorydataset.addValue(scoreLv, algoritma1, s5);
        defaultcategorydataset.addValue(scoreJc, algoritma2, s6);
        defaultcategorydataset.addValue(scoreCo, algoritma3, s7);


    }

from that, I know that I need a comparable. How to make this real Mr.trashGod. i was try this code from the link that you pointing to me

public class UniqueValue implements  Comparable<UniqueValue>{

private final String uniqueId;
private final String value;

public UniqueValue(String uniqueId, String value) {
    this.uniqueId = uniqueId;
    this.value = value;
}

UniqueValue(String nameOfComparingFile, double scoreLv) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}


@Override
public int compareTo(UniqueValue o) {
    return uniqueId.compareTo(o.uniqueId);
}

@Override
 public int hashCode() {
    return uniqueId.hashCode();
}

@Override
 public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj instanceof UniqueValue) {
        return uniqueId.equals(((UniqueValue)obj).uniqueId);
    }
    return false;
}

  @Override
public String toString() {
        return value;
    }

}

and now, how to use this class..?

Community
  • 1
  • 1
user3767455
  • 29
  • 3
  • 8
  • Please edit your question to include an [mcve](http://stackoverflow.com/help/mcve) that exhibits the problem you describe. – trashgod Jun 29 '14 at 16:53

1 Answers1

2

Take a look at the API docs for the setValue() method - the first argument is the data value, followed by the row and column keys that identify the cell for the value. So in your code you are setting the value in one cell three times in each iteration of the loop. You'll end up with the value you set the third time.

David Gilbert
  • 4,427
  • 14
  • 22
  • so, what is the solution ? – user3767455 Jun 29 '14 at 06:42
  • +1 This _is_ the solution; a canonical example is cited [here](http://stackoverflow.com/a/18424706/230513) and additional related examples are shown [here](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjfreechart%5d%20DefaultCategoryDataset). – trashgod Jun 29 '14 at 16:57
  • My suggestion to study the documentation for that one method was incomplete…you should study the documentation for the *whole* class. And the source code. And create some small working examples in code to test it out. Once you are comfortable with how DefaultCategoryDataset works (it's a table of numbers where each row and column is identified by a unique key) you can move on to creating a bar chart from such a dataset. Change the dataset and see how the chart changes (look at which bar is mapped to which data item). At some point in this process, the solution will become obvious to you. – David Gilbert Jun 30 '14 at 03:52
  • lol, yes a whole class. Software development might not be the right field for you. – David Gilbert Jun 30 '14 at 06:26