Two charts in same frame - pan moves both of them

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Two charts in same frame - pan moves both of them

Post by chrisrhyno2003 » Fri Jun 19, 2015 1:51 am

Hi,

I am having two separate charts in the same JFrame. Both have separate Chart panels and separate plots. I have enabled panning and zoom using mouse wheel on both charts. When i press ctrl and pan one chart, the other one moves too.
The same is the case with zoom as well. When I zoom one chart, I have the other one zooming as well.

Can someone help? If you need my code, please let me know, I will post it.


Thank you,
Ashwin

Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Re: Two charts in same frame - pan moves both of them

Post by Naxter » Fri Jun 19, 2015 7:21 am

We definitely need your code. Do the ChartPanels have any connection?
I think it might be a "focus" problem, but that is hard to say without your code.

chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Re: Two charts in same frame - pan moves both of them

Post by chrisrhyno2003 » Fri Jun 19, 2015 5:11 pm

Here's my code:

class DynamicData extends JFrame
{
final TimeTableXYDataset m_incomingData;
final TimeTableXYDataset m_outgoingData;

public DynamicData(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m_incomingData = new TimeTableXYDataset();
m_outgoingData = new TimeTableXYDataset();
final JFreeChart incomingDataChart = ChartFactory.createStackedXYAreaChart(
"Incoming Paylod", "Time", "Payload", m_incomingData, PlotOrientation.VERTICAL, true, true, false);

final JFreeChart outgoingDataChart = ChartFactory.createStackedXYAreaChart(
"Outgoing Payload", "Time", "Payload", m_outgoingData, PlotOrientation.VERTICAL, true, true, false);

final StackedXYAreaRenderer render = new StackedXYAreaRenderer();
render.setSeriesPaint(0, Color.decode("#3399FF"));
render.setSeriesPaint(1, Color.decode("#CC3300"));

final StackedXYAreaRenderer render2 = new StackedXYAreaRenderer();
render2.setSeriesPaint(0, Color.decode("#3399FF"));
render2.setSeriesPaint(1, Color.decode("#CC3300"));

DateAxis domainAxis = new DateAxis("Time");
NumberAxis rangeAxis = new NumberAxis("Packet Size");

XYPlot plot1 = (XYPlot) incomingDataChart.getPlot();
plot1.setRenderer(render);
plot1.setDomainAxis(domainAxis);
plot1.setSeriesRenderingOrder(SeriesRenderingOrder.FORWARD);
plot1.setForegroundAlpha(0.5f);
plot1.setDomainPannable(true);
plot1.setRangePannable(true);
plot1.setBackgroundPaint(Color.WHITE);
plot1.setDomainGridlinesVisible(true);
plot1.setRangeGridlinesVisible(true);

XYPlot plot2 = (XYPlot) outgoingDataChart.getPlot();
plot2.setRenderer(render2);
plot2.setDomainAxis(domainAxis);
plot2.setSeriesRenderingOrder(SeriesRenderingOrder.FORWARD);
plot2.setForegroundAlpha(0.5f);
plot2.setDomainPannable(true);
plot2.setRangePannable(true);
plot2.setBackgroundPaint(Color.WHITE);
plot2.setDomainGridlinesVisible(true);
plot2.setRangeGridlinesVisible(true);

rangeAxis.setAutoRange(true);
ChartPanel chartPanel1 = new ChartPanel(incomingDataChart);
chartPanel1.setMouseWheelEnabled(true);
chartPanel1.addChartMouseListener(new ChartMouseListener() {

@Override
public void chartMouseClicked(final ChartMouseEvent event){
System.out.println("chartMouseClicked");
}

@Override
public void chartMouseMoved(final ChartMouseEvent event){
int newX = event.getTrigger().getX();
int newY = event.getTrigger().getY();
//System.out.println("chartMouseMoved to " + newX + " " + newY);
}
});
ChartPanel chartPanel2 = new ChartPanel(outgoingDataChart);
chartPanel2.setMouseWheelEnabled(true);
chartPanel2.addChartMouseListener(new ChartMouseListener() {

@Override
public void chartMouseClicked(final ChartMouseEvent event){
System.out.println("chartMouseClicked");
}

@Override
public void chartMouseMoved(final ChartMouseEvent event){
int newX = event.getTrigger().getX();
int newY = event.getTrigger().getY();
//System.out.println("chartMouseMoved to " + newX + " " + newY);
}
});
GridLayout layout = new GridLayout(2, 1);
this.setLayout(layout);
this.add(chartPanel1);
this.add(chartPanel2);
this.pack();
}

public void plotGraph(PacketData packet)
{
try
{
TimePeriod period = new Millisecond();
if(packet.getName().equals("Data Sets Received"))
this.m_incomingData.add(period, packet.getLength(), "Data");

else if(packet.getName().equals("Data Sets Sent"))
this.m_outgoingData.add(period, packet.getLength(), "Data");

else if(packet.getName().equals("RPC Received"))
this.m_incomingData.add(period, packet.getLength(), "RPC");

else if(packet.getName().equals("RPC Sent"))
this.m_outgoingData.add(period, packet.getLength(), "RPC");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

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

Re: Two charts in same frame - pan moves both of them

Post by paradoxoff » Fri Jun 19, 2015 10:16 pm

You are using your "domainAxis" in both charts. So if you change its value range by whatever action, both charts will be affected.
What I ahev also noted: you never use your variable "rangeAxis".

chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Re: Two charts in same frame - pan moves both of them

Post by chrisrhyno2003 » Fri Jun 19, 2015 10:39 pm

Thank you. It worked. I have added the range axis too.

Locked