iknow34languages
iknow34languages

Reputation: 183

How to display the value from chart x axis(Timeseries) as format HHMMSS?

Now ,i create a chart ,and show the x axis with HHMMSS;

enter image description here

then i want to show a tip(picture shows) when mouse move on :

CODE:

chartPanel.addChartMouseListener(new ChartMouseListener() {

    @Override
    public void chartMouseClicked(ChartMouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void chartMouseMoved(ChartMouseEvent arg0) {
        // TODO Auto-generated method stub
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        JFreeChart chart = arg0.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        ValueAxis xAxis = plot.getDomainAxis();
        double x = xAxis.java2DToValue(arg0.getTrigger().getX(), dataArea, 
                RectangleEdge.BOTTOM);
        double y = DatasetUtilities.findYValue(plot.getDataset(), 0, x);
        xCrosshair.setValue(x);
        yCrosshair.setValue(y);

    }

});

Double X get a double value

double x = xAxis.java2DToValue(arg0.getTrigger().getX(), dataArea, 
                RectangleEdge.BOTTOM);

and set value with

xCrosshair.setValue(x);

So it will show the double value ,and how can i show the value with the "hhmmss"?

Upvotes: 1

Views: 41

Answers (1)

iknow34languages
iknow34languages

Reputation: 183

I find a way to solve this problem :

use the setLabelGenerator in a Crossshair;

xCrosshair.setLabelGenerator(new CrosshairLabelGenerator() {
    @Override
    public String generateLabel(final Crosshair ch) {               
        Double timevalue = ch.getValue();
        long ltimevalue = ((long) (timevalue*1 ));
        Date itemDate = new Date(ltimevalue);
        String myDateStr = new SimpleDateFormat("HHmmss").format(itemDate);
        return myDateStr;
    }
});

Upvotes: 1

Related Questions