简体   繁体   English

具有类别的Jfreechart散点图

[英]Jfreechart scatter plot with categories

i need to plot a scatter plot but with categories like all points in class a category should be red or the point shape can be a square anything to distinguish it from class b points but i also dont want the points to be joined by lines as in XYPlot. 我需要绘制散点图,但类别应与类中的所有点一样,类别应为红色,或者点形可以是正方形,以将其与b类点区分开来,但是我也不想像XY图 I have searched for it and couldnt find the answer, so if you find this as a duplicate question then please direct me to original one. 我已经搜索了它,却找不到答案,因此,如果您发现这个问题是重复的,请将我直接转到原始问题。 Thanks 谢谢

i played with scatter plot and was able to solve the problem. 我玩过散点图,能够解决问题。 the following code solves it. 以下代码解决了它。 setupData() takes the class name as input and basically categories it. setupData()将类名作为输入,并对其进行基本分类。 I failed to see that earlier. 我没早看。 I hope it helps guys facing same issue. 我希望它可以帮助面临同样问题的人们。

package graph.generator;


import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class GenerateScatterPlot extends ApplicationFrame{

    static XYSeriesCollection dataset;

    public GenerateScatterPlot(String applicationTitle) {
        super(applicationTitle);
        JFreeChart chart = ChartFactory.createScatterPlot("Coordinates", "X", "Y", setupData(), PlotOrientation.VERTICAL,true,true,false); 
        NumberAxis domainAxis = (NumberAxis) chart.getXYPlot().getDomainAxis();
        domainAxis.setAutoRangeIncludesZero(false);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setVerticalAxisTrace(true);
        chartPanel.setHorizontalAxisTrace(true);
        setContentPane(chartPanel);
    }

    public static XYDataset setupData() {
        return dataset;
    }

    public static void addToSet(ArrayList<ArrayList<Double>> coordinates, String classOfData)
    {
        if(dataset == null)
        {
            dataset = new XYSeriesCollection();
        }

        XYSeries series = new XYSeries(classOfData);
        for (ArrayList<Double> arrayList : coordinates) {
            series.add(arrayList.get(0),arrayList.get(1));
        }

        dataset.addSeries(series);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM