How to add a Button to the Demo Applications!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Kishore

How to add a Button to the Demo Applications!

Post by Kishore » Wed Oct 30, 2002 1:31 pm

Hi,
Now i need to add the Existing Application(someDemo.java) when the button clicked.i have achieved that but i need to add a Button at the end of the frame(or Jdialog what ever it is).
here is My Source Code:
_____________________
public class Test extends JDialog {

XYDataset data,data2;

public Test(String title) {

// super(title);

XYSeries series = new XYSeries("Test Data");
series.add(1, 1.0);
series.add(2, 1.1);
series.add(3, 1.3);
series.add(4, 1.5);
series.add(5, 1.7);
series.add(6, 1.8);
series.add(7, 1.9);
series.add(8, 2.0);
series.add(9, 2.1);
series.add(10,2.2);
series.add(11,2.3);
series.add(12,2.4);
series.add(12.1, 5.0);
series.add(12.3, 5.1);
series.add(12.5, 5.3);
series.add(13.4, 5.5);
series.add(13.8, 5.7);
series.add(14.1, 5.8);
series.add(14.7, 5.9);
series.add(15, 6.0);
series.add(15.9, 6.1);
series.add(16,6.2);
series.add(18,6.3);
series.add(20,6.4);

data = new XYSeriesCollection(series);

double [][] my2d=
{
{1,1.0},{2,1.1},{3,1.3},{4,1.5},{5,1.7},{6,1.8},{7,1.9},{8,2.0},{9,2.1},{10,2.2},{11,2.3},{12,2.4},
{12.1,5.0},{12.3,5.1},{12.5,5.3},{13.4,5.5},{13.8,5.7},{14.1,5.8},{14.7,5.9},{15,6.0},{15.9,6.1},{16,6.2},{18,6.3},{20,6.4}
};

JFreeChart chart = createOverlaidChart();
chart.getPlot().setBackgroundPaint(Color.yellow);
ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);
panel.setPreferredSize(new java.awt.Dimension(270, 270));
JButton close=new JButton("Close");
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(close,BorderLayout.SOUTH);


double[] result=Regression.getOLSRegression(data,0);
// Regression.getOLSRegression(this.data1, 0);
LineFunction2D myf=new LineFunction2D(result[0],result[1]);
data2 =DatasetUtilities.sampleFunction2D(myf,1,20,100,"regression");
}
private JFreeChart createOverlaidChart() {
// create subplot 1...
XYDataset data1 =data;
XYDotRenderer renderer1 = new XYDotRenderer();
XYPlot subplot1 = new XYPlot(data1, null, null, renderer1);

// create subplot 2...
XYItemRenderer renderer2 = new StandardXYItemRenderer();
XYPlot subplot2 = new XYPlot(data2, null, null, renderer2);
subplot2.setSeriesPaint(0, Color.green);

// make an overlaid plot and add the subplots...
ValueAxis domainAxis = new HorizontalDateAxis("X");
ValueAxis rangeAxis = new VerticalNumberAxis("Y");
OverlaidXYPlot plot = new OverlaidXYPlot(domainAxis, rangeAxis);
plot.add(subplot1);
plot.add(subplot2);

// return a new chart containing the overlaid plot...
return new JFreeChart("Linear Regression Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}
}
-------------------------------------------------------------------
i have removed main() from this and i am calling this clas from some other class, when ever (some)Button is clicked.
That code is:
-------------------------------------------------------------------------
public void actionPerformed(ActionEvent ae)
{
Test demo = new Test("Liner Regression Demo");
demo.setModal(true);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
demo.setFocusable(true);
}
----------------------------------------------------------------------------------------
how do i add a close Button to the frame at the appropriate Place.
when i run the above code, Button is is coming over the Test Data Box.
i.e I Guess, in the Demo applications the Graph is occupying the entire Frame.
how do i add a Button after Graph(.e Test Dtaa Label)..so that upon clicking this Close button ,user will comeout of the Frame(JDialog).
Hope i am clear in Expressing my Problem, if Not Please let me Know so that i can be more clear...

Thanks,
Kishore.

Dave Gilbert

Re: How to add a Button to the Demo Applications!

Post by Dave Gilbert » Wed Oct 30, 2002 6:41 pm

Hi Kishore,

In most of the demos, I extend the ApplicationFrame class, create a ChartPanel and do setContentPane(chartPanel) on the ApplicationFrame. Those frames aren't going to be very "reusable". Better to cut and paste the code that creates the ChartPanel and then put the ChartPanel into your own frame so you can control the layout better.

Regards,

DG

Kishore

Re: How to add a Button to the Demo Applications!

Post by Kishore » Thu Oct 31, 2002 5:40 am

HI DG,
Thanks.i am not using the Application Frame .i have already modified.i'm extending HDialog(u can see the code above).
but there is a cfatch here.
u have a chartpanel which has many constructors,to allow maxxium hight,max width, min and max of drawable hight,width etc...
i have changed u'r Example code to the to the code below:

ChartPanel panel = new ChartPanel(chart, 370,370,100,100,160,160,true,true, true, true, true, true);

but still there is Box containing The Test Data is coming at the Bottom.
How do i control this.
Thx,
Kishore.

Kishore

Re: How to add a Button to the Demo Applications!

Post by Kishore » Thu Oct 31, 2002 5:50 am

Hi DG,
Is The Test Data with color, which appers at the Bottom of the Frame is the Part of Drawable Hight(max height), plz tell me where is that code available.
thx,
Kishore.

Dave Gilbert

Re: How to add a Button to the Demo Applications!

Post by Dave Gilbert » Thu Oct 31, 2002 10:47 am

I think you are referring to the chart legend. It is part of the chart. You can make it disappear with:

myChart.setLegend(null);

...or you can change the position with:

myChart.getLegend().setAnchor(Legend.EAST);

Regarding the positioning of your "Close" button, I'd recommend getting your layout right first, then putting the chart in. You can do this by commenting out the ChartPanel and just replacing it with a regular Swing component (a JPanel or a JButton). When you have this in the right position, just replace it in your code with the ChartPanel. Doing this lets you focus on the layout first, without having to worry about the chart code.

Regards,

DG.

Locked