Estoy usando chart.js y tengo un gráfico de líneas. Quiero agregar una segunda línea/conjunto de datos que comience desde donde termina la primera línea.
He visto a personas hacer esto agregando null en la propiedad de data dentro de los conjuntos de datasets de esta manera:
type: "line", data: { labels: ["January,"February","March","April","May","June","July","August"], datasets: [ { label: "My First Dataset", data:[4,5,6,7,8], fill: false, borderColor: "rgb(75, 192, 192)", tension: 0.3, }, { label: "My First Dataset", data:[null,null,null,null,null,9,10,11], fill: false, borderColor: "rgb(75, 192, 192)", tension: 0.3, }, ], }, });¿Hay una manera diferente de lograr esto?
Sí, puedes usar la notación de objetos así:
const options = { type: 'line', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes', data: [{ x: "Red", y: 5 }, { x: "Blue", y: 3 }, { x: "Yellow", y: 8 }], borderColor: 'pink' }, { label: '# of Points', data: [{ x: "Yellow", y: 8 }, { x: "Green", y: 5 }, { x: "Purple", y: 3 }, { x: "Orange", y: 8 }], borderColor: 'orange' } ] }, options: {} } const ctx = document.getElementById('chartJSContainer').getContext('2d'); new Chart(ctx, options); <body> <canvas id="chartJSContainer" width="600" height="400"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script> </body>