get getDomainCrosshairValue on applet and open new window

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
hsinhw
Posts: 1
Joined: Wed Dec 14, 2011 12:02 pm
antibot: No, of course not.

get getDomainCrosshairValue on applet and open new window

Post by hsinhw » Wed Dec 14, 2011 12:13 pm

Hi, I have one japplet need to get the Crosshair x value in the xydataset and I use the x vale to be Parameter with the new web page.
But I get problem while I execute the japplet on web very lag, looks not normal.
Could anyone can help me to solve the problem or give me some tip to solve? thank you so much.

my html code below:

Code: Select all

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>scan page</title>
    </head>
    <body>

    <applet code="chart.layer_chart" 
            archive="dist/java_japplet.jar, dist/lib/jcommon-1.0.16.jar, dist/lib/jfreechart-1.0.13.jar"
            width="1024" height="300">
        <param name="filename" value="mzxml_tets">
        <param name="web_address_string" value="http://10.69.10.10/layer_page.jsp" />
        <param name="x_value" value="[0.5408, 2.5699, 4.5756, 6.617, 8.7029]" />
        <param name="y_value" value="[9767.84, 9544.26, 7903.15, 10426.5, 9388.57]" />

    </applet>
  


</body>
</html>

and my japplet code below:

Code: Select all

package chart;

import java.applet.AppletContext;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.event.ChartProgressEvent;
import org.jfree.chart.event.ChartProgressListener;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.RectangleEdge;

public class layer_chart extends JApplet
        implements ChartMouseListener, ChartProgressListener {

    static ArrayList<Float> x;
    static ArrayList<Float> y;
    static String sample_name;
    private JFreeChart jfreechart;
    private ChartPanel chartpanel;
    private String web_address_string;

    public void init() {


        //get from param
        String x_str = this.getParameter("x_value");
        String y_str = this.getParameter("y_value");
        sample_name = this.getParameter("filename");
        web_address_string = this.getParameter("web_address_string");

        //app demo
//        String x_str = "[2545.15, 1051.2151, 2757.00455, 2525.1501518, 4052.051174]";
//        String y_str = "[4241.515, 1420.505, 1515.005, 1515.1, 1510.128]";
//        sample_name = "listen_example";
//        web_address_string = "http://5.5.5.5/layer_page.jsp";


        /*
         * data
         */
        x = new ArrayList<Float>();
        y = new ArrayList<Float>();

        StringTokenizer stk = new StringTokenizer(x_str.substring(1, x_str.length() - 1), ",");
        while (stk.hasMoreElements()) {
            x.add(Float.parseFloat(stk.nextElement().toString()));
        }

        stk = new StringTokenizer(y_str.substring(1, y_str.length() - 1), ",");
        while (stk.hasMoreElements()) {
            y.add(Float.parseFloat(stk.nextElement().toString()));
        }
        XYDataset xydataset = createDataset();


        //init
        //this.setSize(1024, 300);


        jfreechart = ChartFactory.createXYLineChart(sample_name, "time, sec", "Base Peak", xydataset, PlotOrientation.VERTICAL, false, true, true);


        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
        numberaxis.setAutoRangeIncludesZero(false);
        numberaxis.setAutoRangeMinimumSize(1.0D);
        XYItemRenderer xyitemrenderer = xyplot.getRenderer();
        chartpanel = new ChartPanel(jfreechart);

        /*
         * 20111124 for url generator
         */
//        xyitemrenderer.setURLGenerator(new StandardXYURLGenerator("2_showAndFind.jsp",
//                "a$$$$num$$$$$4",
//                "index"));
        xyitemrenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("RT:{1} Int: {2}", new DecimalFormat("0.00"), new DecimalFormat("0")));



        /*
         * 20111124 for crosshair x and y
         */
        xyplot.setDomainCrosshairVisible(true);
        xyplot.setRangeCrosshairVisible(true);

//        chartpanel.setPreferredSize(new Dimension(1024, 300));

        /*
         * 20111124 for mouse wheel zoom
         */
        chartpanel.setMouseWheelEnabled(true);
        chartpanel.setMouseZoomable(true);

        /*
         * 20111124 for mouse listener
         */
        chartpanel.addChartMouseListener(this);
        jfreechart.addProgressListener(this);
        
        setContentPane(chartpanel);

    }

    public void chartMouseClicked(ChartMouseEvent event) {
        int mouseX = event.getTrigger().getX();
        int mouseY = event.getTrigger().getY();
//        System.out.println("x = " + mouseX + ", y = " + mouseY);
        Point2D p = this.chartpanel.translateScreenToJava2D(
                new Point(mouseX, mouseY));
        XYPlot plot = (XYPlot) this.jfreechart.getPlot();

        /*
        Returns the value of the crosshair for the range axis. Note that this value is recalculated as the
        chart is repainted, so you should only rely on this value if you know that the chart has nished
        painting. In particular, this method will return the old value if you call it from within a mouse
        click event handler. You can use a ChartProgressListener to determine when chart painting
        has completed|see CrosshairDemo1.java for an example.
        å
         */
//        System.out.println("corsshair x = " + plot.getDomainCrosshairValue() + " tets_x = " + test_x + " , y = " + plot.getRangeCrosshairValue());


        ChartRenderingInfo info = this.chartpanel.getChartRenderingInfo();
        Rectangle2D dataArea = info.getPlotInfo().getDataArea();

        ValueAxis domainAxis = plot.getDomainAxis();
        RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
        ValueAxis rangeAxis = plot.getRangeAxis();
        RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
        double chartX = domainAxis.java2DToValue(p.getX(), dataArea,
                domainAxisEdge);
        double chartY = rangeAxis.java2DToValue(p.getY(), dataArea,
                rangeAxisEdge);
//        System.out.println("Chart: x = " + chartX + ", y = " + chartY);
    }

    public void chartMouseMoved(ChartMouseEvent cme) {
        //no implements
    }

    private static XYDataset createDataset() {
        XYSeries xyseries = new XYSeries("");


        for (int i = 0; i < x.size(); i++) {
            xyseries.add(x.get(i), y.get(i));
        }

        XYSeriesCollection xyseriescollection = new XYSeriesCollection(xyseries);
        return xyseriescollection;
    }

    public void chartProgress(ChartProgressEvent chartprogressevent) {

        /*
        public static final int DRAWING_STARTED = 1;
        public static final int DRAWING_FINISHED = 2;
         */
        if (chartprogressevent.getType() != 2) {
            return;
        }
        if (chartpanel != null) {
            jfreechart = chartpanel.getChart();
            if (jfreechart != null) {
                XYPlot xyplot = jfreechart.getXYPlot();
                XYDataset xydataset = xyplot.getDataset();
                Comparable comparable = xydataset.getSeriesKey(0);

                //no use
                System.out.println("cp corsshair x = " + xyplot.getDomainCrosshairValue() + ", y = " + xyplot.getRangeCrosshairValue());
                AppletContext a = getAppletContext();
                URL url;
                try {
                    //url = new URL("peak_list.jsp?table_name=" + sample_name + "&" + xyplot.getDomainCrosshairValue());
                    web_address_string = web_address_string.substring(0, web_address_string.indexOf("layer_page.jsp"));
                    url = new URL(web_address_string + "peaklist.jsp?" + xyplot.getDomainCrosshairValue());
                    a.showDocument(url, "_blank");
                } catch (MalformedURLException ex) {
                    Logger.getLogger(layer_chart.class.getName()).log(Level.SEVERE, null, ex);
                }

//                    model.setValueAt(comparable, 0, 0);
//                    long l = (long)d;
//                    model.setValueAt(new Long(l), 0, 1);
//                    int i = series.getIndex(new Minute(new Date(l)));
//                    if(i >= 0)
//                    {
//                        TimeSeriesDataItem timeseriesdataitem = series.getDataItem(Math.min(199, Math.max(0, i)));
//                        
//                        long l1 = timeseriesdataitem.getPeriod().getMiddleMillisecond();
//                        double d1 = timeseriesdataitem.getValue().doubleValue();
//
//                        model.setValueAt(new Long(l1), 0, 1);
//                        model.setValueAt(new Double(d1), 0, 2);
//
//                    }
                
                
            }
        }
    }
}

plz help me, thank you .

ps.
1) I already try not many data point, so I think it's not dataset huge problem.(on online dataset, one chart content about 18000 xy date point at last).

Locked