改变 JFreeChart XYPLot 中单点的形状 [英] Changing shape of single point in JFreeChart XYPLot

查看:23
本文介绍了改变 JFreeChart XYPLot 中单点的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JFreeChart XYPLot 绘制具有不同标签的 XYData 集.我为不同的标签创建了不同的 XYSeries 对象,以便我可以为不同的标签使用不同的颜色.现在我需要要求更改每个 XYDataSeries 中特定点(测试数据)的形状,如下所示 .在上面的绘图中,有两个不同的 XYSeries,分别是蓝色和红色.在这两个中,我需要将某些点(测试数据)的形状更改为 X 而不是 circle .在 JFreeChart 中可能吗?这篇文章解释了如何为整个数据集做这件事,但我只想更改特定点

I am using JFreeChart XYPLot for plotting a XYData set with different labels . I have created different XYSeries objects for different labels so that I can have different colors for different labels . Now I need to require the change the shapes of specific points(test data) in each XYDataSeries as below . In the above plotting , there are two different XYSeries with blue and red color . Out of these two I need to change the shapes of some points(test data) to X instead of circle . Is it possible in JFreeChart. This post explained on how to do it for whole data set , but I want to change only specific points

以下是我目前编写的代码

Below is the code I have written so far

  public static Map<String, XYSeries> createXYSeries(Data[] dataSet){       
    Map<String,XYSeries> xySeries = new HashMap<String, XYSeries>();        
    for(Data data : dataSet){
        if(xySeries.get(data.actualLabel) == null){
            xySeries.put(data.actualLabel, new XYSeries(data.actualLabel));
        }
        xySeries.get(data.actualLabel).add(data.dimensionValues[0],data.dimensionValues[1]);
    }   

    return xySeries;
}

   public XYDataset createXYSeriesCollection(Map<String, XYSeries> plottingDataSet) {
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    for (String key : plottingDataSet.keySet()) {
        xySeriesCollection.addSeries(plottingDataSet.get(key));
    }
    return xySeriesCollection;
}

   private ChartPanel createPlottingPanel(String title,
        Map<String, XYSeries> plottingDataSet) {
    JFreeChart jfreechart = ChartFactory.createScatterPlot(title, "X", "Y",
            createSampleData(plottingDataSet), PlotOrientation.VERTICAL,
            true, true, false);
    XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
    xyPlot.setDomainCrosshairVisible(true);
    xyPlot.setRangeCrosshairVisible(true);
    xyPlot.setBackgroundPaint(Color.white);
    return new ChartPanel(jfreechart);
}

注意:我正在尝试绘制 KNearestNeighbors 结果.(火车数据的圆圈和测试数据的 X)

Note : I am trying to plot the KNearestNeighbors results .(Circles for train data and X for test data)

推荐答案

ChartFactory.createScatterPlot() 实例化一个 XYLineAndShapeRenderer.您可以将渲染器替换为允许您有选择地替换 getItemShape() 返回的 Shape 的渲染器,如下所示.

ChartFactory.createScatterPlot() instantiates an XYLineAndShapeRenderer. You can replace the renderer with one that lets you selectively replace the Shape returned by getItemShape(), as shown below.

    xyPlot.setRenderer(new XYLineAndShapeRenderer(false, true) {

        @Override
        public Shape getItemShape(int row, int col) {
            if (row == 0 & col == N) {
                return ShapeUtilities.createDiagonalCross(5, 2);
            } else {
                return super.getItemShape(row, col);
            }
        }
    });

完整示例,如运行:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Shape;
import java.util.*;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.ShapeUtilities;

/**
 * @see http://stackoverflow.com/a/20359200/230513
 * @see http://stackoverflow.com/a/6669529/230513
 */
public class ScatterShape extends JFrame {

    private static final int N = 8;
    private static final int SIZE = 345;
    private static final String title = "Scatter Shape Demo";
    private static final Random rand = new Random();
    private final XYSeries series = new XYSeries("Data");

    public ScatterShape(String s) {
        super(s);
        final ChartPanel chartPanel = createDemoPanel();
        this.add(chartPanel, BorderLayout.CENTER);
    }

    private ChartPanel createDemoPanel() {
        JFreeChart chart = ChartFactory.createScatterPlot(
            title, "X", "Y", createSampleData(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) chart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        xyPlot.setRenderer(new XYLineAndShapeRenderer(false, true) {

            @Override
            public Shape getItemShape(int row, int col) {
                if (row == 0 & col == N) {
                    return ShapeUtilities.createDiagonalCross(5, 2);
                } else {
                    return super.getItemShape(row, col);
                }
            }
        });
        adjustAxis((NumberAxis) xyPlot.getDomainAxis(), true);
        adjustAxis((NumberAxis) xyPlot.getRangeAxis(), false);
        xyPlot.setBackgroundPaint(Color.white);
        return new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(SIZE, SIZE);
            }
        };
    }

    private void adjustAxis(NumberAxis axis, boolean vertical) {
        axis.setRange(-3.0, 3.0);
        axis.setTickUnit(new NumberTickUnit(0.5));
        axis.setVerticalTickLabels(vertical);
    }

    private XYDataset createSampleData() {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        for (int i = 0; i < N * N; i++) {
            series.add(rand.nextGaussian(), rand.nextGaussian());
        }
        xySeriesCollection.addSeries(series);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ScatterShape demo = new ScatterShape(title);
                demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                demo.pack();
                demo.setLocationRelativeTo(null);
                demo.setVisible(true);
            }
        });
    }
}

这篇关于改变 JFreeChart XYPLot 中单点的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