在JFreeChart XYPLot中更改单点的形状

 海底来的沙3 发布于 2023-02-13 10:19

我正在使用JFreeChart XYPLot绘制具有不同标签的XYData集.我为不同的标签创建了不同的XYSeries对象,这样我就可以为不同的标签设置不同的颜色.现在我需要更改每个XYDataSeries中特定点(测试数据)的形状,如下所示在此输入图像描述.在上面的绘图中,有两种不同的XYSeries,蓝色和红色.在这两个中,我需要将某些点(测试数据)的形状更改为X而不是圆形.是否有可能在JFreeChart. 这篇文章解释了如何为整个数据集做到这一点,但我想只改变特定点

下面是我到目前为止编写的代码

  public static Map createXYSeries(Data[] dataSet){       
    Map xySeries = new HashMap();        
    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 plottingDataSet) {
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    for (String key : plottingDataSet.keySet()) {
        xySeriesCollection.addSeries(plottingDataSet.get(key));
    }
    return xySeriesCollection;
}

   private ChartPanel createPlottingPanel(String title,
        Map 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用于测试数据)

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

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

    2023-02-13 10:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有