0

I am developing application which requires scatter chart. For scatter chart I am using Apache aChartEngine library to draw scatter chart but I needto draw Trade line also on that scatter chart. aChartEngine is not supports Trade line functionality.So anyone has Idea how to draw Trade line on scatter chart in android.enter image description here

Edit

Here is my code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

TrendLine t = new PolyTrendLine(2);
    Random rand = new Random();
   // double[] x = new double[10*10];
    double[] x = {4,6.5,8,10,15.5};
    double[] err = new double[x.length];
    double[] y = new double[x.length];
    Log.d(TAG,""+x.length);
    for (int i=0; i<x.length; i++) { x[i] = 1000*rand.nextDouble(); }
    for (int i=0; i<x.length; i++) { err[i] = 100*rand.nextGaussian(); } 
    for (int i=0; i<x.length; i++)
    {
        y[i] = x[i]*x[i]+err[i];
    //  y = -0.0004x2 + 0.3133x - 6.4081
        Log.d(TAG,"y y[i].."+y[i]);

        //Log.e(TAG,"t.predict..."+t.predict(y[i]));
    } // quadratic model

    Log.d(TAG,"y size.."+y.length);

    t.setValues(y,x);
    System.out.println(t.predict(12)); // when x=12, y should be... , eg 143.61380202745192

    Log.e(TAG,""+t.predict(12));    }

Using this code how can I draw a line on my graph?

Nitish Patel
  • 1,026
  • 2
  • 19
  • 36

1 Answers1

1

You could use Apache Commons math.

For linear, polynomial, exponential, logarithmic, and power trend lines OLSMultipleLinearRegression is all you need.

In this S.O. previous question, you can found the code for the trend lines.

Then you can simply add new series to yout chart with values derived from the trendline.

Community
  • 1
  • 1
Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
  • thnx. I have tried your code it good but I just wanted to know how to draw a trend Line once if I get array of Y. Another thing is that, is it only working for y= x2 + const equation or I can able to chage it to my equation say y=4x2+3x+6.4. Have you tried this code if yes can you share it with me? – Nitish Patel Apr 02 '14 at 11:50
  • One you have your TrendLine, you cold just invoke X[i] = t.predict(Y[i]) creating the second series. For drawing a combined chart look at the aChartEngine demos here (http://code.google.com/p/achartengine/source/browse/trunk/achartengine/demo/org/achartengine/chartdemo/demo/chart/CombinedTemperatureChart.java) for the equation you can change it. – Luca Sepe Apr 02 '14 at 12:07
  • I have already develop combinechart but I am not able to understand which method is draw line? In a code no class is there like canvas,polyline without it how the code is drawing the line?I can see all array values in my log but not in UI. How to show them on UI as a Trend Line. – Nitish Patel Apr 02 '14 at 12:23
  • you must look at your trend line like another serie. If you postthe code, maybe I could help. – Luca Sepe Apr 02 '14 at 12:25