0

I want to use Anychart/Anymap in a dashboard, divided in two parts :

  1. First part the dynamic heat map (choropleth)

  2. Second part : generate a table of the map dataset (which are integrate in the script):

    • The table should automatically integrate the data from the dataset var of the map. With one change (first row has the columns names for more clarity).
    • The table have dynamic rows so when I click on one row (lets say the name of a country in the world map), the row is highlighted and the selected country is shown with its color in the map, with all the remaining countries grayed. Same thing if I click on the country on the map, all the remaining countries are grayed the corresponding row of the table (of the selected country) is highlighted.

This is a simple example of the dashboard, I was not able to make the "dataTable" table read the dataset of the map "var mapData" and show them ([mapData.name, mapData.value, mapData.population] did not work). And I do not want to integrate the data manually in the Table.

    <script>
        anychart.onDocumentReady(function () {
            // Sample data for the map and table
            var mapData = [
                { id: "US", value: 100, name: "United States", population: "330M" },
                { id: "CA", value: 50, name: "Canada", population: "38M" },
                { id: "MX", value: 75, name: "Mexico", population: "128M" }
            ];

            // Create a table layout
            var tableLayout = anychart.standalones.table();
            tableLayout.container("container");
            tableLayout.rowsCount(1); // Two rows
            tableLayout.colsCount(2); // One column
            tableLayout.cellBorder(false);

            // Create a map chart
            var map = anychart.map();
            map.geoData(anychart.maps.world);
            map.title("World Map with Data");

            var series = map.choropleth(mapData);
            series.labels(false); // Disable labels on the map for clarity

            // Create a data table to display selected region info
            var dataTable = anychart.standalones.table();
            dataTable.contents([
                ["Country", "Value", "Population"], // Table headers
                [mapData.name, mapData.value, mapData.population]
            ]);
            dataTable.width("100%");
            dataTable.height("100%");
            dataTable.cellBorder(true);
            dataTable.hAlign("center");
            dataTable.vAlign("middle");

            // Add the map to the first cell of the table layout
            //tableLayout.contents(map);

            // Add the data table to the second cell of the table layout
            //tableLayout.contents(dataTable);
  
            // Set contents.
            tableLayout.contents([
                    [map,dataTable]
            ]);
  
            // Handle click events on map regions to update the data table
            series.listen("pointClick", function (e) {
                var clickedRegion = e.point.get("id");
                var regionData = mapData.find(d => d.id === clickedRegion);

                if (regionData) {
                    dataTable.contents([
                        ["Country", "Value", "Population"], // Table headers
                        [regionData.name, regionData.value, regionData.population]
                    ]);
                } else {
                    dataTable.contents([
                        ["Country", "Value", "Population"],
                        ["N/A", "N/A", "N/A"]
                    ]);
                }
            });

            // Draw the table layout
            tableLayout.draw();
        });
    </script>

1 Answer 1

0

To ensure your dataTable is populated correctly, you need to provide an array of arrays to the contents() method. If you're using the data from your mapData variable, you'll need to transform it into this specific format.
The following sample demonstrates how to correctly map your data into the required array of arrays. It also shows a change from the pointClick event to pointMouseDown, which is generally more reliable and easier to work with.

anychart.onDocumentReady(function () {
  // Sample data for the map and table
  var mapData = [
    { id: "US", value: 100, name: "United States", population: "330M" },
    { id: "CA", value: 50, name: "Canada", population: "38M" },
    { id: "MX", value: 75, name: "Mexico", population: "128M" }
  ];

  // Create a table layout
  var tableLayout = anychart.standalones.table();
  tableLayout.container("container");
  tableLayout.rowsCount(1); // Two rows
  tableLayout.colsCount(2); // One column
  tableLayout.cellBorder(false);

  // Create a map chart
  var map = anychart.map();
  map.geoData(anychart.maps.world_source);
  map.title("World Map with Data");

  var series = map.choropleth(mapData);
  series.labels(false); // Disable labels on the map for clarity

  // Create and 
  var tableData = [["Country", "Value", "Population"]];  
  // Use a for loop to iterate over the array
  for (var i = 0; i < mapData.length; i++) {
    var item = mapData[i];
    // Push the new array with the required values
    tableData.push([item.name, item.value, item.population]);
  }

  // Create a data table to display selected region info
  var dataTable = anychart.standalones.table();
  dataTable.contents(tableData);
  dataTable.width("100%");
  dataTable.height("100%");
  dataTable.cellBorder(true);
  dataTable.hAlign("center");
  dataTable.vAlign("middle");

  // Set contents.
  tableLayout.contents([
    [map, dataTable]
  ]);

  // Handle click events on map regions to update the data table
  series.listen("pointMouseDown", function (e) {
    var clickedRegion = e.point.get("id");
    var regionData = mapData.find(d => d.id === clickedRegion);

    if (regionData) {
      dataTable.contents([
        ["Country", "Value", "Population"], // Table headers
        [regionData.name, regionData.value, regionData.population]
      ]);
    } else {
      dataTable.contents([
        ["Country", "Value", "Population"],
        ["N/A", "N/A", "N/A"]
      ]);
    }
  });

  // Draw the table layout
  tableLayout.draw();
});
html, body, #container {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
<script src="https://cdn.anychart.com/releases/8.13.1/js/anychart-base.min.js?hcode=a0c21fc77e1449cc86299c5faa067dc4"></script>
<script src="https://cdn.anychart.com/releases/8.13.1/js/anychart-map.min.js?hcode=a0c21fc77e1449cc86299c5faa067dc4"></script>
<script src="https://cdn.anychart.com/releases/8.13.1/js/anychart-exports.min.js?hcode=a0c21fc77e1449cc86299c5faa067dc4"></script>
<script src="https://cdn.anychart.com/releases/8.13.1/js/anychart-ui.min.js?hcode=a0c21fc77e1449cc86299c5faa067dc4"></script>
<script src="https://cdn.anychart.com/geodata/2.2.0/custom/world_source/world_source.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js"></script>
<script src="https://cdn.anychart.com/releases/8.13.1/js/anychart-table.min.js"></script>

<div id="container"></div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.