Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Creating a drill down column chart in Amcharts5

I am creating a drill down column chart in Amcharts5. My code is as follows:

var root = am5.Root.new("chartdiv"); 

root.setThemes([
  am5themes_Animated.new(root)
]);


var chart = root.container.children.push( 
  am5xy.XYChart.new(root, {
    width: am5.p100,
    panY: false,
    wheelY: "zoomX",
    layout: root.verticalLayout
  }) 
);

// Define data
var data = [{ 
  category: "I&W", 
  value1: 1000, 
  sub: [{ maatregel: "eerste maat", value2: 200 },
  { maatregel: "tweede maat", value2: 388 }]
}, { 
  category: "J&V", 
  value1: 1200, 
}, { 
  category: "Def", 
  value1: 850, 
}];

// Cretae Y-axis
let yAxis = chart.yAxes.push(
  am5xy.ValueAxis.new(root, {
    renderer: am5xy.AxisRendererY.new(root, {
    })
  })
);

// Create X-Axis
var xAxis = chart.xAxes.push(
  am5xy.CategoryAxis.new(root, {
      maxDeviation: 0.2,
      renderer: am5xy.AxisRendererX.new(root, {
    }),
    categoryField: "category"
  })
);
xAxis.data.setAll(data);

// Create titles 

yAxis.axisHeader.children.push(am5.Label.new(root, {
    text:"Uitgaven in miljoenen",
    centerX: am5.p50,
    centerY: am5.p50
}));

xAxis.axisHeader.children.push(am5.Label.new(root, {
    text:"Begroting",
    fontsize: "35",
    centerX: am5.p50,
    centerY: am5.p50

}));

// Create series
var series = chart.series.push( 
  am5xy.ColumnSeries.new(root, { 
    name: "2022", 
    xAxis: xAxis, 
    yAxis: yAxis, 
    valueYField: "value1", 
    categoryXField: "category",
    tooltip: am5.Tooltip.new(root, {})
    
  }) 
);
series.data.setAll(data);

series.columns.template.setAll({
    tooltipText: "{category}: {value1}"
  });



// Add legend
var legend = chart.children.push(
    am5.Legend.new(root, {
    })); 
legend.data.setAll(chart.series.values);

//event on clicking

series.columns.template.events.on("click", function myFunction(ev) {
    if (ev.target.dataItem.dataContext.sub != undefined) {
        console.log("test", ev.target.dataItem.dataContext.sub);
        ev.chart.data = ev.target.dataItem.dataContext.sub;
    }
  });

The if statement is working as intended, as if you click on the second or third column, nothing happens. The problem isthat I get the following error message:

Uncaught TypeError: Cannot set properties of undefined (setting 'data')

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!