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 .
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.
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.
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.
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
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.
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: