AngularJS is causing the D3.js Bubble chart graph to not display properly

I've been attempting to enhance my Bubble chart graph created with D3.js by incorporating AngularJS, but I'm facing some difficulties. Despite going through this tutorial, nothing seems to be working as expected.

Below is the code I have written:

var d3DemoApp = angular.module('d3DemoApp', []);

// controller logic
d3DemoApp.controller('AppCtrl', function AppCtrl ($scope) {

    $scope.data = './data.json';

    $scope.getCommitData = function () {
        $http({
            method: 'GET',
            url: $scope.data
        }).
        success(function (data) {
            // assign data to the scope
            $scope.data = data;

            // clear any error messages
            $scope.error = '';
        }).
        error(function (data, status) {
            $scope.error = 'Error: ' + status;
        });
    };

    // retrieve commit data immediately
    $scope.getCommitData();
});

d3DemoApp.directive('ghVisualization', function () {

    return {
        restrict: 'E',
        scope: {
            val: '=',
            grouped: '='
        },
        link: function (scope, element, attrs) {
            var diameter = 900,
                format = d3.format(",d"),
                color = d3.scale.category20c();

            var bubble = d3.layout.pack()
                .sort(null)
                .size([diameter, diameter])
                .value(function(d) { return (d.value+1); })
                .padding(1.5);

            var svg = d3.select("body").append("svg")
                .attr("width", diameter)
                .attr("height", diameter)
                .attr("class", "bubble");

            scope.data = getCommitData();

            var node = svg.selectAll(".node")
                .data(bubble.nodes(classes($scope.data))
                    .filter(function (d) {
                        return !d.children;
                    }))
                .enter().append("g")
                .attr("class", "node")
                .attr("transform", function (d) {
                    return "translate(" + d.x + "," + d.y + ")";
                });

            node.append("title")
                .text(function (d) {
                    return d.className + ": " + format(d.value);
                });

            node.append("circle")
                .attr("r", function (d) {
                    return d.r;
                })
                .style("opacity", "0")
                .style("fill", function (d) {
                    return "red";
                });

            node.append("text")
                .attr("dy", ".3em")
                .style("text-anchor", "middle")
                .text(function (d) {
                    return d.className.substring(0, d.r / 3);
                })
                .style("opacity", "0");

            node.on("click", click);
            function click(d) {
                alert("Click Event Triggered!");
                document.getElementById('myDiv').value = d.value;

            };


            // Return a hierarchy containing all leaf nodes under the root.
            function classes(root) {
                var classes = [];
                function recurse(name, node) {
                    if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
                   else classes.push({value: name, className: node.name, value: node.size, size: node.size});
                }
                recurse(null, root);
                return {children: classes};
            }
            d3.select(self.frameElement).style("height", diameter + "px");
        }
    }
});

I would greatly appreciate your assistance as I am quite stuck... Thank you very much!

Answer №1

There seems to be some missing code snippets

(i) Make sure to include ng-app in your view

(ii) Ensure that $http is injected when fetching data

(iii) Verify that the directive is declared correctly and data is being passed

Below is the View:

<div id="containerGraphBubble">
    <bubble-chart chart-data="chartData">
    </bubble-chart>
  </div>

And here is the Directive:

d3DemoApp.directive('bubbleChart', function() {
  ... // Directive functionality goes here
});

If you want to see a live example, check out this Application

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

Triple the Charts: Highcharts Vue Component combines three dynamic charts into one powerful visual tool

looking for help with creating a complex chart Hello, I have a task of creating what seems to be 3 charts in one design. I am able to create the two scattered charts, but the column charts are proving to be challenging. Thanks to Wojciech Chmiel, I manage ...

Exploring the Bounds of Mongodb's $within Query

I'm currently working on a geospatial query in mongodb using the $within operator. I have a collection entry with a location field containing: location: { bounds: { south_west: { lat: XX.XXXXXX, lng: XX.XXXXX }, north_east: { lat: XX.XXXXXX ...

Steps for aligning the upper rectangular text in the center of the larger rectangular border

https://i.stack.imgur.com/7yr5V.png I was aware of a particular element in html that had text positioned in the upper left corner, but my knowledge didn't go beyond that. Should I be adjusting the translation on both the X and Y axes based on the par ...

What steps should I take to create a .NET/JavaScript login page that works on IE7, Chrome, Mozilla, and Safari browsers?

