Edit value of chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
filipefcl
Posts: 3
Joined: Thu May 07, 2015 8:58 pm
antibot: No, of course not.

Edit value of chart

Post by filipefcl » Thu May 07, 2015 9:03 pm

I want edit a value of line chart by clicking and dragging, this is possible ?

Thanks.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Edit value of chart

Post by paradoxoff » Sat May 09, 2015 1:02 pm

In general, yes.
What type of Plot do you use?

filipefcl
Posts: 3
Joined: Thu May 07, 2015 8:58 pm
antibot: No, of course not.

Re: Edit value of chart

Post by filipefcl » Mon May 11, 2015 8:08 pm

This is my code, to create a line chart:

Code: Select all

 public static JFreeChart createChart(final XYDataset dataset, String title) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart(
            title,      // chart title
            "",                      // x axis label
            "",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            false,                     // include legend
            true,                     // tooltips
            false                     // urls
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);

        //final StandardLegend legend = (StandardLegend) chart.getLegend();
        //legend.setDisplaySeriesShapes(true);
        
        // get a reference to the plot for further customisation...
        final XYPlot plot = chart.getXYPlot();
        
        //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.BLACK);
        plot.setRangeGridlinePaint(Color.BLACK);
        
        plot.setBackgroundPaint(Color.WHITE);
        
        //final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        //renderer.setSeriesLinesVisible(0, false);
        //renderer.setSeriesShapesVisible(1, false);
        //plot.setRenderer(renderer);

        //Oculta a borda.
        plot.setOutlineVisible(false);
        chart.setBorderVisible(false);
        
        //Oculta os eixos.
        plot.getDomainAxis().setVisible(false); //Oculta o eixo X.
                
        chart.removeLegend();
        chart.clearSubtitles();
        
        return chart; 
    }

And here I get the mouse click:

Code: Select all

chartPanel.addChartMouseListener(new Edit_Chart.MyChartMouseListener());

Code: Select all

private static class MyChartMouseListener implements ChartMouseListener {

        public MyChartMouseListener() {
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            ChartEntity entity = event.getEntity();
            if (entity == null) {
                return;
            }
            if (entity instanceof XYItemEntity) {
                // Get entity details
                String tooltip = ((XYItemEntity) entity).getToolTipText();
                XYDataset dataset = ((XYItemEntity) entity).getDataset();
                int seriesIndex = ((XYItemEntity) entity).getSeriesIndex();
                int item = ((XYItemEntity) entity).getItem();

                // You have the dataset the data point belongs to, the index of the series in that dataset of the data point, and the specific item index in the series of the data point.
                XYSeries series = ((XYSeriesCollection) dataset).getSeries(seriesIndex);
                XYDataItem xyItem = series.getDataItem(item);

                xyItem.setY(xyItem.getYValue()-1);
            }
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            
        }
    }

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Edit value of chart

Post by paradoxoff » Tue May 12, 2015 7:24 am

Is this a solution for your problem or a better description?

filipefcl
Posts: 3
Joined: Thu May 07, 2015 8:58 pm
antibot: No, of course not.

Re: Edit value of chart

Post by filipefcl » Tue May 12, 2015 12:34 pm

It is a better description. In this code I get a value when I click, and subtract -1. But what I need is click and drag changing the value.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Edit value of chart

Post by paradoxoff » Tue May 12, 2015 9:43 pm

I am afraid that you will have to implement your logic in a custom MouseListener that you add to the ChartPanel.
Everything that is happening inside the mouseClicked(MouseEvent me) and mouseDragged(MouseEvent me) methods of ChartPanel itself should also be doable in an "external" MouseListener, namely the following actions:
- get coordinates of the MouseEvent
- convert the mouse coordinates to data coordinates of the JFreeChart by taking the insets of teh ChartPanel and its scale factors into account
- check whether the ChartRenderingInfo of the ChartPanel has an entity at the data coordinates
- finally, determine the entity type and use the rest of the logic that you have already implementedl.

Locked