Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
Dynamically create chartjs charts in a sidebar

I am loading a toggled sidebar's content when a category is selected from a list on the main page. In this sidebar, I am using ajax to retrive the sub-categories for that category and display them.

For each sub-category, I need to create a chartjs line chart and display it in the sidebar. The number of sub-categories can vary, so sometimes the code it returns many be:

<h1> Sub-Category 01</h1>
<div><canvas id="line-chart01"></canvas></div>

Then sometimes, it might generate two or three.

<h1> Sub-Category 01</h1>
<div><canvas id="line-chart01"></canvas></div>

<h1> Sub-Category 02</h1>
<div><canvas id="line-chart02"></canvas></div>

The code to generate the chartjs objects would need to be similar to this, but I am stuck as to how to generate this on the main page as I only know how many charts to generate after the category has been selected:

new Chart(document.getElementById("line-chart01"), {
type: 'line',
data: {
labels: ['Jan','Feb','Mar', 'April'],
datasets: [{ 
    data: [86,114,106,106],
    label: "Values",
    borderColor: "#3e95cd",
    fill: false
  }
]
},
options: {}
});

The function which opens the sidebar and loads the content is:

function openSidebar(catID){
    
    var dataString = 'catID='+catID;
    $.ajax({
    type: "POST",
    url: "load.php",

    data: dataString,
    cache: false,
    success: function(html) {
         document.getElementById("sidebarContent").innerHTML = html;
    }
    });
            
}

Can anyone suggest how to go about this? I have found threads loading new data and updating a chart which has been generated on the page already, but nothing related to what I'm trying to do here. Thanks.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda