Version:<
jfreechart-0.9.21>
Exactly what I am looking for...
Actually I want to develop some trendline and arrow as well.
Something like
http://www.prophet.net/analyze/javachar ... false&null
Almost all the chart features already done. Now only left the draw
trendline & arrow part. If u see from the Java Applet in this URL, u will find it very easy to draw line... But after I decompile the source Jar file, it already obfuscated and due to some reason, I think I override the jfreechart method is faster & more suitable then searching method inside the 'JavaChartNew.jar' (prophet.net) on how to draw trendline...(lol)
From ur fast reply, seems that we face the same problem(zooming circumstances: because the line u draw is on the x,y position but not the X(Timeseries) & Y(ValueAxis), hence zooming only apply to dataset but not the trendline)
I also saw ur conversation with DG about this issue on other topic...
If I am not mistaken, ur solution on this issue is related to these following steps:(for reference for other SE that face the same problem)
1) User Click on 'draw TrendLines' button/image
2) User Click on the grid, then the chartPanel class mousePressed events handler will get the X,Y of the mouse click position.
2) convert to java2D(???), and override the paint method to draw out the initial x,y point. (Or create a new class 'SupportLines/TrendLine' to handle the stroke, paint activities)
3)Then follow by user dragging events. Upon user release the mouse, dragging activities complete, then keep track on its last x,y position in order to draw a line from starting point to end point. (Drawing line using Line2D.Double(...). save the final points in 'SupportLine' class.
4)Save all lines drawn by user(vector of supportline) so that we can draw these lines on paintComponent. If not, the lines will be gone on repaint events (Just like when user click undo/clear all trendline-we repaint it to clear without storing any location of trendline). Or another workaround is to detect whether the user had click the clear all trendline button (Bollean flag), then if yes, the boolean is false, the repaint activities will not draw/paint on the trendline.(Virtually end user thought is that trendline is erased.)
5)Next, override paintComponent of chartPanel and get line from vector and draw all lines on chart on repaint.
6)MMM...because I am only a beginner(hope u don't laugh on my little opinion). I think the workaround to solve the zooming part is try to override the dataset/dataseries in the relevant class then use the annotation to draw the lines out using overlaid class.
plz don't laugh...this is only my little opinion...
From the documentation that I decompile the class and research on other topic in jfreechart forum...Maybe this class can work. Because annotation will no zooming problem (it will follow the data to be magnifying).
Here is the demo classes from the 'jfreechart-0.9.21-demo.jar'... ()
i)JOverlaidXYPlotDemo1.class (shown below-for ref. to whom need)
ii)JOverlaidXYPlotDemo2.class (shown below-for ref. to whom need)
iii)JRegressionDemo1.class (shown below-for ref. to whom need)
iv)JPlotOrientationDemo1.class(inlcuded in demo)
v)JPlotOrientationDemo2.class(included in demo)
JOverlaidXYPlotDemo1
---------------------------
Code: Select all
package demo;
import java.awt.Dimension;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.time.*;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class JOverlaidXYPlotDemo1 extends ApplicationFrame
{
public JOverlaidXYPlotDemo1(String s)
{
super(s);
JFreeChart jfreechart = createOverlaidChart();
ChartPanel chartpanel = new ChartPanel(jfreechart, true, true, true, true, true);
chartpanel.setMouseZoomable(true,false);
chartpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartpanel);
}
private JFreeChart createOverlaidChart()
{
IntervalXYDataset intervalxydataset = createDataset1();
XYBarRenderer xybarrenderer = new XYBarRenderer(0.20000000000000001D);
xybarrenderer.setToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
DateAxis dateaxis = new DateAxis("Date");
dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
NumberAxis numberaxis = new NumberAxis("Value");
XYPlot xyplot = new XYPlot(intervalxydataset, dateaxis, numberaxis, xybarrenderer);
double d = (new Day(9, 3, 2002)).getMiddleMillisecond();
XYTextAnnotation xytextannotation = new XYTextAnnotation("Hello!", d, 10000D);
xytextannotation.setFont(new Font("SansSerif", 0, 9));
xyplot.addAnnotation(xytextannotation);
XYDataset xydataset = createDataset2();
StandardXYItemRenderer standardxyitemrenderer = new StandardXYItemRenderer();
standardxyitemrenderer.setToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
xyplot.setDataset(1, xydataset);
xyplot.setRenderer(1, standardxyitemrenderer);
xyplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
}
private IntervalXYDataset createDataset1()
{
TimeSeries timeseries = new TimeSeries("Series 1", org.jfree.data.time.Day.class);
timeseries.add(new Day(1, 3, 2002), 12353.299999999999D);
timeseries.add(new Day(2, 3, 2002), 13734.4D);
timeseries.add(new Day(3, 3, 2002), 14525.299999999999D);
timeseries.add(new Day(4, 3, 2002), 13984.299999999999D);
timeseries.add(new Day(5, 3, 2002), 12999.4D);
timeseries.add(new Day(6, 3, 2002), 14274.299999999999D);
timeseries.add(new Day(7, 3, 2002), 15943.5D);
timeseries.add(new Day(8, 3, 2002), 14845.299999999999D);
timeseries.add(new Day(9, 3, 2002), 14645.4D);
timeseries.add(new Day(10, 3, 2002), 16234.6D);
timeseries.add(new Day(11, 3, 2002), 17232.299999999999D);
timeseries.add(new Day(12, 3, 2002), 14232.200000000001D);
timeseries.add(new Day(13, 3, 2002), 13102.200000000001D);
timeseries.add(new Day(14, 3, 2002), 14230.200000000001D);
timeseries.add(new Day(15, 3, 2002), 11235.200000000001D);
return new TimeSeriesCollection(timeseries);
}
private XYDataset createDataset2()
{
TimeSeries timeseries = new TimeSeries("Series 2", org.jfree.data.time.Day.class);
timeseries.add(new Day(3, 3, 2002), 16853.200000000001D);
timeseries.add(new Day(4, 3, 2002), 19642.299999999999D);
timeseries.add(new Day(5, 3, 2002), 18253.5D);
timeseries.add(new Day(6, 3, 2002), 15352.299999999999D);
timeseries.add(new Day(7, 3, 2002), 13532D);
timeseries.add(new Day(8, 3, 2002), 12635.299999999999D);
timeseries.add(new Day(9, 3, 2002), 13998.200000000001D);
timeseries.add(new Day(10, 3, 2002), 11943.200000000001D);
timeseries.add(new Day(11, 3, 2002), 16943.900000000001D);
timeseries.add(new Day(12, 3, 2002), 17843.200000000001D);
timeseries.add(new Day(13, 3, 2002), 16495.299999999999D);
timeseries.add(new Day(14, 3, 2002), 17943.599999999999D);
timeseries.add(new Day(15, 3, 2002), 18500.700000000001D);
timeseries.add(new Day(16, 3, 2002), 19595.900000000001D);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
return timeseriescollection;
}
public static void main(String args[])
{
JOverlaidXYPlotDemo1 JOverlaidXYPlotDemo1 = new JOverlaidXYPlotDemo1("Overlaid XYPlot Demo");
JOverlaidXYPlotDemo1.pack();
RefineryUtilities.centerFrameOnScreen(JOverlaidXYPlotDemo1);
JOverlaidXYPlotDemo1.setVisible(true);
}
}
JOverlaidXYPlotDemo2
--------------------------
Code: Select all
package demo;
import java.awt.Dimension;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.time.*;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class JOverlaidXYPlotDemo2 extends ApplicationFrame
{
public JOverlaidXYPlotDemo2(String s)
{
super(s);
JFreeChart jfreechart = createOverlaidChart();
ChartPanel chartpanel = new ChartPanel(jfreechart, true, true, true, true, true);
chartpanel.setMouseZoomable(true,false);
chartpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartpanel);
}
private JFreeChart createOverlaidChart()
{
DateAxis dateaxis = new DateAxis("Date");
dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
NumberAxis numberaxis = new NumberAxis("Value");
IntervalXYDataset intervalxydataset = createDataset1();
XYBarRenderer xybarrenderer = new XYBarRenderer(0.20000000000000001D);
xybarrenderer.setToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
XYPlot xyplot = new XYPlot(intervalxydataset, dateaxis, numberaxis, xybarrenderer);
double d = (new Day(9, 3, 2002)).getMiddleMillisecond();
XYTextAnnotation xytextannotation = new XYTextAnnotation("Hello!", d, 10000D);
xytextannotation.setFont(new Font("SansSerif", 0, 9));
xyplot.addAnnotation(xytextannotation);
NumberAxis numberaxis1 = new NumberAxis("Value 2");
xyplot.setRangeAxis(1, numberaxis1);
XYDataset xydataset = createDataset2A();
StandardXYItemRenderer standardxyitemrenderer = new StandardXYItemRenderer();
standardxyitemrenderer.setToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
xyplot.setDataset(1, xydataset);
xyplot.setRenderer(1, standardxyitemrenderer);
XYDataset xydataset1 = createDataset2B();
xyplot.setDataset(2, xydataset1);
xyplot.setRenderer(2, new StandardXYItemRenderer());
xyplot.mapDatasetToRangeAxis(2, 1);
xyplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
xyplot.setOrientation(PlotOrientation.VERTICAL);
return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
}
private IntervalXYDataset createDataset1()
{
TimeSeries timeseries = new TimeSeries("Series 1", org.jfree.data.time.Day.class);
timeseries.add(new Day(1, 3, 2002), 12353.299999999999D);
timeseries.add(new Day(2, 3, 2002), 13734.4D);
timeseries.add(new Day(3, 3, 2002), 14525.299999999999D);
timeseries.add(new Day(4, 3, 2002), 13984.299999999999D);
timeseries.add(new Day(5, 3, 2002), 12999.4D);
timeseries.add(new Day(6, 3, 2002), 14274.299999999999D);
timeseries.add(new Day(7, 3, 2002), 15943.5D);
timeseries.add(new Day(8, 3, 2002), 14845.299999999999D);
timeseries.add(new Day(9, 3, 2002), 14645.4D);
timeseries.add(new Day(10, 3, 2002), 16234.6D);
timeseries.add(new Day(11, 3, 2002), 17232.299999999999D);
timeseries.add(new Day(12, 3, 2002), 14232.200000000001D);
timeseries.add(new Day(13, 3, 2002), 13102.200000000001D);
timeseries.add(new Day(14, 3, 2002), 14230.200000000001D);
timeseries.add(new Day(15, 3, 2002), 11235.200000000001D);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
return timeseriescollection;
}
private XYDataset createDataset2A()
{
TimeSeries timeseries = new TimeSeries("Series 2", org.jfree.data.time.Day.class);
timeseries.add(new Day(3, 3, 2002), 16853.200000000001D);
timeseries.add(new Day(4, 3, 2002), 19642.299999999999D);
timeseries.add(new Day(5, 3, 2002), 18253.5D);
timeseries.add(new Day(6, 3, 2002), 15352.299999999999D);
timeseries.add(new Day(7, 3, 2002), 13532D);
timeseries.add(new Day(8, 3, 2002), 12635.299999999999D);
timeseries.add(new Day(9, 3, 2002), 13998.200000000001D);
timeseries.add(new Day(10, 3, 2002), 11943.200000000001D);
timeseries.add(new Day(11, 3, 2002), 16943.900000000001D);
timeseries.add(new Day(12, 3, 2002), 17843.200000000001D);
timeseries.add(new Day(13, 3, 2002), 16495.299999999999D);
timeseries.add(new Day(14, 3, 2002), 17943.599999999999D);
timeseries.add(new Day(15, 3, 2002), 18500.700000000001D);
timeseries.add(new Day(16, 3, 2002), 19595.900000000001D);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
return timeseriescollection;
}
private XYDataset createDataset2B()
{
TimeSeries timeseries = new TimeSeries("Series 2B", org.jfree.data.time.Day.class);
timeseries.add(new Day(3, 3, 2002), 43.899999999999999D);
timeseries.add(new Day(4, 3, 2002), 72.599999999999994D);
timeseries.add(new Day(5, 3, 2002), 89.400000000000006D);
timeseries.add(new Day(6, 3, 2002), 23.800000000000001D);
timeseries.add(new Day(7, 3, 2002), 45D);
timeseries.add(new Day(8, 3, 2002), 65.799999999999997D);
timeseries.add(new Day(9, 3, 2002), 92.099999999999994D);
timeseries.add(new Day(10, 3, 2002), 84.700000000000003D);
timeseries.add(new Day(11, 3, 2002), 77.200000000000003D);
timeseries.add(new Day(12, 3, 2002), 65.099999999999994D);
timeseries.add(new Day(13, 3, 2002), 78.5D);
timeseries.add(new Day(14, 3, 2002), 75.299999999999997D);
timeseries.add(new Day(15, 3, 2002), 69.900000000000006D);
timeseries.add(new Day(20, 3, 2002), 56.600000000000001D);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE);
return timeseriescollection;
}
public static void main(String[] args)
{
JOverlaidXYPlotDemo2 JOverlaidXYPlotDemo2 = new JOverlaidXYPlotDemo2("Overlaid XYPlot Demo");
JOverlaidXYPlotDemo2.pack();
RefineryUtilities.centerFrameOnScreen(JOverlaidXYPlotDemo2);
JOverlaidXYPlotDemo2.setVisible(true);
}
}
JRegressionDemo1
----------------------
Code: Select all
package demo;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JTabbedPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.function.LineFunction2D;
import org.jfree.data.function.PowerFunction2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.statistics.Regression;
import org.jfree.data.xy.*;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class JRegressionDemo1 extends ApplicationFrame
{
private XYDataset data1;
public JRegressionDemo1(String s)
{
super(s);
data1 = createSampleData1();
getContentPane().add(createContent());
}
private JTabbedPane createContent()
{
JTabbedPane jtabbedpane = new JTabbedPane();
jtabbedpane.add("Linear", createChartPanel1());
jtabbedpane.add("Power", createChartPanel2());
return jtabbedpane;
}
private ChartPanel createChartPanel1()
{
NumberAxis numberaxis = new NumberAxis("X");
numberaxis.setAutoRangeIncludesZero(false);
NumberAxis numberaxis1 = new NumberAxis("Y");
numberaxis1.setAutoRangeIncludesZero(false);
StandardXYItemRenderer standardxyitemrenderer = new StandardXYItemRenderer(1);
standardxyitemrenderer.setShapesFilled(true);
XYPlot xyplot = new XYPlot(data1, numberaxis, numberaxis1, standardxyitemrenderer);
double ad[] = Regression.getOLSRegression(data1, 0);
LineFunction2D linefunction2d = new LineFunction2D(ad[0], ad[1]);
XYDataset xydataset = DatasetUtilities.sampleFunction2D(linefunction2d, 2D, 11D, 100, "Fitted Regression Line");
xyplot.setDataset(1, xydataset);
DefaultXYItemRenderer defaultxyitemrenderer = new DefaultXYItemRenderer();
defaultxyitemrenderer.setSeriesPaint(0, Color.blue);
xyplot.setRenderer(1, defaultxyitemrenderer);
JFreeChart jfreechart = new JFreeChart("Linear Regression", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
ChartPanel chartpanel = new ChartPanel(jfreechart, false);
chartpanel.setMouseZoomable(true,false);
return chartpanel;
}
private ChartPanel createChartPanel2()
{
NumberAxis numberaxis = new NumberAxis("X");
numberaxis.setAutoRangeIncludesZero(false);
NumberAxis numberaxis1 = new NumberAxis("Y");
numberaxis1.setAutoRangeIncludesZero(false);
StandardXYItemRenderer standardxyitemrenderer = new StandardXYItemRenderer(1);
standardxyitemrenderer.setShapesFilled(true);
XYPlot xyplot = new XYPlot(data1, numberaxis, numberaxis1, standardxyitemrenderer);
double ad[] = Regression.getPowerRegression(data1, 0);
PowerFunction2D powerfunction2d = new PowerFunction2D(ad[0], ad[1]);
XYDataset xydataset = DatasetUtilities.sampleFunction2D(powerfunction2d, 2D, 11D, 100, "Fitted Regression Line");
DefaultXYItemRenderer defaultxyitemrenderer = new DefaultXYItemRenderer();
defaultxyitemrenderer.setSeriesPaint(0, Color.blue);
xyplot.setDataset(1, xydataset);
xyplot.setRenderer(1, defaultxyitemrenderer);
JFreeChart jfreechart = new JFreeChart("Power Regression", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
ChartPanel chartpanel = new ChartPanel(jfreechart, false);
chartpanel.setMouseZoomable(true,false);
return chartpanel;
}
private XYDataset createSampleData1()
{
XYSeries xyseries = new XYSeries("Series 1");
xyseries.add(2D, 56.270000000000003D);
xyseries.add(3D, 41.32D);
xyseries.add(4D, 31.449999999999999D);
xyseries.add(5D, 30.050000000000001D);
xyseries.add(6D, 24.690000000000001D);
xyseries.add(7D, 19.780000000000001D);
xyseries.add(8D, 20.940000000000001D);
xyseries.add(9D, 16.73D);
xyseries.add(10D, 14.210000000000001D);
xyseries.add(11D, 12.44D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection(xyseries);
return xyseriescollection;
}
public static void main(String args[])
{
JRegressionDemo1 JRegressionDemo1 = new JRegressionDemo1("Regression Demo 1");
JRegressionDemo1.pack();
RefineryUtilities.centerFrameOnScreen(JRegressionDemo1);
JRegressionDemo1.setVisible(true);
}
}
These 3 classes can be founded in the 'jfreechart-0.9.21-demo.jart',
unzip it and decompile the classes:
JOverlaidXYPlotDemo1,JOverlaidXYPlotDemo2, JRegressionDemo1
Develop, if possible, can u provide me a simple demo class along with the few override class like chartPanel.class, supportLine.class and etc. I hope I can contribute in modifying the code to solve the zooming fault, then I will republish a new simple demo on this trendline program demo for other SE to use it...
Thanks in advance.
regards,
wei wei.