我可以在XYPlot中更改DomainCrosshair的默认颜色:jFreeChart

时间:2013-02-20 05:31:18

标签: java

我指的是:

Changing the shapes of points in scatter plot

但是我无法看到x,y数据点的形状:

public void plotHysteresis()
       {

        s1= new double[6];
        s2= new double[6];
        s3= new double[6];
        s4= new double[6];

        int i=0;
        for(i=0;i<6;i++)
           {
               s1[i]=hyst[i];
               System.out.println("s1[" +i +"] : " +s1[i] +"\n");
               }
        for(i=0;i<6;i++)
           {
               s2[i]=hyst[10-i];
               System.out.println("s2[" +i +"] : " +s2[i] +"\n");
               }
        for(i=0;i<6;i++)
               {
                   s3[i]=hyst[11+i];
                   System.out.println("s3[" +i +"] : " +s3[i] +"\n");
               }
        for(i=0;i<6;i++)
                       {
                           s4[i]=hyst[21-i];
                           System.out.println("s4[" +i +"] : " +s4[i] +"\n");
               }

       // DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        XYSeries series3 = new XYSeries("III");
        int x=-25;
        System.out.println(x +"***\n");
        for(i=5;i>=0;i--)
        {

            series3.add(x,s3[i]);
            x=x+5;

        }
        System.out.println(x +"***\n");
        XYSeries series4 = new XYSeries("IV");
        for(i=0;i<6;i++)
        {
            x=x-5;
            series4.add(x,s4[i] );

        }
         System.out.println(x +"###\n");
         XYSeries series1 = new XYSeries("I");
         x=0;
        for(i=0;i<6;i++)
        {
            series1.add(x,s1[i] );
            x=x+5;
        }
       // x=x-5;
        System.out.println(x +"***\n");
        XYSeries series2 = new XYSeries("II");
        for(i=5;i>=0;i--)
        {
            x=x-5;
            series2.add(x,s2[i] );

        }
        System.out.println(x +"***\n");

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series3);
        dataset.addSeries(series4);
        dataset.addSeries(series1);
        dataset.addSeries(series2);

        JFreeChart chart = ChartFactory.createXYLineChart(
    "Hysteresis Plot", // chart title
    "Pounds(lb)", // domain axis label
    "Movement(inch)", // range axis label
    dataset, // data
    PlotOrientation.VERTICAL, // orientation
    false, // include legend
    true, // tooltips
    false // urls
    );


        chart.setBackgroundPaint(Color.white);

        //chart.setBackgroundPaint(new Color(249, 231, 236));
        /*CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);
        plot.getRenderer().setSeriesPaint(0, Color.BLUE);
        plot.getRenderer().setSeriesPaint(1, Color.BLUE);
        plot.getRenderer().setSeriesPaint(2, Color.BLUE);
        plot.getRenderer().setSeriesPaint(3, Color.BLUE);*/
        Shape cross = ShapeUtilities.createRegularCross(4, 3);

        XYPlot plot = (XYPlot) chart.getPlot();
        XYItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesPaint(1, Color.RED);
        renderer.setSeriesPaint(2, Color.RED);
        renderer.setSeriesPaint(3, Color.RED);

        renderer.setSeriesShape(0, cross);
        renderer.setSeriesShape(1, cross);
        renderer.setSeriesShape(2, cross);
        renderer.setSeriesShape(3, cross);
        renderer.setSeriesVisible(0,true);
        renderer.setSeriesVisible(1,true);
        renderer.setSeriesVisible(2,true);
        renderer.setSeriesVisible(3,true);

        plot.setDomainCrosshairVisible(true);//Sets Y axis visible blue
        plot.setRangeCrosshairVisible(true);//Sets X axis visible blue


        /*LineAndShapeRenderer rend
    = (LineAndShapeRenderer) plot.getRenderer();
    rend.setShapesVisible(true);*/
    //renderer.setDrawOutlines(true);
    //renderer.setUseFillPaint(true);
    //renderer.setFillPaint(Color.white);

        ChartPanel frame = new ChartPanel(chart);
        frame.setVisible(true);
        frame.setSize(plotPanel.getWidth(),plotPanel.getHeight());
        plotPanel.add(frame);
        plotPanel.repaint();
       }

上面的代码给出了输出:

enter image description here

之前我能够看到使用CategoryPlot和LineChart的形状,如代码的注释部分所示,但它不适用于XYLineChart。

请帮忙 感谢

获得解决方案: How to get diamond shape for points in JFreechart 我可以改变颜色吗? plot.setDomainCrosshairVisible(true); //将Y轴设置为可见蓝色 plot.setRangeCrosshairVisible(true); //将X轴设置为可见蓝色 想把它变成黑色,而不是看起来像蓝色。

1 个答案:

答案 0 :(得分:0)

好的得到了答案:

How to get diamond shape for points in JFreechart

XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
r.setSeriesShape(0, ShapeUtilities.createDiamond(5));
r.setSeriesShapesVisible(0, true);

未在我的代码中添加XYLineAndShapeRenderer