How to display ValueMarker label outside the plot?

A free public discussion forum for the JFreeChart class library.

How to display ValueMarker label outside the plot?

Postby rakesh_krk » Wed Jul 28, 2010 4:43 pm

Hi

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


rakesh_krk
 
Posts: 2
Joined: Wed Jul 28, 2010 2:36 pm

Re: How to display ValueMarker label outside the plot?

Postby matinh » Thu Jul 29, 2010 9:00 am

Hi!

Markers are designed to be inside a plot. You cannot place it outside.

hth,
- martin
matinh
 
Posts: 214
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: How to display ValueMarker label outside the plot?

Postby rakesh_krk » Fri Jul 30, 2010 2:35 pm

Hi

Thanks for the reply.

If ValueMarker is not the right way to show the label outside the plot , then ,

How do i display a label outside the plot against a particular value in the Y-Axis?

In my case i have to display the label "Target=60" (for value 60 on Y-AXIS ) against marker line outside the plot on right side.

Please suggest an idea.
rakesh_krk
 
Posts: 2
Joined: Wed Jul 28, 2010 2:36 pm

Re: How to display ValueMarker label outside the plot?

Postby paradoxoff » Fri Jul 30, 2010 6:59 pm

There are two things that can keep a ValueMarker from being fully displayed:
- the value is outside the range of the respective ValueAxis. This check is performed in AbstractCategoryItemRenderer.drawRangeMarker(). if that check fails, the marker will not be drawn at all.
- part of the rendering occurs outside the dataArea. This is because rendering on the Graphics2D is clipped to the data area, which is done inside the CategoryPlot.draw() method.
To circumvent that, you can try to set the clip of the Graphics2D to null inside AbstractCategoryItemRenderer.drawRangeMarker(). Do not forget to set it back to the original clip, otherwise you might find lots of undesired graphical stuff outside the data area!
paradoxoff
 
Posts: 826
Joined: Sat Feb 17, 2007 1:51 pm


Return to JFreeChart - General

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests