Updating the value in Series (both X and Y)...Please help...

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
manjusha0110
Posts: 10
Joined: Tue May 02, 2017 7:24 am
antibot: No, of course not.

Updating the value in Series (both X and Y)...Please help...

Post by manjusha0110 » Tue May 02, 2017 7:35 am

Hi, I am new to JFreechart. My problem is i want to update a value in series (both X and Y) when the user clicks on any point in the scatter plot and drags it around, I want the point to move.

I have implemented the ChartMouseListener, MouseListener, MouseMotionListener interface.

I have written the following code in the mousedragged event, but the point is only moving vertically. The X in the series is not getting updated.

int itemIndex = xyItemEntity.getItem();
Point pt = me.getPoint();
XYPlot xy = jfreechart.getXYPlot();
Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
Point2D p = chartPanel.translateScreenToJava2D(pt);

finalMovePointX = xy.getRangeAxis().java2DToValue(p.getX(),dataArea, xy.getRangeAxisEdge());
finalMovePointY = xy.getRangeAxis().java2DToValue(p.getY(),dataArea, xy.getRangeAxisEdge());

//System.out.println("Final move PointX" + finalMovePointX);
//System.out.println("Final move PointY" + finalMovePointY);

initialMovePointX = finalMovePointX;
initialMovePointY = finalMovePointY;

Series1.update(finalMovePointX, finalMovePointY);

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

Re: Updating the value in Series (both X and Y)...Please hel

Post by paradoxoff » Wed May 03, 2017 4:58 pm

Rewrite the calculation of finalMovePointX to

Code: Select all

finalMovePointX = xy.getDomainAxis().java2DToValue(p.getX(),dataArea, xy.getDomainAxisEdge());

manjusha0110
Posts: 10
Joined: Tue May 02, 2017 7:24 am
antibot: No, of course not.

Re: Updating the value in Series (both X and Y)...Please hel

Post by manjusha0110 » Thu May 04, 2017 5:54 am

Hi,

I did the changes but its still not working as expected. When i move a point and if there is another point with same X value, the two points coincide where as i am trying to only move the first point.

Is there a update function that i can use so that i can change the X and Y values of the series for a particular index. What i saw is there is only one function that takes index and updates the Y co-ordinate..

Please help me...I need it urgently.

My changed code looks like :-

public void mousePressed(MouseEvent e) {

int x = e.getX(); // initialized point whenenver mouse is pressed
int y = e.getY();


EntityCollection entities = this.info.getEntityCollection();
ChartMouseEvent cme = new ChartMouseEvent(jfreechart, e, entities.getEntity(x, y));
ChartEntity entity = cme.getEntity();

if ((entity != null) && (entity instanceof XYItemEntity)) {
xyItemEntity = (XYItemEntity) entity;
} else if (!(entity instanceof XYItemEntity)) {
xyItemEntity = null;
return;
}
if (xyItemEntity == null) {
return; // return if not pressed on any series point
}


Point pt = e.getPoint();
XYPlot xy = jfreechart.getXYPlot();
Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
Point2D p = chartPanel.translateScreenToJava2D(pt);



initialMovePointX = xy.getDomainAxis().java2DToValue(p.getX(), dataArea, xy.getDomainAxisEdge());
initialMovePointY = xy.getRangeAxis().java2DToValue(p.getY(), dataArea, xy.getRangeAxisEdge());


canMove = true;

chartPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));

}



public void movePoint(MouseEvent me) {

try{

if (canMove == true && isLocked == false) {
int itemIndex = xyItemEntity.getItem();
Point pt = me.getPoint();
XYPlot xy = jfreechart.getXYPlot();
Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
Point2D p = chartPanel.translateScreenToJava2D(pt);

finalMovePointX = xy.getDomainAxis().java2DToValue(p.getX(), dataArea, xy.getDomainAxisEdge());
finalMovePointY = xy.getRangeAxis().java2DToValue(p.getY(),dataArea, xy.getRangeAxisEdge());


switch (gridsz){
case 0:
if (finalMovePointX >3 || finalMovePointX < 0 ||finalMovePointY >3 || finalMovePointY < 0){
return;
}
case 1:
if (finalMovePointX > 7 || finalMovePointX < 0 ||finalMovePointY > 7 || finalMovePointY < 0){
return;
}
case 2:
if (finalMovePointX > 10 || finalMovePointX < 0 ||finalMovePointY > 10 || finalMovePointY < 0){
return;
}
}


series1.remove(itemIndex);

series1.add(finalMovePointX, finalMovePointY);

initialMovePointX = finalMovePointX;
initialMovePointY = finalMovePointY;


//series1.fireSeriesChanged();
//jfreechart.fireChartChanged();
//chartPanel.updateUI();
}
} catch(Exception e){
System.out.print(e);
}
}


public void mouseDragged(MouseEvent e) {
movePoint(e);

}

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

Re: Updating the value in Series (both X and Y)...Please hel

Post by paradoxoff » Thu May 04, 2017 7:40 pm

I never really became familiar with the XYSeries and XYSeriesCollection classes.
You might be better off with writing your own dataset implementation that allows to change the x,y values at a specific item and series index.
You oucld, for example, stuff the code for DefaultXYDataset into a new class MutableDefaultXYDataset that, like DefaultXYDataset, extends AbstractXYDataset,
and add the following method

Code: Select all

public void setX(int series, int item, double x){
        double[][] seriesData = (double[][]) this.seriesList.get(series);
        seriesData[0][item] = x;
        notifyListeners(new DatasetChangeEvent(this, this));
}
public void setY(int series, int item, double y){
        double[][] seriesData = (double[][]) this.seriesList.get(series);
        seriesData[1][item] = y;
        notifyListeners(new DatasetChangeEvent(this, this));
}

Then use your new dataset instead of the one that your are using at present, and store a reference to the dataset in your class, instead of a reference to "Series".

manjusha0110
Posts: 10
Joined: Tue May 02, 2017 7:24 am
antibot: No, of course not.

Re: Updating the value in Series (both X and Y)...Please hel

Post by manjusha0110 » Tue May 09, 2017 10:56 am

Thank you for your response

Locked