How to draw a spiderchart above a existing JfreeChart

不问归期 提交于 2019-12-02 02:25:25
trashgod

I want to override few more spider graphs on the same chart.

It may help to examine how a spider web plot is used to display multivariate data. The simplified example below compares just two OBSERVATIONS, each having five VARIABLES named A .. E, with random values in the range 1 .. 3. By chance, the values for variable B coincide; the rest differ. You can adjust the value of OBSERVATIONS to see the effect, but the result becomes progressively more muddled as the number of observations grows. You may want to alter series visibility, as suggested here, or consider these alternatives.

import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;

/** @see https://stackoverflow.com/a/32885067/230513 */
public class SpiderChart extends ApplicationFrame {

    private static final int OBSERVATIONS = 2;
    private static final int VARIABLES = 5;
    private static final Random r = new Random();

    public SpiderChart(String s) {
        super(s);
        add(createDemoPanel());
    }

    private static CategoryDataset createDataset() {

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 1; i <= OBSERVATIONS; i++) {
            String rowKey = "Observation " + i;
            for (int j = 1; j <= VARIABLES; j++) {
                Comparable colKey = Character.valueOf((char)(j+64));
                dataset.addValue(r.nextInt(3) + 1, rowKey, colKey);
            }
        }
        return dataset;
    }

    public static JFreeChart createChart(CategoryDataset dataset) {
        SpiderWebPlot plot = new SpiderWebPlot(dataset);
        JFreeChart chart = new JFreeChart("Test", plot);
        return chart;
    }

    public static JPanel createDemoPanel() {
        JFreeChart jfreechart = createChart(createDataset());
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(() -> {
            SpiderChart demo = new SpiderChart("SpiderWebChart");
            demo.pack();
            demo.setDefaultCloseOperation(EXIT_ON_CLOSE);
            demo.setVisible(true);
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!