0

在单个系列中,即在时间序列图中,必须获得不同的形状。但它不会来......请参考以下代码并告诉我我犯了什么错误?

      TimeSeriesCollection dataset = new TimeSeriesCollection(glucoseries);                                                
      JFreeChart chart = ChartFactory.createTimeSeriesChart("Glucometer","Date","value",dataset,true,true,false);                           

     XYPlot xyplot = (XYPlot)chart.getPlot();
     XYLineAndShapeRenderer  renderer = (XYLineAndShapeRenderer) xyplot.getRenderer(); 

    int glucovalue = Integer.parseInt(glcvalue);

    if(glucovalue<80)
    {     
    renderer.setSeriesShape(0,new Ellipse2D.Double(-3.0,-3.0,8.0,8.0));
    renderer.setSeriesShapesVisible(0,true);
     }
    else if(glucovalue>80 && glucovalue<100)
    {               
    renderer.setSeriesShape(0,ShapeUtilities.createUpTriangle(4.0f));
    renderer.setSeriesShapesVisible(0,true);            
    }
    else if(glucovalue>100 && glucovalue< 120)
    {                   
    renderer.setSeriesShape(0,ShapeUtilities.createDiamond(4.0f));
    renderer.setSeriesShapesVisible(0,true);                    
    }
    else
    {
    renderer.setSeriesShape(0,ShapeUtilities.createDownTriangle(4.0f));
    renderer.setSeriesShapesVisible(0,true);                    
    }

     ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
    File file1 = new File(request.getRealPath("\\jspchart\\gluco7.png"));
    ChartUtilities.saveChartAsPNG(file1, chart, 700, 400, info);    

在此处输入图像描述

4

2 回答 2

3

IF 语句中的逻辑只评估一次,您需要子类XYLineAndShapeRenderer#getItemShape化以根据当前值返回形状。像这样的东西

    XYLineAndShapeRenderer renderer =  new XYLineAndShapeRenderer(true, true){
        Shape ellipse = new Ellipse2D.Double(-3.0,-3.0,8.0,8.0);
        Shape upTriangle = ShapeUtilities.createUpTriangle(4.0f);
        Shape diamond = ShapeUtilities.createDiamond(4.0f);
        Shape downTriangle = ShapeUtilities.createDownTriangle(4.0f);


        @Override
        public Shape getItemShape(int row, int column) {
            Double value = (Double) plot.getDataset(0).getY(row, column);
            if (value > 6.0 ) {
                return ellipse;
            } else if (value >= 5.0 ) {
                return upTriangle;
            } else if (value >= 2.0 ) {
                return diamond;
            }else {
                return downTriangle;
            }
        }

    };

  plot.setRenderer(renderer);

在此处输入图像描述

于 2013-04-05T15:47:24.243 回答
0

我无法弄清楚 :葡萄糖值的价值是什么

但是您在这里错过的一件事是:您没有正确考虑边界值。

例如:

 if(glucovalue<80)
{     
renderer.setSeriesShape(0,new Ellipse2D.Double(-3.0,-3.0,8.0,8.0));
renderer.setSeriesShapesVisible(0,true);
 }
else if(glucovalue>80 && glucovalue<100)
{    

在上面的代码中,如果glucovalue = 80

更改代码如下:

 if(glucovalue<80)
{     
renderer.setSeriesShape(0,new Ellipse2D.Double(-3.0,-3.0,8.0,8.0));
renderer.setSeriesShapesVisible(0,true);
 }
else if(glucovalue>=80 && glucovalue<=100)
{  
于 2013-04-05T09:51:40.800 回答