1

I want to create a chart with Chart.bundle.js. The data in the graph must come from the database. A servlet sends the data to the JSP. My problem is how to put the data received from the servlet into "labels" and "data". My example is below.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
    <input type="hidden" name="donnee" id="donnee" value="${donnees }" />
    <c:out value="${donnees }"></c:out>
    <input type="hidden" name="etiquete" id="etiquete" value="${etiquetes}" />
    <c:out value="${etiquetes }"></c:out>
<div style="width:80%">
    <canvas id="myChart" ></canvas>
</div>
<script>
    var donnees=document.getElementById("donnee");
    var etiquetes=document.getElementById("etiquete");
    var myContext = document.getElementById("myChart");
     var myChartConfig = {
        type: 'bar',
        data: {
            labels:etiquetes,
            datasets: [
                {
                    label: "etiquette n°1",
                    data:donnees,
                    backgroundColor: ['green','yellow','red'],
                },
            ]
        }
    }
    var myChart = new Chart(myContext, myChartConfig);

</script>
  • you don't need jsp, you can use ajax to get the data directly from the servlet -- have a look at these answers --> [How to use Servlets and Ajax?](https://stackoverflow.com/q/4112686/5090771) -- [How to perform Ajax call from Javascript to JSP?](https://stackoverflow.com/a/3507021/5090771) – WhiteHat Jul 31 '19 at 11:23

0 Answers0