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:
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);