Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Austin Harms
8,680 PointsTrouble accessing second dataset in ChartJS
I'm having trouble figuring out how to access my second dataset object. My chart works with both datasets and my goal is to toggle each dataset on and off. For now I'm just needing help with accessing the entire second object. Thanks!
var myLineChart = new Chart(line, {
type: 'line',
data: {
labels: ["16-22", "23-29", "30-5", "6-12", "13-19", "20-26", "27-3", "4-10", "11-17", "18-24", "25-31"],
datasets: [
{
label: "My First dataset",
fill: true,
lineTension: 0.1,
backgroundColor: "rgba(116,119,191,0.2)",
borderColor: "rgba(116,119,191,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(116,119,191,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 6,
pointHitRadius: 10,
data: [750, 1250, 1000, 1500, 2000, 1500, 1750, 1250, 1750, 2250, 1750, 2250],
spanGaps: false,
},
{
label: "My Second dataset",
fill: true,
lineTension: 0.1,
backgroundColor: "rgba(116,119,191,0.2)",
borderColor: "rgba(116,119,191,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(116,119,191,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 6,
pointHitRadius: 10,
data: [1750, 250, 7000, 1100, 1000, 1300, 750, 1550, 1350, 2650, 750, 2550],
spanGaps: false,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
}
});
2 Answers
Sebastian H
19,905 PointsHey Austin, it looks like the datasets array is actually wrapped inside the data object. Which is easy to miss because you are missing some indentation on the 4th and 5th lines there
var myLineChart = new Chart(line, {
type: 'line',
data: {
labels: ["16-22", "23-29", "30-5", "6-12", "13-19", "20-26", "27-3", "4-10", "11-17", "18-24", "25-31"],
datasets: [
So to access that second dataset object try:
myLineChart.data.datasets[1]
Austin Harms
8,680 PointsAustin Harms
8,680 PointsMy first thought was something like this but I know it's not right: myLineChart.datasets[0][1]