Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

java - How to draw a spiderchart above a existing JfreeChart

I have one a jfree chart which I can generate everytime I run the code. Now i want to override few more spider graphs on the same chart. please help me how to do that

enter image description here

Above this i need to add one more spider chart using jfree.

Here is my code for doing this chart.

package com.rectrix.exide.pdfbox;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;

public class DemoChart extends ApplicationFrame {

    public DemoChart(String s)
    {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    private static CategoryDataset createDataset()
    {


        String s1 = "First";
        String s2 = "Second";
        String s3 = "Third";
        String s4 = "Forth";
        String s5 = "Fivth";
        String s6 = "Sixth";
        String s7 = "Seventh";
        String s8 = "Eighth";
        String s9 = "Ninth";
        String s10 = "Tenth";

        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        int count = 5;
        int value = 0;
        //String keyRow="s";
        for (int i=1;i<=10;i++){
            value = i*4;
            Comparable colKey = 0;
            String keyRow = "s"+i;
            for(int j=1;j<=count;j++){
            colKey = j;
                defaultcategorydataset.addValue(value, keyRow, colKey);
            }
        }
return defaultcategorydataset;
    }

    public static JFreeChart createChart1(CategoryDataset categorydataset,double d) {
        SpiderWebPlot plot = new SpiderWebPlot(categorydataset);
        Color bckColor1 = Color.decode("#4282CE"); //Light blue
        Paint p = new GradientPaint(0, 1, bckColor1, 0, 1, bckColor1);
        plot.setSeriesPaint(p);
        JFreeChart chart = new JFreeChart("", plot);
        return chart;
    }


    public static JPanel createDemoPanel()
    {
        JFreeChart jfreechart = createChart1(createDataset(), 10D);
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[])
    {
        DemoChart spiderwebchartdemo1 = new DemoChart("JFreeChart: SpiderWebChartDemo1.java");
        spiderwebchartdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(spiderwebchartdemo1);
        spiderwebchartdemo1.setVisible(true);
    }
}

Please help me as soon as possible i need to send this build by tomorrow Thank u in advance for helping and taking efforts to see this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

spider web plot

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);
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...