I have an issue with displaying the marker label outside the plot.
The program uses a categoryplot and ValueMarker is used to show the target value (60). I need to display the Marker label "Target = 60" parallel to Marker line outside the plot on right side. I tried setting Label Anchor to TOP_TIGHT and RIGHT, then the String "T" is displayed on the extreme right inside the plot instead of ""Target = 60", the remaining string "arget = 60" is not getting displayed as its not inside the plot. I could manage this by setting the label offset and could display it inside the plot on extreme right above the marker line.
Could you please help me to show the marker label outside the plot on right side, parallel to the marker line. ?
It is really urgent.
- Code: Select all
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
/**
* @author 30072386
*
*/
public class MarkerTest extends ApplicationFrame{
private static final long serialVersionUID = -2738148876035773698L;
public MarkerTest(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart();
return new ChartPanel(chart);
}
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(80.2, "Series 1", "Jan");
dataset.addValue(82.4, "Series 1", "Jan - Feb");
dataset.addValue(83.5, "Series 1", "Jan - Mar");
return dataset;
}
private static JFreeChart createChart() {
CategoryDataset dataset = createDataset();
JFreeChart chart = ChartFactory.createLineChart(
"", // chart title
"", // domain axis label
"Percent", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
false, // tooltips
false // urls
);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.getDomainAxis().setUpperMargin(0.3);
plot.setInsets(new RectangleInsets(20,20,20,20));
Marker rangeMarker = new ValueMarker(60);
float dash1[] = {10.0f};
BasicStroke dashed = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
rangeMarker.setPaint(Color.BLACK);
rangeMarker.setStroke(dashed);
rangeMarker.setLabel("Target = 60");
rangeMarker.setLabelOffset(new RectangleInsets(10,10,10,50));
rangeMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
rangeMarker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
plot.addRangeMarker(rangeMarker);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setAutoRange(false);
rangeAxis.setLowerBound(20);
rangeAxis.setUpperBound(120);
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
Shape shape = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0);
renderer.setSeriesShape(0, shape);
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setSeriesPaint(0, Color.black);
renderer.setBaseOutlinePaint(Color.black);
renderer.setUseFillPaint(true);
renderer.setSeriesLinesVisible(0, true);
renderer.setBaseFillPaint(Color.black);
plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
plot.getDomainAxis().setTickLabelsVisible(true);
plot.setBackgroundPaint(Color.white);
return chart;
}
public static void main(String[] args) {
MarkerTest demo = new MarkerTest("Range Marker Test");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
