ricardo spek
ricardo spek

Reputation: 5

how to use Arraylist (for-loop) in Jfreechart piechart?

i recently started on collecting data in my Database, i wrote a code that retrieves this information, name(language) and the amount of times this language has been counted, into an arraylist. from this data i wanted to make a Piechart.

i have tried to do this with jfreechart

does anyone know how to use the data in my arraylist with the piechart createdataset code from jfreechart , since my attempts resulted in failure(empty piechart) while using a for loop in the the method ,createdataset, since the data in the arraylist changes and therefore dont want to hardcode the values and names.

for loop attempt(everything else left default):

public PieDataset createDataset() {
    DefaultPieDataset result = new DefaultPieDataset();
    for(Language l: languageList){
    result.setValue( l.getName(), l.getCount());
    }
    return result;
}

if this for loop is possible what am i doing wrong or what has to change tom make this work, if i'm completely off i also want to know.

--edit--

public PieDataset createDataset() {
    DefaultPieDataset result = new DefaultPieDataset();
    int count = 0;
    for(Language l: languageList){
        result.insertValue(count++, l.getName(), l.getCount());

    }
    return result;
}

Upvotes: 0

Views: 980

Answers (1)

SMA
SMA

Reputation: 37023

Try using insertValue api like:

int count = 0;
for(Language l: languageList){
    result.insertValue(count++, l.getName(), l.getCount());
}

Upvotes: 1

Related Questions