Concatenate values in the Jfreechart chart

Asked

Viewed 55 times

2

How to concatenate values in jfreechart?

I’m trying this way:

public static DefaultCategoryDataset dataset = new DefaultCategoryDataset();;

    public void atualizaDataset(int humano, int zumbi, int rodada){
        final String series1 = "Humano";
        final String series2 = "Zumbi";

        dataset.addValue(humano, series1, (Comparable)rodada);
        dataset.addValue(zumbi, series2, (Comparable)rodada);
    }

However I am not succeeding, he executes the first time I command, but when I send the second he adds as if they were the first values, they do not concatenate!

  • You are using this example http://www.java2s.com/Code/Java/Chart/JFreeChartLineChartDemo1.htm?

  • Yes, this very one

  • You want to add the values by your main?

  • Yes add by main

1 answer

1

You can do the following, create a constructor that receives a CategoryDataset

public LineChartDemo1(final String title, CategoryDataset dataset) {
        super(title);
        final CategoryDataset dataset = dataset;
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
}

and in your main():

 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//adiciona valores aqui
final LineChartDemo1 demo = new LineChartDemo1("Line Chart Demo", dataset);
//resto do código

If you add after you have built the chart, you will need to rebuild(repaint) it.

  • But this method will not remake the graphics with the new values instead of just adding them to the existing ones?

  • Yes, it generates a graph. If you want to add to the existing one is different, just add in the dataset and make a repaint.

  • But that’s my question. How do I update the dataset without recreating the chart ? I updated the post with the current code. I tried to create the dataset as global and add values to it, but it hasn’t worked out very well yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.