How to change ValueAxis value dynamically ?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
ramp&s
Posts: 7
Joined: Fri Feb 27, 2009 8:19 am

How to change ValueAxis value dynamically ?

Post by ramp&s » Mon Mar 02, 2009 11:46 am

Hi,
i am facing problem while changing ValueAxis Label value dynamically.. i am able to set the label value only once.the subsequent changes are not reflecting.
please find the below code

private JFreeChart createChart() {
final JFreeChart chartTemp = ChartFactory.createLineChart(
"Analysis Graph", "Realtive Time (in Secs)", "Usage ", dataset,
PlotOrientation.VERTICAL, true, true, false);

chartTemp.setTitle("Analysis Graph");
chartTemp.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chartTemp.getPlot();

plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.black);

CategoryAxis domainAxis= (CategoryAxis) plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setLabel("Relative Time (in Secs)");

domainAxis.setLabelFont(new Font("Verdana", Font.BOLD, 12));
domainAxis.setTickLabelFont(new Font("Verdana", Font.PLAIN, 9));


ValueAxis valueAxis =(ValueAxis)plot.getRangeAxis();
valueAxis.setAutoRange(true);

valueAxis.setLabel(metric);
valueAxis.setLabelFont(new Font("Verdana", Font.PLAIN, 12));
valueAxis.setTickLabelFont(new Font("Verdana", Font.PLAIN, 9));

final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
.getRenderer();
for (int i = 0; i < 10; i++) {
renderer.setSeriesStroke(i, new BasicStroke(3f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0.9f,
new float[] { 0.9f, 1.0f }, 1.0f));
}
return chartTemp;
}

I am trying to change the value of YAxis label using the highlighted line .But my graph always showing the same label (the default values of metric )
Please correct me if am doing wrong.

Thanks!!!
Regards
Ram

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to change ValueAxis value dynamically ?

Post by david.gilbert » Mon Mar 02, 2009 4:49 pm

I think you should post a runnable code sample. It is impossible to know where the 'metric' variable is coming from (so maybe the problem lies where you set the value of 'metric').
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

ramp&s
Posts: 7
Joined: Fri Feb 27, 2009 8:19 am

Re: How to change ValueAxis value dynamically ?

Post by ramp&s » Tue Mar 03, 2009 9:07 am

Hi
metric value changing dynamically i verified that by printing that values before setting in chart

Regards
Ram

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

Re: How to change ValueAxis value dynamically ?

Post by paradoxoff » Tue Mar 03, 2009 10:51 am

You should print the value of "metric" right before you use it as axis label.
If the value of "metric" is what you expect, then there is an error in JFreeChart.
If the value of "metric" is not what it should be, then it is up to you to find out the reason.
Obviously, "metric" is an instance or class variable of the class (lets call it "ClassX" for the moment) in which you have defined your createChart() method.
I assume that it is given a default value in either the constructor or in the initialization block of ClassX.
Additionally you have the possibility to change the value of "metric", either within another method of ClassX or maybe in a totally different class. Make sure that the variable you are changing is really "metric" of ClassX and not something else! Something that I used to do frequently is to define a local variable in a method body with the same name as an instance variable, then set the value of this local variable, and then wonder why the instance variable, which I intended to change, is still the same. Maybe you are also having a case of this "variable shadowing".

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to change ValueAxis value dynamically ?

Post by david.gilbert » Tue Mar 03, 2009 10:59 am

...or maybe the axis is updating correctly, but somewhere *else* in the code it gets reset to something else. Or maybe the poster isn't actually running the code that was given, but some other code. Or maybe...who knows what? I think it is a waste of time for us to guess when the poster could simply post a self-contained example that shows the problem, as requested.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

ramp&s
Posts: 7
Joined: Fri Feb 27, 2009 8:19 am

Re: How to change ValueAxis value dynamically ?

Post by ramp&s » Thu Mar 05, 2009 3:53 pm

Hi
Herewith i am sending my sample runnable code.
Please correct me,if i am doing wrong



import java.awt.Color;
import javax.swing.plaf.metal.*;

import java.text.NumberFormat;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import java.awt.BasicStroke;


