luxora
luxora

Reputation: 21

realizing a Spider chart with JFreeChart

I'm working on a program, collecting around twenty slider values from JSliders (bad sliders from values -4 up to 0 and good sliders from 0 to +4) looking like this: enter image description here

My program collects all these 20 sliders in an array, catching the values of the changed sliders.

The program should, therefore, create a Chart, to view these values. Therefore I have to use a Spider chart as you can see here: spider

I made a collection, adding my series to a graph: final XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(r1); dataset.addSeries(r2); Added the dataset to my chart via JFreeChart

designed a plot: final XYPlot plot = xylineChart.getXYPlot();

coded my axis:
` ValueAxis domainAxis = plot.getDomainAxis(); // y
ValueAxis rangeAxis = plot.getRangeAxis(); // x

  domainAxis.setRange(0.0, 1.0);
  ((NumberAxis) domainAxis).setTickUnit(new NumberTickUnit(0.5));
  rangeAxis.setRange(0.0, 1.0);
  ((NumberAxis) rangeAxis).setTickUnit(new NumberTickUnit(0.5));

  plot.getRangeAxis().setRange(-4, 4); // Y Achse Range 
  plot.getDomainAxis().setRange(-4,4);   
  final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
 // renderer.setSeriesShapesVisible(0, false);
  renderer.setSeriesPaint( 0 , Color.RED );
  renderer.setSeriesLinesVisible(0, false);
  renderer.setSeriesPaint( 1 , Color.GREEN );
  renderer.setSeriesLinesVisible(1, false);
  plot.setRenderer(renderer);

So my question is now, how to display the points at the right position of my spider chart.

Do I have to add a coordinate System for every single point here?

for example, I would like to display sozialer Druck (=social pressure) my Slider value from above -4, then I have to set the coordinates (-3/-2) for -3 then (-2.6,-1.6) and so on for every single point.

Could anybody help me make this easier? (I can't really change the background graphic to something like a bar chart or anything easier, unfortunately)

enter image description here

Thank you! @luxora

Upvotes: 1

Views: 1582

Answers (1)

luxora
luxora

Reputation: 21

Based on SpiderWebChartDemo1 and this related example, here is the full code that helped me, if anyone else tries to solve something like that:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
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.BufferedImage;
import java.awt.image.ColorModel;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
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.chart.ui.ApplicationFrame;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

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<=8;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) {
        Color bckColor1 = Color.decode("#4282CE"); //Light blue
        Paint p = new GradientPaint(0, 1, bckColor1, 0, 1, bckColor1);

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(35.0, "S1", "u");
        dataset.addValue(45.0, "S1", "r");
        dataset.addValue(2.0, "S1", "b");
        dataset.addValue(75.0, "S1", "t");
        dataset.addValue(25.0, "S1", "l");
        dataset.addValue(95.0, "S1", "bla");
        dataset.addValue(15.0, "S1", "bla");
        dataset.addValue(45.0, "S1", "bla");
        SpiderWebPlot plot = new SpiderWebPlot(dataset);
        JFreeChart chart = new JFreeChart(plot);
        boolean success = false;
        try {
            BufferedImage image = new BufferedImage(200 , 100, 
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = image.createGraphics();
            chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
            g2.dispose();
            success = true;
        }
        catch (Exception e) {
            success = false;
        }
        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();
        spiderwebchartdemo1.setVisible(true);
    }
}

Upvotes: 1

Related Questions