Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JFreeChart - adding 'X' mark where domain and range lines cross

Posted on 2012-09-11
1
Medium Priority
?
1,782 Views
Last Modified: 2012-09-23
Hi Experts,

I have modified AnnotationDemo1 class from Jfree demo.
From the attached image, I want to put 'X' mark instead of '+' inside of small red line circle.

Anybody has seen similar example or done it?

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.annotations.XYDrawableAnnotation;
import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

import demo.CircleDrawer;

public class AnnotationDemo1 extends ApplicationFrame
{

        public AnnotationDemo1(String s)
        {
                super(s);
                JPanel jpanel = createDemoPanel();
                jpanel.setPreferredSize(new Dimension(600, 700));
                setContentPane(jpanel);
        }

        private static XYSeriesCollection createDataset()
        {
                XYSeriesCollection xyseriescollection = new XYSeriesCollection();
                try
                {
                        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader((org.jfree.data.xy.XYSeriesCollection.class).getClassLoader().getResourceAsStream("demo/wtageinf.txt")));
                        String s = bufferedreader.readLine();
                        s = bufferedreader.readLine();
                        s = bufferedreader.readLine();
                        s = bufferedreader.readLine();
                        XYSeries xyseries = new XYSeries("P3", true, false);
                        XYSeries xyseries1 = new XYSeries("P5", true, false);
                        XYSeries xyseries2 = new XYSeries("P10", true, false);
                        XYSeries xyseries3 = new XYSeries("P25", true, false);
                        XYSeries xyseries4 = new XYSeries("P50", true, false);
                        XYSeries xyseries5 = new XYSeries("P75", true, false);
                        XYSeries xyseries6 = new XYSeries("P90", true, false);
                        XYSeries xyseries7 = new XYSeries("P95", true, false);
                        XYSeries xyseries8 = new XYSeries("P97", true, false);
                        for (String s1 = bufferedreader.readLine(); s1 != null; s1 = bufferedreader.readLine())
                        {
                                int i = Integer.parseInt(s1.substring(1, 8).trim());
                                float f = Float.parseFloat(s1.substring(9, 17).trim());
                                float f1 = Float.parseFloat(s1.substring(69, 86).trim());
                                float f2 = Float.parseFloat(s1.substring(87, 103).trim());
                                float f3 = Float.parseFloat(s1.substring(104, 122).trim());
                                float f4 = Float.parseFloat(s1.substring(123, 140).trim());
                                float f5 = Float.parseFloat(s1.substring(141, 158).trim());
                                float f6 = Float.parseFloat(s1.substring(159, 176).trim());
                                float f7 = Float.parseFloat(s1.substring(177, 193).trim());
                                float f8 = Float.parseFloat(s1.substring(194, 212).trim());
                                float f9 = Float.parseFloat(s1.substring(212, s1.length()).trim());
                                if (i == 1)
                                {
                                        xyseries.add(f, f1);
                                        xyseries1.add(f, f2);
                                        xyseries2.add(f, f3);
                                        xyseries3.add(f, f4);
                                        xyseries4.add(f, f5);
                                        xyseries5.add(f, f6);
                                        xyseries6.add(f, f7);
                                        xyseries7.add(f, f8);
                                        xyseries8.add(f, f9);
                                }
                        }

                        xyseriescollection.addSeries(xyseries);
                        xyseriescollection.addSeries(xyseries1);
                        xyseriescollection.addSeries(xyseries2);
                        xyseriescollection.addSeries(xyseries3);
                        xyseriescollection.addSeries(xyseries4);
                        xyseriescollection.addSeries(xyseries5);
                        xyseriescollection.addSeries(xyseries6);
                        xyseriescollection.addSeries(xyseries7);
                        xyseriescollection.addSeries(xyseries8);
                }
                catch (FileNotFoundException filenotfoundexception)
                {
                        System.err.println(filenotfoundexception);
                }
                catch (IOException ioexception)
                {
                        System.err.println(ioexception);
                }
                return xyseriescollection;
        }
        private static JFreeChart createChart(XYDataset xydataset)
        {
                JFreeChart jfreechart = ChartFactory.createXYLineChart(null, "Age in Months", "Weight (kg)", xydataset, PlotOrientation.VERTICAL, true, true, false);
                TextTitle texttitle = new TextTitle("Growth Charts: United States", new Font("SansSerif", 1, 14));
                TextTitle texttitle1 = new TextTitle("Weight-for-age percentiles: boys, birth to 36 months", new Font("SansSerif", 0, 11));
                jfreechart.addSubtitle(texttitle);
                jfreechart.addSubtitle(texttitle1);
                XYPlot xyplot = (XYPlot)jfreechart.getPlot();
                NumberAxis numberaxis = (NumberAxis)xyplot.getDomainAxis();
                numberaxis.setUpperMargin(0.12D);
                numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                NumberAxis numberaxis1 = (NumberAxis)xyplot.getRangeAxis();
                numberaxis1.setAutoRangeIncludesZero(false);
                XYTextAnnotation xytextannotation = null;
                Font font = new Font("SansSerif", 0, 9);
                xytextannotation = new XYTextAnnotation("3rd", 36.5D, 11.76D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("5th", 36.5D, 12.039999999999999D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("10th", 36.5D, 12.493D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("25th", 36.5D, 13.313000000000001D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("50th", 36.5D, 14.33D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("75th", 36.5D, 15.478D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("90th", 36.5D, 16.641999999999999D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("95th", 36.5D, 17.408000000000001D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                xytextannotation = new XYTextAnnotation("97th", 36.5D, 17.936D);
                xytextannotation.setFont(font);
                xytextannotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
                xyplot.addAnnotation(xytextannotation);
                
                
                Marker weightTarget = new ValueMarker(10.7);
                weightTarget.setPaint(Color.red);
                weightTarget.setLabel("Weight");
                weightTarget.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
                weightTarget.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT);
                xyplot.addRangeMarker(weightTarget);
                
                
                
                Marker ageTarget = new ValueMarker(15);
                ageTarget.setPaint(Color.orange);
                ageTarget.setLabel("Age");
                ageTarget.setLabelAnchor(RectangleAnchor.TOP_LEFT);
                ageTarget.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
                xyplot.addDomainMarker(ageTarget);

            
                CircleDrawer cd = new CircleDrawer(Color.red, new BasicStroke(1.0f), null);
                XYAnnotation bestBid = new XYDrawableAnnotation(15, 10.7, 11, 11, cd);
                xyplot.addAnnotation(bestBid);
                XYPointerAnnotation pointer = new XYPointerAnnotation("Plot", 15, 10.7,
                        3.0 * Math.PI / 4.0);

                xyplot.addAnnotation(pointer);
                
                return jfreechart;
        }
        public static JPanel createDemoPanel()
        {
                JFreeChart jfreechart = createChart(createDataset());
                return new ChartPanel(jfreechart);
        }

        public static void main(String args[])
        {
                AnnotationDemo1 annotationdemo1 = new AnnotationDemo1("Weight-for-age percentiles Growth Chart");
                annotationdemo1.pack();
                RefineryUtilities.centerFrameOnScreen(annotationdemo1);
                annotationdemo1.setVisible(true);
        }
}
[embed=file 602418]

Open in new window


thanks in advance,
growthchart.png
0
Comment
Question by:dkim18
1 Comment
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 2000 total points
ID: 38390089
I think what you want to do is change the annotation.  You still want the circle, but you want the strokes in it to be an "X" rather than a "+".

The demo class CircleDrawer is creating the circle with the + in it.  If you make your own version, you can draw whatever you want.  

Here are the basics of CircleDrawer -- you can see where it's putting in the 2 lines for the plus inside the circle:

import java.awt.*;
import java.awt.geom.*;
import org.jfree.ui.Drawable;

public class CircleDrawer
    implements Drawable
{

    public CircleDrawer(Paint paint, Stroke stroke, Paint paint1)
    {
        outlinePaint = paint;
        outlineStroke = stroke;
        fillPaint = paint1;
    }

    public void draw(Graphics2D graphics2d, Rectangle2D rectangle2d)
    {
        java.awt.geom.Ellipse2D.Double double1 = new java.awt.geom.Ellipse2D.Double(rectangle2d.getX(), rectangle2d.getY(), rectangle2d.getWidth(), rectangle2d.getHeight());
        if(fillPaint != null)
        {
            graphics2d.setPaint(fillPaint);
            graphics2d.fill(double1);
        }
        if(outlinePaint != null && outlineStroke != null)
        {
            graphics2d.setPaint(outlinePaint);
            graphics2d.setStroke(outlineStroke);
            graphics2d.draw(double1);
        }
        graphics2d.setPaint(Color.black);
        graphics2d.setStroke(new BasicStroke(1.0F));
        java.awt.geom.Line2D.Double double2 = new java.awt.geom.Line2D.Double(rectangle2d.getCenterX(), rectangle2d.getMinY(), rectangle2d.getCenterX(), rectangle2d.getMaxY());
        java.awt.geom.Line2D.Double double3 = new java.awt.geom.Line2D.Double(rectangle2d.getMinX(), rectangle2d.getCenterY(), rectangle2d.getMaxX(), rectangle2d.getCenterY());
        graphics2d.draw(double2);
        graphics2d.draw(double3);
    }

    private Paint outlinePaint;
    private Stroke outlineStroke;
    private Paint fillPaint;
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question