What's preventing me from drawing a line on this D3.js chart with my Angular code?

I am attempting to create a graph on my webpage using the D3.js library for JavaScript. I am able to draw a line on an SVG using HTML, but when I try to draw a line using JavaScript, I encounter the error message:

ReferenceError: d3 is not defined
.

Even though I have included the D3.js file, I still face this issue.

Could you please advise me on how to overcome this problem?

You can view the Plunker here: https://plnkr.co/edit/pdDCfYmCQxX56sBfjfzW?p=preview

Below are the relevant files for your review.

Javascript:

myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
    $stateProvider.state('myState', {url: '/myState', params: {slug: {value: null, squash: true}}, templateUrl: 'my-state-page1.html', controller: 'MyStateCtrl'});
  }
);

myApp.controller('MyStateCtrl', function($scope, $http) {
      var self = this;

      $scope.$watch(function() {return self.myDataFromAPI}, function(objVal) {
          console.log('objVal = ', objVal);
          x = objVal.origin.split('.');
          console.log("X = ", x)

          var svgContainer = d3.select("body").append("svg").attr("width", 1000).attr("height", 1000);

          var line = svgContainer.append("line").attr("x1", x[0]).attr("y1", x[1]).attr("x2", x[2]).attr("y2", x[3]).attr("stroke-width", 2).attr("stroke", "black");
        },
        true
      );


      self.httpFailure = function(response) {
        console.log('Failure');
        self.myDataFromAPI = null;
      }

      self.httpSuccess = function(response) {
        console.log('\n\n\nGot the data from the API!');
        self.myDataFromAPI = response.data;
        console.log('\n\n\self.myDataFromAPI =', self.myDataFromAPI);
      }

      $http.get(
        'https://httpbin.org/get'
      ).then(self.httpSuccess, self.httpFailure);
    }
);


myApp.directive('mypMyDirective', function() {
    return {
      restrict: 'E',
      scope: {
        myDataFromAPI: '='
      },
      controller: 'MyStateCtrl',
      controllerAs: 'myStateCtrl',
      bindToController: true,
      templateUrl: 'myD3Diagram.html'
    };
  }
);

index.html:

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.js"></script>
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    HELLO!
    <div ng-controller="MyStateCtrl as myStateCtrl">
      <myp-my-directive mydatafromapi="myStateCtrl.myDataFromAPI"></myp-my-directive>
    </div>
  </body>
</html>

myD3Diagram.html:

<svg id="my_svg_widget" width="500" height="500>
  <line x1="5" y1="5" x2="40" y2="40" stroke="gray" stroke-width="5" />
</svg>

Answer №1

The issue lies in accessing a non-secure http library.

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>

It should be

<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>

Check out the working plunk 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

Divide the data received from an AJAX request

After making my ajax request, I am facing an issue where two values are being returned as one when I retrieve them using "data". Javascript $(document).ready(function() { $.ajax({ type: 'POST', url: 'checkinfo.php', data: ...

Storing new li and ul elements to the database by utilizing an array for dynamic saving

I am in the process of creating a user interface for an application where users can add unordered lists and list items by clicking on "add question". I have successfully designed the front end part and here is a preview: However, I am facing difficulties ...

Scrolling with jQuery to create a fixed navigation bar

I'm having an issue with my navbar on my website. I used CSS and jQuery to keep it fixed at the top, but now when I click on a menu item, it doesn't scroll to that section of the page. Can anyone help me troubleshoot this problem? ...

Is there a way to extract information from the HTML source code of an external website and display it in my application?

Seeking a method to search an external URL for content that matches "title" and then display the results on my HTML page using Javascript. Despite my efforts with Javascript, I have not come across any resources that address this issue. Could it be that I ...

Working with conditional properties in an AngularJS application across two controllers

Managing Controllers in my Application In my application, I am working with two controllers: Controller 1: When a button is clicked, I open a menu and set the property this.isTelephoneMenuOpen = true;. The result is that the menu opens with options ...

Apply a CSS class when the tab key is pressed by the user

Currently in my Angular 14 project, I am working on a feature where I need to apply "display: block" to an element once the user reaches it using the tab key. However, I am struggling with removing the "display: block" when the user tabs out of the element ...

There was a hiccup in the camera positioning as the tweening began

I've been working on a basic program that involves tweening the camera to a specific position. The functionality works smoothly for the most part, however, I encounter some glitches at the start of the transition when trying to pan the camera using or ...

Conceal the div element when located beneath an ordered list with a designated

I need help hiding the display of comment information if it is a child comment. Below is my attempt to hide the div with the id "info" if the "ol" element has a class of "children". If you have another method for hiding the div when the comment is a chil ...

Steps for referencing an autogenerated id in a Firestore collection

I need assistance updating a 'progress' field in a document with an autogenerated ID (using Firestore) every time the progress button is clicked. https://i.stack.imgur.com/hZieN.png Despite my attempts, nothing seems to work. Here is the method ...

Are there any AJAX tools or packages in Node.js Express for connecting (posting/getting) with other servers and retrieving data?

Can someone please guide me on how to utilize ajax in node.js to send and receive JSON data from another server? Is there a package available that allows for this functionality, similar to jQuery's $.ajax, $.post, or $.get methods? ...

Arranging information extracted from an XML document following an ajax request

Here is a snippet of XML data sample to work with: <?xml version="1.0" encoding="ISO-8859-1"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>U ...

How can jQuery be used to split a string and isolate the desired string in the center? For example, if we have a string like: sample_str[targeted_str][other_str

http://jsfiddle.net/150aby4u/ There are several dropdown menus, each with a unique field name that is incremented. The objective is to extract the number in the middle of the string/text after making changes to the dropdown. In this case, we want to retr ...

Utilizing ng-options to dynamically populate a select element with data received from a

Currently, I am receiving a promise in the following manner. GetAgentsPromise.then(function(response) { GetAgentsPromise.then(function(response) { $scope.ClientAgents.id = response.data.d.AgentIDs; $scope ...

Enable users to input their custom code and run it when the specified conditions are met

Currently, I am developing a Multi-tenant application that requires users to input their own code and run it under specific conditions. I have several ideas in mind, but I am unsure which approach would be most effective. All the proposed methods will ha ...

Pressing the reset button on the search box triggers a JQuery and HTML function

I am new to this, so bear with me. I've simplified my styling for easy pasting here, but the concepts still apply. I have a list of items, a working search field, and functioning filter buttons. What I want is for the filter buttons to reset to &apos ...

The use of a Bootstrap row is leading to incorrect dimensions for FullPageJS

Within the body tag, I have included the following code snippet: <div id="container"> <div class="section profile"> <div class="row"> <div class="col-sm-6"> A </div> ...

How to modify a variable in the Config.json using a Discord.js command

Lately, I enhanced my bot's functionality by allowing it to retrieve the color for embeds from a file specified in my config.json. All I need to do is modify something like: "embedcolor": "00A950" to "embedcolor": "0 ...

Utilizing API data as props within an Autocomplete component in Reactjs with Material-UI

New to Reactjs, I am currently exploring the implementation of the Autocomplete component from material-ui. My goal is to pass the API link as a prop to the element. However, I'm stuck on how to pass the json label name as a prop to be used in "getOpt ...

What is the best way to transfer a querystring received in Node.js to a page being shown through res.render?

I have a nodejs file where I am rendering a page named foo.html. Within foo.html, I am using ajax to retrieve variables from a querystring and load an appropriate xml document based on those variables. The issue arises when I run my nodejs server, which is ...

The jQuery bookmarklet in the djangobyexample book is completely unresponsive

As I work my way through Django By Example, I came across a chapter where a jQuery bookmarklet is created within a Django app. This allows users to easily save jpg images from websites into their user profile area within the Django app. Although the tutor ...