Could it be that the AmCharts Drillup feature is not fully integrated with AngularJS?

UPDATE:

Check out this Plunker I created to better showcase the issue.

There seems to be an issue with the Back link label in the chart not functioning as expected.


I'm currently facing a challenge with the AmCharts Drillup function, which should allow users to navigate back to previously displayed chart content. While I've managed to get the Drilldown function working correctly, I'm struggling with getting the Drillup functionality to work in AngularJS.

Error in Console:

https://i.stack.imgur.com/E7Mlp.png

Origin of the Error:

.........................
// add back link
// let's add a label to go back to yearly data
event.chart.addLabel(
  70, 10, 
  "< Go back",
  undefined, 
  13, 
  undefined, 
  undefined, 
  undefined, 
  true, 
  'javascript:mostSoldDrillUp();');  <------- THIS IS THE LINK LABEL FOR DRILLUP
.................................

 function mostReferralsDrillUp() {
 ...........
 ...........
 }

Methods I have attempted:

  • I tried placing the AmChart JavaScript code inside a directive's link function, but encountered the error: Uncaught ReferenceError: mostSoldDrillUp is not defined at
    :1:1
    in the browser console.
  • I also attempted to place it within a controller, resulting in the same error.
  • Simply including the JavaScript file and defining the chart's id in a div within the HTML using ui-router yielded the same error.

    .state('home', {
            url: '/',
            templateUrl: './pages/home.html',
            controller: 'HomeCtrl'
        })
    
  • Finally, I omitted the controller from my route definition:

    .state('home', {
            url: '/',
            templateUrl: './pages/home.html',
            // controller: 'HomeCtrl'
        })
    

While this approach worked, I require a controller for other functionalities, rendering this method obsolete.

If anyone has encountered a similar issue or has a potential solution, your input would be greatly appreciated. Thank you.

Answer №1

AmCharts does not have knowledge of Angular's $scope, and when the label link is clicked, it executes resetChart in the global window scope instead of your controller's scope. There are a few ways to address this issue.

1) Define resetChart in the global/window scope to maintain the label within the chart. While not recommended if you want to adhere to Angular's best practices, it can still work.

//inside controller
window.resetChart = function() {
  chart.dataProvider = chartData;
  chart.titles[0].text = 'Yearly data';

  // Remove the "Go back" label
  chart.allLabels = [];

  chart.validateData();
  chart.animateAgain();
}

2) Attach the resetChart method to your controller's scope and use an external element to handle the chart reset, rather than relying on the chart label. This may not be as visually appealing, but it is cleaner than manipulating window.

home.html

<h1>HOME STATE</h1>
<button ng-show="isDrillDown" ng-click="resetChart()">Go back to yearly</button>
<div id="chartdiv"></div>

excerpt of script.js

chart.addListener("clickGraphItem", function(event) {
  if ('object' === typeof event.item.dataContext.months) {

    // Set the monthly data for the clicked month
    event.chart.dataProvider = event.item.dataContext.months;

    // Update the chart title
    event.chart.titles[0].text = event.item.dataContext.category + ' monthly data';

    // Validate the new data and animate the chart again
    event.chart.validateData();
    event.chart.animateAgain();
    $scope.isDrillDown = true;
  }
});

// Function to reset the chart back to yearly data
$scope.resetChart = function() {
  chart.dataProvider = chartData;
  chart.titles[0].text = 'Yearly data';

  // Remove the "Go back" label
  chart.allLabels = [];

  chart.validateData();
  chart.animateAgain();
  $scope.isDrillDown = false;
}

Make adjustments as necessary.

For a demonstration of the second method, see the updated plunker here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with the execution of Typescript decorator method

Currently, I'm enrolled in the Mosh TypeScript course and came across a problem while working on the code. I noticed that the code worked perfectly in Mosh's video tutorial but when I tried it on my own PC and in an online playground, it didn&apo ...

Calculating the sum of all words that have been successfully finished

My spelling game includes words of different sizes ranging from 3 to 6 letters. Once a certain number of words are completed in the grid, the remaining grid fades away. Instead of only considering one word size at a time, I want the game to calculate the t ...

Transferring Variables from WordPress PHP to JavaScript

I have implemented two WordPress plugins - Snippets for PHP code insertion and Scripts n Styles for JavaScript. My objective is to automatically populate a form with the email address of a logged-in user. Here is the PHP snippet used in Snippets: <?p ...

Error: It is not possible to access the 'map' property of an undefined value 0.1

I'm currently experimenting with React.js and I encountered an error that's giving me trouble. I can't seem to pinpoint the root cause of this issue. Shout out to everyone helping out with errors: TypeError: Cannot read property 'map ...

How can I export an array in Ajax/PHP to the user as a .txt file?

Currently, I am working on a PHP file named "php-1" that is responsible for generating an HTML page. This particular file requests input from the user and once the user clicks on a button labelled "getIDs" (it's worth noting that there are multiple b ...

Issue: [$controller:ctrlreg] Angular JS code experiencing functionality issues

I recently started learning angularJs and I'm using a book as a guide. However, there seems to be an issue with one of the examples provided in the book. <!DOCTYPE html> <html ng-app> <head> <script src="js/angular.min.js">&l ...

Error occurs during server to server mutation with Apollo Client (query successful)

Currently, I am using apollo-client on my web server to interact with my graphql server (which is also apollo). While I have successfully implemented a query that retrieves data correctly, I encounter new ApolloError messages when attempting a mutation. Od ...

Having trouble accessing the value of an <asp:HiddenField> using javascript

Currently, I am setting a value to a hidden field and attempting to retrieve that value in my JavaScript code. In the declarative part of my code: <asp:HiddenField ID="chkImages" runat="server" /> <div id="main" runat="server" style="display:non ...

What is the best way to serve index.html from a Node.js/Express application?

I am attempting to serve the index.html file from my node/express app, but I am encountering some difficulties. This is my server.js: const express = require('express'); const path = require('path'); const http = require('http&ap ...

When making xmlhttp requests, IE9 will prioritize loading from the cache rather than from the server when the data is available

During my local development process, I've been utilizing this AJAX code: function getChart(num,ld,margin,idr) { idr = typeof(idr) != 'undefined' ? idr : 0; $(ld).style.display="inline-block"; if (window.XMLHttpRequest) { ...

Fetching chat messages using an AJAX script depending on the timestamp they were sent

I am currently working on developing a real-time chat application with various rooms as part of my project. Progress has been smooth so far, but I am facing an issue with properly fetching the most recent chat messages from the database. The message table ...

Incorporating external function from script into Vue component

Currently, I am attempting to retrieve an external JavaScript file that contains a useful helper function which I want to implement in my Vue component. My goal is to make use of resources like and https://www.npmjs.com/package/vue-plugin-load-script. Thi ...

What is the best way to isolate particular components of an argument and store them in separate variables?

Currently, I am facing a challenge in extracting the name and id of an emoji from a discord argument using discord.js. The input provided to me is <:hack_wump:670702611627769876>, and my goal is to retrieve var id = '670702611627769876' alo ...

React code displaying misalignment between label and input

Can you help me align my URL and https:// on the same margin? https://i.sstatic.net/hxkkC.png <div className="card"> <div className="card-body"> <div className="form-group col-12"> <label cla ...

Implement a unique navigation bar for each page using layout.js in Next.js

https://i.stack.imgur.com/Ha3yC.pngI have a unique setup in my next js project with two different navbar layouts and ten pages. I'm looking to assign navbar one to five pages and navbar two to the remaining pages using layout.js. Can anyone provide gu ...

Sending a value to a different Ionic page based on a specific condition

Here is the code snippet from my app.js: var app = angular.module('dbpjkApps', ['ionic']) app.controller('kategoriCtrl', function($scope) { $scope.listKat = [ {kat: 'math'}, {kat: 'physics'}, ...

Can anyone tell me the Ionic framework's counterpart to Android Webview?

Recently, I designed a unique IONIC/Angular JS application and I am currently exploring various ways to enhance its features. One particular feature that I have been contemplating is the ability to load a web page within my application, similar to how it ...

The Angular directive fails to function properly without utilizing the timeout service

I have implemented a directive in AngularJS to display data retrieved from a remote database using an HTTP service. When I include the following timeout service in my code, the results are shown on the HTML page within one second because the HTTP service ...

In search of an improved scoring system for matching text with JavaScript

For many of my projects, I've relied on String Score to assist with sorting various lists such as names and countries. Currently, I am tackling a project where I need to match a term within a larger body of text, like an entire paragraph. Consider t ...

What is the best way to transmit a two-dimensional array using ajax?

Here is the process I use to send data to the server: var points = []; var coords = polyline.geometry.getCoordinates(); for (var i = 0; i < coords.length; i++) { var x = (coords[i][0]).toFixed(4); var y = (coords[i][1]).toFixed(4); points[i ...