I am working on a program that takes an array of doubles and displays them all in one line on a horizontal bar graph. Right now it works except for the coloring. I have an indeterminate amount of rows so the data is added to the graph like this:
public CategoryDataset createDataset() {
DefaultCategoryDataset bardataset1 = new DefaultCategoryDataset();
for (int i = 0; i < nanoArray.length; i++) {
bardataset1.addValue(nanoArray[i], "Packet" + i, "Class A");
bardataset1.addValue(startgap, "Packet Gap" + i, "Class A");
}
}
This stacks the data properly but because there are around 300 different rowKeys it assigns 300 different colors. I attached this picture to show you what it looks like:
As you can see this makes the data completely unreadable. What I want is to see bars of alternating colors of red then blue.
Edit: I have found the answer; I used jcern's method. I also wrote a for
loop that iterates through the length of my double array and assigns a color based on whether it is even or odd.
for (int i = 0; i < packetCount; i++) {
setSeriesToolTipGenerator(i, new StandardCategoryToolTipGenerator());
if (i % 2 == 0) {
setSeriesPaint(i, new Color(255, 0, 0));
} else {
setSeriesPaint(i, new Color(0, 0, 0));
}
}
You need to create a new renderer and specify that to the plot.
First, you'll create a render that has the two colors you want:
CategoryItemRenderer renderer = new StackedBarRenderer {
private Paint[] colors = new Paint[] {
new Color(255, 0, 0),
new Color(0, 0, 255)
}
public Paint getSeriesPaint(int series) {
return colors[(series % 2)];
}
};
Then, grab the plot and specify which render you want to use:
if(chart.getPlot() instanceof CategoryPlot){
chart.getCategoryPlot().setRenderer(renderer);
}
Hopefully, that should get you where you want to be.