Customize bar colors in XYJfree chart

后端 未结 3 558
日久生厌
日久生厌 2021-01-21 08:49

how to paint different bars in different colors, I tried to use renderer, here is my sample code:

    public IntervalXYDataset createDataset() throws Interrupted         


        
相关标签:
3条回答
  • 2021-01-21 09:32

    What you want to do is the following:

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.getRenderer().setSeriesPaint(1, Color.yellow);
    

    Replace 1 with the (zero-based) index of the bar whose color you would like to change.

    Edit to respond to comment:

    List<Color> myBarColors = .....
    
    XYPlot plot = (XYPlot) chart.getPlot();
    XYItemRenderer renderer = plot.getRenderer();
    
    for (int i = 0; i < 40; i++) {
        renderer.setSeriesPaint(i, myBarColors.get(i));
    }
    

    Edit 2: Misunderstood OPs problem, new solution in comments.

    0 讨论(0)
  • 2021-01-21 09:37

    The most flexible approach is to override the getItemPaint() method of AbstractRenderer in a custom XYBarRenderer, as shown here for XYLineAndShapeRenderer.

    0 讨论(0)
  • 2021-01-21 09:52

    I found the answer Create two series, and then add how many ever bars you want and set color for each series. using setSeriesPaint

    0 讨论(0)
提交回复
热议问题