How To create regression lines (standard deviation channel)
How To create regression lines (standard deviation channel)
I am using jfreechart in my application from one month but i can not found how to draw linear regression channels on any time series jfreechart. we can draw xyline annotation but here many regression lines and its a series of regression lines so please help me how to draw it? chart displaying in upper image.
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: How To create regression lines (standard deviation chann
You may be able to use org.jfree.data.statistics.Regression in this context. There's a basic example here.
Re: How To create regression lines (standard deviation chann
Thanks for the reply but sorry to say that according to this example when i will add more regression line data in trend then it will display jointed regression lines like line chart! i want to do same like image - a segmented regression lines.
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: How To create regression lines (standard deviation chann
Right, Regression is just a convenient way to get the endpoints.somali wrote:When I add more regression line data…it will display jointed regression lines.
As it looks like you're using a HighLowRenderer, you may be able to adapt the approach shown in HighLowChartDemo2, which adds a second renderer to the plot, but I haven't tried it.somali wrote:I want…segmented regression lines..
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: How To create regression lines (standard deviation chann
1. Group your data into segments, one segment per regression line. We probably won't be able to help on that.
2. Calculate the regression line for the points in each section.
3. Get the first and last x value for each segment, and calculate the corresponding y value with the help of the regression line.
4. Use the first and last x and y values to construct a XYLineAnnotation. You may want to use the constructor that lest you specify a stroke (such as a new BasicStroke(3.0f)) and a Paint (such as Color.RED).
5. Add the XYLineAnnotation to the plot.
Note that this solution won't be dynamic,and will not automatically react to changes in the HighLowDataset.
2. Calculate the regression line for the points in each section.
3. Get the first and last x value for each segment, and calculate the corresponding y value with the help of the regression line.
4. Use the first and last x and y values to construct a XYLineAnnotation. You may want to use the constructor that lest you specify a stroke (such as a new BasicStroke(3.0f)) and a Paint (such as Color.RED).
5. Add the XYLineAnnotation to the plot.
Note that this solution won't be dynamic,and will not automatically react to changes in the HighLowDataset.
Re: How To create regression lines (standard deviation chann
i had created a functionality of edit chart (user can drag and drop mouse and draw line on chart) where i am using xyline annotation and to remove it another option is also for clear all annotation . you had give me a solution that will work but right now that's not the logic which will help me. Thanks.paradoxoff wrote:1. Group your data into segments, one segment per regression line. ..
5. Add the XYLineAnnotation to the plot.
Note that this solution won't be dynamic,and will not automatically react to changes in the HighLowDataset.
Re: How To create regression lines (standard deviation chann
I got the solution , if someone have requirement as my question here is demo code:
This is not perfect solution but my work done! you have to modifies as your requirement if you are using time series jfreechart.
Code: Select all
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Paint;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class RegressionTest {
private static final int N = 16;
private static final Random R = new Random();
private static XYDataset createDataset() {
XYSeries series = new XYSeries("Data");
for (int i = 0; i < N; i++) {
series.add(i, R.nextGaussian() + i);
}
XYSeriesCollection xyData = new XYSeriesCollection(series);
/*//count regression data------------------ as requirement
double[] coefficients = Regression.getOLSRegression(xyData, 0);
double b = coefficients[0]; // intercept
double m = coefficients[1]; // slope
double x = series.getDataItem(0).getXValue();
System.out.println("x:"+x+"m:"+m+"b:"+b+"ans:"+ m * x + b);
trend.add(x, m * x + b);
x = series.getDataItem(series.getItemCount() - 1).getXValue();
System.out.println("x:"+x+"m:"+m+"b:"+b+"ans:"+ m * x + b);
trend.add(x, m * x + b);
*/
XYSeries trend = new XYSeries("Trend");
//add here regressiondata
//here dummydata added
trend.add(1, 2);//1 st regression line startpoint
trend.add(3,4);//1 st regression line endpoint
trend.add(5,6);//2 regression line startpoint
trend.add(7,9);//2 regression line endpoint
trend.add(7.5,8.5);//3 regression line startpoint
trend.add(10,7);//3 regression line endpoint
trend.add(9,8);//4 regression line startpoint
trend.add(12,12);//4 regression line endpoint
xyData.addSeries(trend);
return xyData;
}
private static JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart("Test", "X", "Y",
dataset, PlotOrientation.VERTICAL, true, false, false);
return chart;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
XYPlot plot=chart.getXYPlot();
XYItemRenderer renderer = new CustomLineRenderertest(true,false);
plot.setRenderer(renderer);
ChartPanel chartPanel = new ChartPanel(chart) {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
f.add(chartPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
class CustomLineRenderertest extends XYLineAndShapeRenderer {
/**
* for add study line chart red green
*/
private static final long serialVersionUID = 1L;
public CustomLineRenderertest(boolean lines, boolean shapes){
super(lines, shapes);
}
@Override
public Paint getItemPaint(int row, int column) {
if(row>0){
if(column%2==0){
return new Color(0, 0,0, 0);
}else{
return Color.red;
}
}
return Color.blue;
}
}
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: How To create regression lines (standard deviation chann
As an aside, your renderer's colors don't match the legend. One mitigation strategy is to let your implementation of getItemPaint() obtain the parent Paint and return it as needed. For example,
Code: Select all
private static final Paint TRANSPARENT_BLACK = new Color(0, 0, 0, 0);
…
@Override
public Paint getItemPaint(int row, int column) {
Paint p = super.getItemPaint(row, column);
if (row > 0) {
if (column % 2 == 0) {
return TRANSPARENT_BLACK;
} else {
return p;
}
}
return p;
}
Re: How To create regression lines (standard deviation chann
Its work Perfect! Thank you.As an aside, your renderer's colors don't match the...