Search code examples
angulartypescriptamcharts

ES6: Property 'selected' does not exist on type 'Object'


From 'AMcharts4 codepen pre selecting areas', I want to convert the JS into ES6. However, I got error

error TS2339: Property 'selected' does not exist on type 'Object'.

The code which I want to convert is given below:

// Create map instance
let chart = am4core.create("chartdivmap", am4maps.MapChart);

// Set map definition
chart.geodata = am4geodata_worldHigh;

// Set projection
chart.projection = new am4maps.projections.Miller();

// Center on the groups by default
chart.homeZoomLevel = 1.5;
chart.homeGeoPoint = {
    longitude: 10,
    latitude: 52
};

// Polygon series
let polygonSeries = chart.series.push(new am4maps.MapPolygonSeries());
polygonSeries.useGeodata = true;


var polygonTemplate = polygonSeries.mapPolygons.template;
polygonTemplate.tooltipText = "{name}";
polygonTemplate.fill = chart.colors.getIndex(0);

// Hover state
var hs = polygonTemplate.states.create("hover");
polygonTemplate.fill = am4core.color("#CCCCCC");
hs.properties.fill = am4core.color("#010101");

polygonTemplate.adapter.add("fill", function(fill, target) {
    if (target.dataItem.dataContext && target.dataItem.dataContext.selected) {
        return am4core.color("#666666");
    }
    return fill;
});

I tried by let k:any = target; and pass the variable like function(fill, target, k) and tried to catch the value like: k.dataItem.dataContext.selected which gave me more error.


Solution

  • Can you try something like this :

    polygonTemplate.adapter.add("fill", function(fill, target) {
        const ctx = target.dataItem.dataContext as any;
        if (ctx && ctx.selected) {
            return am4core.color("#666666");
        }
        return fill;
    });