In the following code, I created a simple histogram chart with displaying total counts of 2 barSeries. If you run the code below, you will see that there are orange colors slightly on the top of the 2nd and 4th bars. This could misinform that there are data for series1 on 2nd and 4th bars. If I set the variable 'hasValue' false in my code below and run it, then I don't see the orange colors on them any more. However, there are 2 missing labels on the 2nd and 4th bars.
Here is my question. How can I display all labels on the top of the bars without displaying colors for 0 data? Note that I don't want to display a label when the total count is 0.
Code: Select all
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(600, 600);
TChart chart = new TChart();
panel.add(chart);
Bar b = new Bar(chart.getChart());
b.add(1.0, 10.0);
b.add(2.0, 20.0);
b.add(3.0, 30.0);
b.add(4.0, 40.0);
b.add(5.0, 0.0);
b.setMultiBar(MultiBars.STACKED);
b.setBarWidthPercent(100);
b.getMarks().setVisible(false);
boolean hasValue = true;
Bar b1 = new Bar(chart.getChart());
b1.add(1.0, 10.0);
if(hasValue){
b1.add(2.0, 0);
}else{
b1.add(2.0, 0, Color.TRANSPARENT);
}
b1.add(3.0, 30.0);
if(hasValue){
b1.add(4.0, 0);
}else{
b1.add(4.0, 0, Color.TRANSPARENT);
}
b1.add(5.0, 50.0);
b1.setMultiBar(MultiBars.STACKED);
b1.setBarWidthPercent(100);
int[] totalMarkCount = new int[b1.getCount()];
for(int i=0; i<b.getCount(); i++){
totalMarkCount[i] += b.getMarkValue(i);
}
for(int i=0; i<b1.getCount(); i++){
totalMarkCount[i] += b1.getMarkValue(i);
}
for(int i=0; i<totalMarkCount.length; i++){
System.out.println("total count: " + totalMarkCount[i]);
}
//move int to StringList
StringList labelList = new StringList(b1.getCount());
for(int i=0; i<b1.getCount(); i++){
labelList.add(i, Integer.toString(totalMarkCount[i]));
}
//reset new total count to b1
b1.setLabels(labelList);
chart.getAxes().getBottom().getLabels().setStyle(AxisLabelStyle.VALUE);
//set 2D
chart.getAspect().setView3D(false);
frame.add(panel);
frame.setSize(600, 600);
frame.setVisible(true);
}