I am currently dealing with a .NET login page that functions perfectly in Internet Explorer 7. However, my goal is to enhance its compatibility with Chrome, Mozilla, and Safari. The login code behind looks like this: Protected Sub btnLogin_Click(ByVal se ...

JavaScript vanilla can be difficult to grasp, especially when it comes to

I'm experiencing a delay in displaying and hiding the mobile menu and table of contents section on my website when viewed on a mobile device. I'm using vanilla JavaScript to add and remove classes, but it's taking about one second for the me ...

Insert a fresh row into the current table

Is it possible to add a new row to an existing table in SAP UI5? Below is the code snippet: onInit: function() { var oModel = new sap.ui.model.json.JSONModel("Model/Clothing.json"); this.getView().setModel(oModel); var table = this.getView(). ...

What is the best way to transfer data between functions?

I'm working on a fun Santa's game to play with my friends. The concept is simple: you enter your name, click a button, and a random name from the list will be displayed as the result. I've encountered a couple of challenges: I can succe ...

Having trouble importing SVG as a React component in Next.js

I initially developed a project using create-react-app with Typescript, but later I was tasked with integrating next.js into it. Unfortunately, this caused some SVGs throughout the application to break. To resolve this issue, I implemented the following pa ...

A guide to setting up a unit test for an Angular JS controller that utilizes an ajax service

I've been attempting to write unit tests for the code below, but I'm struggling to make it work. Service Code: angular.module('ActivityApp').service('PersonService', [ '$http', function ($http) { v ...

Empty req.body object in Node.js

I came across this code snippet: const bodyParser = require("body-parser"); const express = require("express"); const app = express(); app.use(express.json()); app.use(bodyParser.json()); app.post("/api/foo", (request, response) => { let foo = { ...

Apple Safari 14.0.3 restricts JavaScript from setting a cookie expiry date beyond one week from the current date

This is my second time reaching out with this question, but after plenty of trial and error experimentation, I have gathered much more information to share. I have been attempting different methods to set a cookie in Safari using JavaScript 'document ...

Show off beautiful text using a styled pre component in a React application

I've been attempting to highlight specific text within a React pre element, but unfortunately, it doesn't seem to be working as expected. Instead of displaying the highlighted text, it shows [object Object]. const Text = styled.pre ` col ...

Attempting to implement Vue js extensions without relying on NPM or webpack

The issue Currently, I am trying to follow the jqWidgets guidelines provided in the link below to create a dropdown box. However, the challenge I'm facing is that their setup involves using the IMPORT functionality which is restricted by my tech lead ...

Clicking on a flex card will trigger the opening of a new page where the parsed data will be displayed in a stylish manner using JavaScript

Currently, I am attempting to showcase the data retrieved from the following URL: . My objective is to format the parsed data with a specific style. However, I have encountered difficulties accomplishing this using `window.open()`. Any assistance in resolv ...

The issue with ui-router failing to render the template in MVC5

I'm having trouble setting up a basic Angular UI-Router configuration. My goal right now is to have a hardcoded template render properly, and then work on loading an external .html file. My project is using MVC5, so I'll provide the necessary fi ...

I would like to automatically log out when closing the tab in Mozilla, Internet Explorer 8.0, or Chrome

Hello there, I was wondering if it's feasible to end the session and log out when I close my tab. I'd appreciate it if you could confirm whether this feature has been implemented on your end. Thank you, Manish ...

Why must we always begin rotating with 0 in CSS Animation's webkit transform rotate?

Can anyone assist me with this issue? I have created a jsfiddle with an example. In summary, I am trying to rotate an "orange dart" in a circle every time the user clicks on a link. The rotation works fine, but the problem is that the "orange dart" always ...

What is the process of incorporating data from PHP into AngularJS?

Looking for ways to transfer data from a PHP array to AngularJS. I attempted the following: HTML <? foreach ($new->images as $val): ?> <div ng-init="func(<?=$val?>)"></div> <? endforeach; ?> AngularJS: $ ...

Encountering an unexpected identifier error in Sails JS

As a newcomer to Sails JS, I am attempting to perform CRUD operations with it. However, I am encountering an unexpected error in my index function. I am struggling to identify where the mistake lies. // Usercontroller.js file module.exports = { creat ...

When the user clicks on an organizational chart, a new organizational chart will appear in a modal popup

Currently, I am developing a project with Highcharts where I have a specific requirement to display a modal popup when a node on an org chart is clicked. The popup should also contain another org chart. Below is the code snippet I am working with: [link to ...