Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7889987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:14:17+00:00 2026-06-03T06:14:17+00:00

To the esteemed readers. I’m reasonably new in javascript and I have come across

  • 0

To the esteemed readers. I’m reasonably new in javascript and I have come across this problem. I’m trying to implement a modified version of this force directed graph:

http://mbostock.github.com/d3/ex/force.html

The json data is generated on the fly from a php script. The idea is to color all lines connecting to one specific node ( defined in a php script) in one color and all the others in shades of gray. I’m attempting to do it by matching the source variable in the json file to the variable from the php script and changing color when that is true like this:

  var link = svg.selectAll("line.link")
  .data(json.links)
  .enter().append("line")
  .attr("class", "link")
  .style("stroke-width", function(d) { return Math.sqrt(d.value);})
  .style("stroke-opacity", function(d) { return d.value/10;})
  .style("stroke", function(d) { 
  x = (tested == d.source) ?  return '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
  })

however this does not work. The script works fine but without the color change if I just do this

  var link = svg.selectAll("line.link")
  .data(json.links)
  .enter().append("line")
  .attr("class", "link")
  .style("stroke-width", function(d) { return Math.sqrt(d.value);})
  .style("stroke-opacity", function(d) { return d.value/10;})
  .style("stroke", function(d) { 
  return '#707070';
  })

I’ve been staring at this for days trying to figure out to get this done and I’m stuck. Any help would be greatly appreciated!!

Here is my complete script

<script type="text/javascript">

var width = 1200,
    height = 1200;

var color = d3.scale.category20();

var tested=<?php echo $tested_source;?>; //<-- the variable from php

var svg = d3.select("#chart").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.json("data.json", function(json) {

var force = d3.layout.force()
    .charge(-130)
    .linkDistance(function(d) { return 500-(50*d.value);})
    .size([width, height]);

  force
      .nodes(json.nodes)
      .links(json.links)
      .start();

  var link = svg.selectAll("line.link")
      .data(json.links)
      .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value);})
      .style("stroke-opacity", function(d) { return d.value/10;})
      .style("stroke", function(d) { 
      x = (tested == d.source) ?  return '#1f77b4' : '#707070'; //<-- Attempt to change the color of the link when this is true. But is is not working...  :(
      })


  var node = svg.selectAll("circle.node")
      .data(json.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", 12)
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

  node.append("title")
      .text(function(d) { return d.name; });

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });
});

</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:14:18+00:00Added an answer on June 3, 2026 at 6:14 am

    d.source is an object, you can’t use == to determine if tested is a similar object. Have a look at this answer for more details on object equality.

    If you want to test for a specific value of the d.source object described below, which I assume you want, you need to specify it.

    Here is the source object architecture : (I’m using the example you pointed so the data comes from the miserables.json)

    source: Object
        group: 4
        index: 75
        name: "Brujon"
        px: 865.6440689638284
        py: 751.3426708796574
        weight: 7
        x: 865.9584580575608
        y: 751.2658636251376
    

    Now, here is the broken part in your code :

    x = (tested == d.source) ?  return '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
    

    It doesn’t work because the return is misplaced.
    You’re mixing ternary and return statements but you don’t put them in the right order :

    return test ? value_if_true : value_if_false;
    

    if you want to assign the value to x anyway, you can do

    x = test ? value_if_true : value_if_false;
    return x;
    

    You should do something like this :

    return (tested == d.source) ? '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
    

    That’s for the general syntax, but this won’t work as is You need to pick one of the value for your test for example :

    return (tested === d.source.name) ? '#1f77b4' : '#707070';
    

    Also, if the variable from PHP is a string you should do

    var tested="<?php echo $tested_source;?>"; //<-- the variable from php
    

    and in most cases you should use json_encode to map PHP variables into javascript ones.

    As a final note, I would recommend using console functions coupled with Firebug‘s console panel if you’re using Firefox, or the Chrome Developer Tool‘s console panel if you’re using a Chromium based browser. It would allow you to debug your code more easily.


    Working code

    var width = 960,
      height = 500;
    
    var color = d3.scale.category20();
    
    var force = d3.layout.force().charge(-120).linkDistance(30).size([width, height]);
    
    var svg = d3.select("#chart").append("svg").attr("width", width).attr("height", height);
    
    var tested = 20;
    
    d3.json("miserables.json", function (json) {
      force.nodes(json.nodes).links(json.links).start();
    
      var link = svg.selectAll("line.link")
      .data(json.links)
      .enter()
      .append("line")
      .attr("class", "link")
      .style("stroke-width", function (d) {
        return Math.sqrt(d.value);
      }).style("stroke-opacity", function (d) {
        return d.value / 10;
      }).style("stroke", function (d) {
        return (tested == d.source.index) ? '#ee3322' : '#707070'; //'#1f77b4'
      });
    
      var node = svg.selectAll("circle.node")
      .data(json.nodes)
      .enter()
      .append("circle")
      .attr("class", "node")
      .attr("r", 5)
      .style("fill", function (d) {
        return color(d.group);
      }).call(force.drag);
    
      node.append("title").text(function (d) {
        return d.name;
      });
    
      force.on("tick", function () {
        link.attr("x1", function (d) {
          return d.source.x;
        }).attr("y1", function (d) {
          return d.source.y;
        }).attr("x2", function (d) {
          return d.target.x;
        }).attr("y2", function (d) {
          return d.target.y;
        });
    
        node.attr("cx", function (d) {
          return d.x;
        }).attr("cy", function (d) {
          return d.y;
        });
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Esteemed Overflow-ers of the Stack, So I've recently been trying to fully wrap my
Gentlemen and esteemed ladies. I posted this question in the COM forum at Code
I use javascript to detect long press( a touchstart event then rare touchmove events
Might the esteemed os.system of Python fame throw an exception? If so, which ones?
I'm haiving an interesting discussion with an esteemed colleague and would like some additional
This is the situation: A user filters a database by selecting keywords from a
Problem: By Rosa Golijan <br/><br/>The next time someone interrupts you to proclaim that retweet
Alrighty, I don't know how I did this, probably the AS3 gods became angry
i am using php,mysql,jquery. When a user clicks on save i go to back
In an Xcode project, if I use std::cout to write to the console the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.