1

I have created an application that reads in serial input from an arduino (Pressure and Temperature data on areas of human leg) and plots it to a jfreechart bar graph in real time. I have created it so that when any bar value of a specific category (Area of the leg) in a series(Pressure or Temperature) exceeds a certain threshold that specific bar changes color from blue(Pressure)/green(Temperature) to red (indicating that this value is too high in that area). It can also change back from red to blue/green when the value decreases below this threshold.

Pressure Threshold = 300 Temperature Threshold = 37

Below are images of this process and the graph:

Below Threshold: All Sensor Outputs Below Threshold

Above Threshold: Sensor Output Values Above Threshold

From the above images (within the links) you can see that this all works well, but the legend's color box for this series is going mad (as you can see from the graph). It is constantly cycling through all the colors in the spectrum (blue, black, green, yellow, etc). and because this is in real time it is changes every 50ms.

I would like to know if there is a way to set the color box (for each series within the legend) to always show blue for pressure, and green for Temperature, (even though the bar could change to red now and then)

Below is a bit of my code that I use to achieve this:


Paint[] colorsP = new Paint[dataP.length];   
Paint[] colorsT = new Paint[dataT.length];
for(int i = 0; i < colorsP.length; i++)
{
colorsP[i] = dataP[i] > 300 ? Color.red:Color.blue;  //Threshold decision
}

CategoryItemRenderer rendererP = new CustomRenderer(colorsP); //RendererP for Pressure series
        for(int i = 0; i < colorsT.length; i++)
            {
             colorsT[i] = dataT[i] > 37 ? Color.red:Color.green; //threshold        decision
            }           

CategoryItemRenderer rendererT = new CustomRenderer(colorsT); //Renderer for Temperature Series
//below is code for adding the value of each bar to the top of the bars.
ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_CENTER,TextAnchor.TOP_CENTER,0);
rendererP.setPositiveItemLabelPosition(p);      
rendererP.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE5,TextAnchor.BOTTOM_RIGHT)); 
DecimalFormat NumberFormat = new DecimalFormat("##");
rendererP.setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator("{2}",NumberFormat));                       
rendererP.setSeriesItemLabelsVisible(0, true);  
rendererT.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE5,TextAnchor.BOTTOM_RIGHT));
rendererT.setSeriesItemLabelGenerator(1, new StandardCategoryItemLabelGenerator("{2}",NumberFormat)); 
rendererT.setSeriesItemLabelsVisible(1, true);
                        ;

rendererP.setBasePositiveItemLabelPosition(p);
rendererT.setBasePositiveItemLabelPosition(p);
//assign each series of the plot its specific renderer
plot.setRenderer(0,rendererP);
plot.setRenderer(1,rendererT);          
  • The legend gets its color from the same `DrawingSupplier` used by the renderer; see the examples cited [here](http://stackoverflow.com/a/16754801/230513). For specific guidance, please edit your question to include a [mcve] that exhibits the problem you describe. – trashgod Mar 14 '17 at 18:29
  • Hi trashgod, thank you for your comment. I followed your link and I was a little unsure as to how to use the DrawingSupplier. I have updated my post, incase you did not 100% understand my problem. I just wanna set the legend color box for Pressure to Blue, and for Temperature to Green.. I do not want it to change at all. – Scott Bruton Mar 16 '17 at 08:19
  • Once I posted my reply to you a did a few more edits. it should now explain exactly what problem I am having. – Scott Bruton Mar 16 '17 at 08:55
  • Absent a [mcve], I can only guess where your program goes awry; I'd override `getItemPaint()` in the `BarRenderer`. – trashgod Mar 16 '17 at 09:18
  • Also, check your synchronization, for [example](http://stackoverflow.com/a/13205322/230513). – trashgod Mar 16 '17 at 10:36
  • Also consider `ThermometerPlot`, seen [here](http://stackoverflow.com/a/7602126/230513). – trashgod Mar 17 '17 at 10:14

0 Answers0