public class DemoFrame extends JApplet implements ActionListener,
WindowListener, TreeSelectionListener, MouseListener {

private Dimension screensize = null;
private Container cp = null;
private JFrame frame = null;
private JPanel chartPanel = null;
private JPanel treePanel = null;
private JPanel summaryPanel = null;
private JFreeChart chart = null;
private ChartPanel graphPanel = null;
private DefaultCategoryDataset dataset = null;
private static HashMap summaryData = new HashMap();
private JTree myTree;
private DefaultMutableTreeNode root, cpu, memory;
private String metric = new String("");
private Object[][] data = new Object[10][10];

public DemoFrame() {
super();
initComponents();
initWindow();

}


public String getMetric() {
return metric;
}

public void setMetric(String metricname) {
this.metric = metricname;
}

private void initComponents() {

frame = new JFrame("Sample JFree Chart ");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.getContentPane().add(this);
cp = getContentPane();
cp.setLayout(new FlowLayout(FlowLayout.LEFT));
screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
chartPanel = new JPanel();
summaryPanel = new JPanel();
dataset = new DefaultCategoryDataset();
}

private void initWindow() {

frame.setSize(screensize.width, screensize.height);
SwingUtilities.updateComponentTreeUI(frame);
frame.setVisible(true);
this.initFrame();
}

private void initFrame() {

this.initTreePane();
this.drawChart();
this.addToContainer();
this.addListeners();
this.drawChart();
this.setVisible(true);
}

private void initTreePane() {
treePanel = new JPanel();
root = new DefaultMutableTreeNode("Performance Metrics");
cpu = new DefaultMutableTreeNode("CPU Usage");
cpu.add(new DefaultMutableTreeNode("Total Time"));
cpu.add(new DefaultMutableTreeNode("User Time"));
cpu.add(new DefaultMutableTreeNode("System Time"));

memory = new DefaultMutableTreeNode("Memory Usage");
memory.add(new DefaultMutableTreeNode("Virtual Memory"));
memory.add(new DefaultMutableTreeNode("Free Memory"));
memory.add(new DefaultMutableTreeNode("Buffer Memory"));
memory.add(new DefaultMutableTreeNode("Cache Memory"));
root.add(cpu);
root.add(memory);
myTree = new JTree(root);
new JScrollPane(myTree);
myTree.setPreferredSize(new Dimension((screensize.width) / 9,
screensize.height + 70 - ((screensize.height) / 3)));
treePanel.add(myTree);
treePanel.setBorder(new LineBorder(java.awt.Color.LIGHT_GRAY, 2));
}

private void addToContainer() {

graphPanel.setBorder(new LineBorder(java.awt.Color.LIGHT_GRAY, 2));
chartPanel.setBorder(new LineBorder(java.awt.Color.LIGHT_GRAY, 2));
this.createDataset();
chartPanel.add(graphPanel);
chartPanel.add(summaryPanel);
cp.add(treePanel);
cp.add(chartPanel);
graphPanel.setPreferredSize(new Dimension((screensize.width - 40)
- (screensize.width / 9), (screensize.height - 565)));

chartPanel.setPreferredSize(new Dimension((screensize.width - 40)
- (screensize.width / 9), (screensize.height - 265)));

}

public void drawChart() {
chart = createChart();
graphPanel = new ChartPanel(chart);
}

private void addListeners() {
frame.addWindowListener(this);
myTree.addTreeSelectionListener(this);
}

private void createDataset() {
Random random = new Random(1000);
dataset.clear();
Double value = new Double(0.0);
for (int j = 0; j < 50; j++) {
value = random.nextDouble();

dataset.addValue(Double.valueOf(value), metric,
(Integer) ((Integer) j));
}
}

private JFreeChart createChart() {
System.out.println(" Chart " + metric);
final JFreeChart chartTemp = ChartFactory.createLineChart(
metric+"Analysis Graph", "Realtive Time ", metric, dataset,
PlotOrientation.VERTICAL, true, true, false);

chartTemp.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chartTemp.getPlot();

plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.black);

CategoryAxis domainAxis = (CategoryAxis) plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setLowerMargin(0.0);

domainAxis.setLabelFont(new Font("Times New Roman", Font.BOLD, 12));
domainAxis.setTickLabelFont(new Font("Times New Roman", Font.PLAIN, 9));

ValueAxis valueAxis = (ValueAxis) plot.getRangeAxis();
valueAxis.setAutoRange(true);

valueAxis.setLabel(metric);
valueAxis.setLabelFont(new Font("Times New Roman", Font.PLAIN, 12));
valueAxis.setTickLabelFont(new Font("Times New Roman", Font.PLAIN, 9));

final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
.getRenderer();
for (int i = 0; i < 4; i++) {
renderer.setSeriesStroke(i, new BasicStroke(2.5f,
BasicStroke.CAP_ROUND, BasicStroke.CAP_SQUARE, 0.9f,
new float[] { 0.9f, 1.0f }, 2.0f));
}
return chartTemp;
}

public void actionPerformed(ActionEvent ae) {

}

public void windowActivated(WindowEvent e) {
}

public void windowClosed(WindowEvent e) {
frame.setVisible(false);

}

public void windowClosing(WindowEvent e) {
frame.setVisible(false);
System.exit(0);
}

public void windowDeactivated(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowOpened(WindowEvent e) {
}

public void valueChanged(TreeSelectionEvent e) {

String selected = e.getPath().getLastPathComponent().toString();
this.setMetric(selected);
this.createDataset();
this.drawChart();

}

public void itemStateChanged(ItemEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

}

public static void main(String[] args) {
new DemoFrame();
System.out.println(" Chart main ");
}

}


Thanks and Regards
Ram

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

Re: How to change ValueAxis value dynamically ?

Post by paradoxoff » Thu Mar 05, 2009 6:12 pm

In your drawChart-method, exchange

Code: Select all

public void drawChart() {
chart = createChart();
graphPanel = new ChartPanel(chart);
}
with

Code: Select all

public void drawChart() {
chart = createChart();
graphPanel.setChart(chart);
}
and use code tags if you post code.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to change ValueAxis value dynamically ?

Post by david.gilbert » Thu Mar 05, 2009 9:37 pm

Good spot, thanks!
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

ramp&s
Posts: 7
Joined: Fri Feb 27, 2009 8:19 am

Re: How to change ValueAxis value dynamically ?

Post by ramp&s » Fri Mar 06, 2009 5:40 am

Hi paradoxoff,

thank you very much..now its working

Regards
Ram

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to change ValueAxis value dynamically ?

Post by david.gilbert » Fri Mar 06, 2009 9:00 am

I hope you can see the value of a self-contained demo now...
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked