Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By benji2505
  • 1 Post By ShadowWalker

Thread: How to draw Polynomial Trendline in JavaFX

  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default How to draw Polynomial Trendline in JavaFX

    Greetings.

    First of all, I would like to give some details about the background of this project. I was searching all over the internet for a ScatterChart or LineChart which can represent data on 2 Y-Axis ( each one of them has a different scale ). I couldn't find anything good in native JavaFX ( without any use of external API ) so I decided to create on for myself. (Yes sometimes I tend to reinvent the wheel :P )

    The creation of the chart is made with the code below :

    Java FX Code:
    
    CustomAxis xAxis = new CustomAxis(0, 160, 20);
    CustomAxis y1Axis = new CustomAxis(0, 100, 10);
    CustomAxis y2Axis = new CustomAxis(2150, 2500, 50);
    
    TwoAxisScatterChart chart = new TwoAxisScatterChart(850, 500, xAxis, y1Axis, y2Axis);
    
    chart.addValue(0, 10 , TwoAxisScatterChart.FIRST_Y_AXIS);
    chart.addValue(10, 22, TwoAxisScatterChart.FIRST_Y_AXIS);
    chart.addValue(5, 34, TwoAxisScatterChart.FIRST_Y_AXIS);
    chart.addValue(28, 78, TwoAxisScatterChart.FIRST_Y_AXIS);
    chart.addValue(39, 37, TwoAxisScatterChart.FIRST_Y_AXIS);
    chart.addValue(70, 11, TwoAxisScatterChart.FIRST_Y_AXIS);
    chart.addValue(48, 64, TwoAxisScatterChart.FIRST_Y_AXIS);
    			
    chart.addValue(100, 2298 , TwoAxisScatterChart.SECOND_Y_AXIS);
    chart.addValue(110, 2193, TwoAxisScatterChart.SECOND_Y_AXIS);
    chart.addValue(120, 2469, TwoAxisScatterChart.SECOND_Y_AXIS);
    chart.addValue(130, 2332, TwoAxisScatterChart.SECOND_Y_AXIS);
    chart.addValue(140, 2404, TwoAxisScatterChart.SECOND_Y_AXIS);
    chart.addValue(150, 2399, TwoAxisScatterChart.SECOND_Y_AXIS);
    chart.addValue(160, 2240, TwoAxisScatterChart.SECOND_Y_AXIS);
    How to draw Polynomial Trendline in JavaFX-chart.png


    My chart looks something like the image above and it's all created using shapes ( Lines, Text, Circles ) which are drawn into a 'Region' ( I am not very fond of Canvas to say the truth ). The position of each element ( Axis, Values, Title ) is calculated at runtime depending on the Stage dimensions. I was thinking to upload this entire project into GitHub and share it, but before I do that I want to make it as much portable as I could.

    Everything works very smoothly and now I would like to more utilities. I want to give the user the ability to draw a 'trend line' ( Polynomial, Linear ). Right now I stack on computations of those lines but I believe I will manage. The question I have is about drawing such a shape in JavaFX. Well in a case of linear trendline the question is trivial but what about polynomial? What shape do I need to use in order to achieve that?

    Polynomial TrendLine should like something like that :

    How to draw Polynomial Trendline in JavaFX-polynomial.png

    Sorry for the long post. Thank for your time.
    Last edited by ShadowWalker; 03-22-2017 at 12:46 PM.

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: How to draw Polynomial Trendline in JavaFX

    Do you want the user to draw these lines in there or do you want the machine to create lines from linear/polynomial regression?

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: How to draw Polynomial Trendline in JavaFX

    I would like to have a method like
    Java FX Code:
    .showPolynomialTrendLine(TwoAxisScatterChart.FIRST_Y_AXIS)
    and the machine will calculate/generate and draw the trendline by itself using the points on the selected Y-Axis. I believe that the calculation might be very slow combined with the drawing process, so I would like to find the most efficient way to do that.

    I checked this website
    Thorwin: Trend Curve/Line in JavaFX Chart
    where he uses an external math API to find the coefficients points and after that he draw multiple points like the code below to make a curved line.

    Java FX Code:
    for (double x = 0; x <= 5.0; x += 0.05) {
                double y = polynomial(x, coefficients);
                series2.getData().add(new XYChart.Data<>(x,y));
            }
    The above implementation is very slow when the upper bound is bigger. For example, if we start at the point x1 = 0 and we end at x2 = 5000+ it's almost impossible to achieve that in dissent amount of time.

    If for example there was a JavaFX shape where I could say " Draw a line/curve which goes through all points[] " this will be trivial, but I believe that there is not something like that.
    Last edited by ShadowWalker; 03-22-2017 at 01:46 PM.

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: How to draw Polynomial Trendline in JavaFX

    I guess this is post is more about the linear/ polynomial regression algorithms rather than the display in JavaFX.

    I would look for publications that deal with big data regressions. I am sure there is some literature out there. I would google something like "big data regression techniques"
    ShadowWalker likes this.

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: How to draw Polynomial Trendline in JavaFX

    I was searching it with "Linear Trendline" term so didn't find at lot about it. I see with "linear/ polynomial regression" ( which I now believe is the correct term) there are a lot of publications. Thank you very much for the information you gave me!! I hope I will find an efficient way to represend it in JavaFX.

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: How to draw Polynomial Trendline in JavaFX

    I complete the entire project and if anyone ( now or in the future ) want to give it a look or just use it, you may find it on GitHub
    https://github.com/JKostikiadis/MulitAxisScatterChart
    ( I hope I am not violating any forum rule by posting another site's link. )

    I will try to keep it updated as much as I can ( and of course if needed ).
    benji2505 likes this.

Similar Threads

  1. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-17-2013, 11:10 PM
  2. work to do about Polynomial
    By Joana in forum New To Java
    Replies: 14
    Last Post: 04-15-2012, 07:54 AM
  3. Polynomial Graphing help
    By captain in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 12:53 PM
  4. JavaFX Script and JavaFX Mobile
    By levent in forum Java Software
    Replies: 1
    Last Post: 01-27-2010, 04:48 PM
  5. Help with polynomial program
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •