Win a copy of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home this week in the General Computing forum!
Forums Login/signup

Scatterplot without JCharts

+Pie Number of slices to send: Send
I have a CSV file containing 3 entities,how do i create a scatterplot without JCharts??
+Pie Number of slices to send: Send
Why do you not want to use jcharts?

Are other charting libraries OK?
+Pie Number of slices to send: Send
I have a CSV File containing bond trades which has 3 columns namely Yield, amount of maturity and days to maturity.I want a scatterplot to be generated using Graphics 2D, paint component.I took the entities in a class and created an object of the same and stored the contents in an arraylist.But when i am trying to plot the same, it doesnt work,throws a null point exception error .
+Pie Number of slices to send: Send
There's not much we can do about that without seeing the relevant code and the full stack trace of the exception.

What are the answers to my questions?
+Pie Number of slices to send: Send
Welcome to the Ranch

As Tim ay, we need to know the full detail of the exception before we can help/
+Pie Number of slices to send: Send
Hi

I have a CSV file as given below.I need to do scatter plot for the same without using JCharts or charting libraries.

CSV FILE is like shown below

YIELD         DAYS_TO_MATURITY      AMOUNT_CHF(000)
7.223                  1002                             250

I have provided a sample code what I have done,but apparently the plot is not getting displayed on the screen

Ideally, when I click the open button from the Frame class, after the file is been opened the graph should be displayed. There is a point class which has the attributes as the columns in the CSV file.So, as soon as I get the file name , the file is read and the values are stored in an arraylist of class Point type.And based on the values in the ArrayList, the data is to be plotted.


+Pie Number of slices to send: Send
Several things which I think are poor design
  • 1: Having a frame implement action listener
  • 2: Extending JFrame
  • It is incorrect style to have non‑constant fields' names in UPPER_CASE or to start them with underscores.
    Please explain the arithmetic of lines 31‑32. Work out where your ellipses will appear.
    Is that an overriding paintComponent method? Put the @Override annotation on it. Make its first line read super.paintComponent(g); which will remove old painting after resizing. Give it protected access not coderanch.
    Your method starting line 69 is error‑prone because you don't validate the array anywhere. I also think it ought to have private access.
    +Pie Number of slices to send: Send
    The lines 31 and 32 are the shifting of the points with respect to the axis (both x and Y).I am unable to bring in a idea on how to shift and plot the same.If there is any idea in which I could bring in the plot with the same.
    +Pie Number of slices to send: Send
    Have you tried the arithmetic in lines 31‑32?
    +Pie Number of slices to send: Send
    No,I havent tried as I am unaware where the axes are actually.If I am able to create the axes in the initial stage , perhaps I can try on to the arithmetic,Could you advice on How to start as I think I have missed a point somewhere
    +Pie Number of slices to send: Send
    Simply work out the location of an ellipse with 1 day to maturity. That arithmetic is very simple and I think that will make the probllem obvious.
    +Pie Number of slices to send: Send
    Can you give me one sample example to try so that I can understand how it works?
    +Pie Number of slices to send: Send
    No. Work out the location of an ellipse on a 100×100px component with 1 day to maturity.
    +Pie Number of slices to send: Send
    Hi,

    I tried creating a point under no axis consideration on a 100x 100 plot..just wanted to know is this what was intended as result?

    The result what I am getting is a small ellipse based on the dimensions given...

    +Pie Number of slices to send: Send
    Why don't you do what I suggested. Not create new code, but do a little bit of primary school arithmetic and work out where the ellipse will be.
    +Pie Number of slices to send: Send
    hi Jan,

    in your first code your calculation of the pixel coordinates is far off, when I tried your code I got pixel coordinates of well above 10_000 (and even above 100_000).
    So, Campbells suggestion is spot on: check your calculation and find out where the mistake is and what the correct result should be.

    Another way, that looks more complicated at first sight, but is o so convenient, is to use your own coordinate system for your PlotComponent instance.
    I used it in your code, and I set the left bottom corner to (0,.9 * minimum x-value of your points nominal value, 0.9 * minimum y value of days to maturity) and as top right point (1,1 * maximum x value of nominal value, 1.1 * max value of days to maturity).

    I use an AffineTransform for this, and in particular this method:


    Now, I used only one Point (your example with 7.xxx yield, 1002 days to maturity, and 250 nominal value, and I used this as paintComponent:

    As you see, the advantage is that we let Java do all the scalings, in stead of doing it ourselve. But remember, if you add another point, then you must issue a repaint; a new AffineTransform will be calculated and applied.

    Well, see if you like this approach!
    +Pie Number of slices to send: Send
     

    Jan Rad wrote:I have a CSV file containing 3 entities, how do I create a scatterplot without JCharts??



    comment: if this does not help you personally I write it as a future reference for anyone needing to write a graph from scratch in java from a csv.

    I did something like that a month ago.

    for charts from scratch I used the normalization equation described here:

    http://www.untoldentertainment.com/blog/2009/08/27/flash-as3-the-only-math-youll-ever-need/

    using the knowledge I got from this article I learned how to create charts from scratch( and minimaps, sliders etc).

    how do you put a dot with the value of 5000 on a chart of 100px? you need to normalize it and find the relative position within the 100 px graph.

    let's say the max val of the 5000 is 10000. then:
    graphx = (5000/10000)*100 = 50

    you can do the same for y val or even z if it's a 3d graph.

    for the csv file.

    all csv files have a line separator (for each row) and each line have separators (for columns).
    the first row is the column names usually (this is important to remember as these are not values.

    for example (csv file):

    id,name,age
    1,gorge,12
    2,ben,33
    3,dan,20

    so to get data from the file (after you get it using file io), you need to split it by new line (.split("\n") ).
    now you have all the csv file rows in an array.

    to get the rows columns split it by comma "," ( .split(",").

    let's say we want bens age we will write something like:

    String row  = csv[1];

    String[] cols = row.split(",")

    System.out.print(cols[2]);//prints 33

    this can also be done the same way in: c++,php,js,python etc. you can use regex instead\with split too.

    in your case, you will need a class to do this with loops and methods that return all the colls.

    I can't give you my code as its a clients paid work and he asked me not to give it to anyone.

    if it's beyond your level, don't give up, learn java anyway you can until you master the basics.

    also, swing was not easy to master but once you do its great.


    if you have a specific question I'll try to help.

    reply
    reply
    This thread has been viewed 873 times.
    Similar Threads
    JChart not being displayed on JSP
    JChart 2D Demo
    Getting the X, Y coordinate values in a JChart
    JChart component .. Need some pointers !!
    JChart export to Powerpoint
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Jun 27, 2025 07:49:07.