线性图表与多个系列颜色问题

时间:2016-10-25 10:21:21

标签: java jfreechart

代码使用折线图显示3系列图表。我为每个系列设置了3种不同的颜色。但是发生的事情是前2系列显示相同的颜色,我设置为series1。 Series3显示了我没有设置的不同颜色。

public class LineChartServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
private Connection dbConnection  = null;

JDBCCategoryDataset dataset1,dataset2,dataset3;

public LineChartServlet() {
    dbConnection  = DaoDbUtil.getConnection();
   }

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

     dataset1 = new JDBCCategoryDataset(dbConnection);
     dataset2 = new JDBCCategoryDataset(dbConnection);
     dataset3 = new JDBCCategoryDataset(dbConnection);
try {
    dataset1.executeQuery("SELECT top 25 DateTime,Reading FROM SPC_DATA 
        where Machine = 1 and Channel = 1 order by DateTime desc");
    dataset2.executeQuery("SELECT top 25 DateTime,Reading FROM SPC_DATA 
        where Machine = 1 and Channel = 2 order by DateTime desc");
    dataset3.executeQuery("SELECT top 25 DateTime,Reading FROM SPC_DATA 
        where Machine = 1 and Channel = 3 order by DateTime desc");

DefaultCategoryDataset dcd = new DefaultCategoryDataset();

    for (int i = 0; i < dataset1.getColumnCount(); i++) {

    dcd.addValue(dataset1.getValue(dataset1.getRowKey(0), dataset1.getColumnKey(i)),
                    dataset1.getRowKey(0), dataset1.getColumnKey(i));
    dcd.addValue(dataset2.getValue(dataset2.getRowKey(0), dataset2.getColumnKey(i)),
                    dataset2.getRowKey(0), dataset2.getColumnKey(i));
    dcd.addValue(dataset3.getValue(dataset3.getRowKey(0), dataset3.getColumnKey(i)),
                        dataset3.getRowKey(0), dataset3.getColumnKey(i));
    }

JFreeChart chart = ChartFactory.createLineChart("x vs y", "x", "y", dcd,                    
             PlotOrientation.VERTICAL, false, true,false);
chart.setBorderVisible(true);

final LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
      renderer1.setBaseShapesVisible(true);
      renderer1.setBaseShapesFilled(true);
      renderer1.setSeriesStroke(0, new BasicStroke(2.0f));
    Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
        renderer1.setSeriesShape(0, circle);

final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
      renderer2.setBaseShapesVisible(true);
      renderer2.setBaseShapesFilled(true);
      renderer2.setSeriesStroke(0, new BasicStroke(2.0f));
    Shape circle2 = new Ellipse2D.Double(0,0,5,5);
        renderer2.setSeriesShape(0, circle2);

final LineAndShapeRenderer renderer3 = new LineAndShapeRenderer();
      renderer3.setBaseShapesVisible(true);
      renderer3.setBaseShapesFilled(true);
      renderer.setSeriesStroke(0, new BasicStroke(2.0f));
    Shape circle3 = new Ellipse2D.Double(0,0,3,3);
        renderer3.setSeriesShape(0, circle3);

final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setDataset(0,dataset1);
plot.setRenderer(0, renderer1);
plot.setDataset(1,dataset2);
plot.setRenderer(1, renderer2);
plot.setDataset(2,dataset3);
plot.setRenderer(2, renderer3);

renderer1.setSeriesPaint(0, Color.decode("#0066CC"));
renderer2.setSeriesPaint(1, Color.GREEN);
renderer3.setSeriesPaint(2, Color.WHITE);

if (chart != null) {
        int width = 500;
        int height = 300;
        response.setContentType("image/jpeg");
        OutputStream out = response.getOutputStream();
        ChartUtilities.writeChartAsJPEG(out, chart, width, height);
        }
    }
        catch (SQLException e) {
        System.err.println(e.getMessage());
        }
    }
public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    }
}

1 个答案:

答案 0 :(得分:0)

我通过这样的设置解决了这个问题

renderer1.setSeriesPaint(0, Color.decode("#0066CC"));
renderer2.setSeriesPaint(0, Color.GREEN);
renderer3.setSeriesPaint(0, Color.WHITE);