As the previous topic illustrates, data points legends are displayed
next to the top of a bar series bars. However, in certain conditions, as a narrow
chart or a bar series with a large number of values, data points legends may
overlap each other, and the solution is to display the legends inside the bars.
The method BarSerie.setMarkLegendPosition(int markLegendPosition) can be
used to place legends inside the bars, horizontally or vertically oriented. This
method can be passed three constants:
import javax.swing.*; import java.awt.*; import com.jinsight.jetchart.*; public class Main extends JFrame { public Main() { Graph graph=new Graph(); String[] labels={"label1","label2","label3","label4"}; graph.setLabels(labels); GraphSet graphSet=graph.getGraphSet(0); Grid grid=graphSet.getGrid(); grid.setEnabled(true); grid.setColor(Color.gray); String[] title={"The JetChart Library","Setting the legend position of bar series data points"}; graph.setTitle(title); Container ct=getContentPane(); ct.add("Center",graph); BarSerie bs1=new BarSerie(); bs1.setTitle("Bar series 1"); bs1.setColor(Color.yellow); bs1.setWidth(45); double[] values1={100,130,90,110}; bs1.setValues(values1); bs1.setMarkLegendEnabled(true); bs1.setMarkLegendPosition(BarSerie.INSIDE); bs1.setMarkLegendRoomCheckEnabled(true); bs1.setValueFormat("$ ###,###"); BarSerie bs2=new BarSerie(); bs2.setTitle("Bar series 2"); bs2.setColor(Color.cyan); bs2.setWidth(15); double[] values2={50,70,55,70}; bs2.setValues(values2); bs2.setMarkLegendEnabled(true); bs2.setMarkLegendPosition(BarSerie.INSIDE_VERTICAL); bs2.setMarkLegendRoomCheckEnabled(true); bs2.setValueFormat("$ ###,###"); graph.addSerie(bs1); graph.addSerie(bs2); setSize(500,400); setVisible(true); } public static void main(String[] args) { new Main(); } }