0

I am trying to populate a DataTable from google chart to generate a chart. So far, I was able to get the information to travel from the database (MySQL) to the controller, and from there to the view using an ArrayList but when reading the dates, something happens and controls are subtracted:

In summary: I am sending an Array with data in the 2019-05-21 format and JavaScript interprets it as a mathematical operation, leaving 1,993.

I understand that it may be seen as a basic error, but it is my first time working with JavaScript and I have already spent several hours reading documentation and have not found any solution.

Controller:

@Controller
@RequestMapping
public class indexController {
    
    @Autowired
    private IAccionService service;
    

    
    @GetMapping("chart_index")
    public String profileSettings(Model model) {
        
        String msg="holasoyundatodeprueba";
        String msg2="Fecha1";
        model.addAttribute("msg", msg);
        model.addAttribute("msg2", msg2);
        
        List<Acciones>accioneslst=service.listar_acciones();
        Acciones[] arrayAcciones = new Acciones[accioneslst.size()];
        arrayAcciones=accioneslst.toArray(arrayAcciones);
        
        ArrayList<String> arrayFechas = new ArrayList<String>();
        ArrayList<Integer> arrayOpen = new ArrayList<Integer>();
        ArrayList<Integer> arrayClose = new ArrayList<Integer>();
        
        
        for (Acciones acciones : arrayAcciones) {
            
            arrayFechas.add(acciones.getFecha().toString());
            arrayOpen.add(acciones.getOpen_value());
            arrayClose.add(acciones.getClose_value());
            System.out.println(arrayFechas.toString());
        }
        
        
        
        model.addAttribute("fechaGrafico",arrayFechas);
        model.addAttribute("openGrafico",arrayOpen);
        model.addAttribute("closeGrafico",arrayClose);
        
        return "chart_index";
        
    }

JavaScript code (Google Chart: Line Chart) added to the view:

<!DOCTYPE html>
<html>
<head xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gráfico de acciones</title>

<link rel="stylesheet" type="text/css" href="CSS/styles_chart.css" />


<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
      
      function drawChart() {
        var Fecha=[[${fechaGrafico}]];
        var Open=[[${openGrafico}]];
        var Close=[[${closeGrafico}]];
        
        var data = new google.visualization.DataTable(); //Cambiado a DataTable
        data.addColumn('string','Fecha');
        data.addColumn('number','Open');
        data.addColumn('number','Close');
        
        for(i=0;i<Fecha.length;i++)
        data.addRow(["'"+Fecha[i]+"'",Open[i],Close[i]]);

        
        console.log(Fecha);
        
        var options = {
                title : "[[${msg}]]",
                curveType : 'function',
                backgroundColor : '#EDEEF0',
                width : '1323',
                height : '855',
                legend : 'none',
                chartArea : {
                    width : '1200',
                    height : '800'
                },
                colors : [ '#A6CEE3', '#1F78B4' ]
            };
        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }
    </script>

Class:

Package com.amsterdam.springboot.v1.app.models;

import java.sql.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name="accion") //Nombre de la tabla
public class Acciones {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Date fecha;
    private int open_value;
    private int high_value;
    private int low_value;
    private int close_value;
    private int adj_close;
    private int volume;
    private String enterprise;
    
    
    public Acciones() {
        // TODO Auto-generated constructor stub
    }
    
    
    
    
    public Acciones(Date fecha, int open_value, int high_value, int low_value, int close_value, int adj_close,
            int volume, String enterprise) {
        super();
        this.fecha = fecha;
        this.open_value = open_value;
        this.high_value = high_value;
        this.low_value = low_value;
        this.close_value = close_value;
        this.adj_close = adj_close;
        this.volume = volume;
        this.enterprise = enterprise;
    }


    public Date getFecha() {
        return fecha;
    }
    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }
    public int getOpen_value() {
        return open_value;
    }
    public void setOpen_value(int open_value) {
        this.open_value = open_value;
    }
    public int getHigh_value() {
        return high_value;
    }
    public void setHigh_value(int high_value) {
        this.high_value = high_value;
    }
    public int getLow_value() {
        return low_value;
    }
    public void setLow_value(int low_value) {
        this.low_value = low_value;
    }
    public int getClose_value() {
        return close_value;
    }
    public void setClose_value(int close_value) {
        this.close_value = close_value;
    }
    public int getAdj_close() {
        return adj_close;
    }
    public void setAdj_close(int adj_close) {
        this.adj_close = adj_close;
    }
    public int getVolume() {
        return volume;
    }
    public void setVolume(int volume) {
        this.volume = volume;
    }
    public String getEnterprise() {
        return enterprise;
    }
    public void setEnterprise(String enterprise) {
        this.enterprise = enterprise;
    }
    
    
    
    
}
  • Hello, thanks for your answer. Yes, sure i can do that. This is what is showing in the console [2005, 2002, 2001, 2000, 1999, 1998, 1995, 1993, 1992, 1991, 1988, 1987, 1986, 1985, 1984, 2011, 2010, 2009, 2008, 2007, 2004, 2003, 2002, 2001, 2000, 1997] This is what i am sending: [2019-05-09, 2019-05-12, 2019-05-13, 2019-05-14, 2019-05-15, 2019-05-16, 2019-05-19, 2019-05-21, 2019-05-22, 2019-05-23, 2019-05-26, 2019-05-27, 2019-05-28, 2019-05-29] – Anthonny Gueli Jul 12 '20 at 16:53
  • Hello, the controller works with a class named "Acciones" I just added the class in the main post. – Anthonny Gueli Jul 12 '20 at 18:26
  • what data is actually stored in the MySQL database, what is the column type of "Fecha" in the table "accion" – Toerktumlare Jul 12 '20 at 19:05
  • Hello, the data type of that column is "date" And the current format is yyyy-mm-dd – Anthonny Gueli Jul 12 '20 at 20:25

0 Answers0