What is the easiest way to build a scatter chart with java?

There are two data arrays, I divide each of them into two to get the coordinates

//ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ для построСния Π³Ρ€Π°Ρ„ΠΈΠΊΠ° double[] Xwords = new double[words.length]; double[] Ywords = new double[words.length]; //раздСляСм массив слов Π½Π° Π΄Π²Π° массива с ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌΠΈ for (int i=0; i<words.length; i++){ Xwords[i] = words[i][0]; Ywords[i] = words[i][1]; } double[] Xdocs = new double[docs[0].length]; double[] Ydocs = new double[docs[0].length]; //раздСляСм массив Ρ‚Π΅Ρ€ΠΌΠΎΠ² Π½Π° Π΄Π²Π° массива с ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌΠΈ for (int i=0; i<docs[0].length; i++){ Xdocs[i] = docs[0][i]; Ydocs[i] = docs[1][i]; } 

Which library will allow in a couple of lines to drive this data into the method and get the usual scatter plot?

    3 answers 3

    If you use standard Java2D, then the code that draws a dotted chart may look like this:

     public void drawDiagram(Graphics g, double[] x, double y[], double scaling, int height, Color color) { int lastX = 0, lastY = 0; g.setColor(color); for(int i = 0; i < x.length; i++) { g.drawLine((int)(lastX * scaling), height - (int)(lastY * scaling), height - (int)(x[i] * scaling), (int)(y[i] * scaling)); lastX = (int)x[i]; lastY = (int)y[i]; } } 

    Where:

    • g - the graphic context of the canvas or image.
    • x is an array with x coordinates.
    • y - an array with coordinates y .
    • scaling is a scaling factor.
    • height - the height of the drawing panel (you can also take the maximum y coordinate).
    • color - color of the line.

      I used the JFreeChart library, a good example here .

        On the Canvas to draw. Take SurfaceView and in OnDraw() method draw what you need.