Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created October 20, 2023 07:47
Show Gist options
  • Save rg3915/fb795692402e15f55192b7620aa5f467 to your computer and use it in GitHub Desktop.
Save rg3915/fb795692402e15f55192b7620aa5f467 to your computer and use it in GitHub Desktop.
Django and ChartJS load data chartjs 2023 json parse data
const ctx = document.getElementById('grafico')
const labels = JSON.parse('{{ labels|safe }}')
const data = JSON.parse('{{ data|safe }}')
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'The quick brown fox jumps over the lazy dog',
data: data,
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: false
}
}
}
})
import json
labels = json.dumps([item.title for item in items])
data = json.dumps([float(item.value) for item in items])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment