Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
              
            
!

CSS

              
                body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  background-color: #dedede;
}


#chartdiv {
  width: 100%;
  height: 500px;
}
              
            
!

JS

              
                /**
 * ---------------------------------------
 * This demo was created using amCharts 4.
 *
 * For more information visit:
 * https://www.amcharts.com/
 *
 * Documentation is available at:
 * https://www.amcharts.com/docs/v4/
 * ---------------------------------------
 */

// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

var chart = am4core.create("chartdiv", am4charts.ChordDiagram);
var currentNode;
var currentLink;

chart.padding(10, 10, 10, 10);
// colors of main characters
chart.colors.saturation = 0.45;
chart.colors.step = 3;
var colors = {
  Rachel: chart.colors.next(),
  Monica: chart.colors.next(),
  Phoebe: chart.colors.next(),
  Ross: chart.colors.next(),
  Joey: chart.colors.next(),
  Chandler: chart.colors.next()
};

// data was provided by: https://www.reddit.com/user/notrudedude

chart.data = [
  // node property fields take data from data items where they are first mentioned, that's
  // why we add empty data items at the beginning and set colors here
  { from: "Monica", image: "monica.png", color: colors.Monica },
  { from: "Rachel", image: "rachel.png", color: colors.Rachel },
  { from: "Chandler", image: "chandler.png", color: colors.Chandler },
  { from: "Ross", image: "ross.png", color: colors.Ross },
  { from: "Joey", color: colors.Joey, image: "joey.png" },
  { from: "Phoebe", image: "phoebe.png", color: colors.Phoebe },

  // real data
  { from: "Monica", to: "Rachel", value: 4 },
  { from: "Monica", to: "Chandler", value: 113 },
  { from: "Monica", to: "Ross", value: 16 },
  { from: "Monica", to: "Joey", value: 9 },
  { from: "Monica", to: "Phoebe", value: 3 },
  { from: "Monica", to: "Paul the wine guy", value: 1 },
  { from: "Monica", to: "Mr Geller", value: 6 },
  { from: "Monica", to: "Mrs Geller", value: 5 },
  { from: "Monica", to: "Aunt Lilian", value: 1 },
  { from: "Monica", to: "Nana", value: 1 },
  { from: "Monica", to: "Young Ethan", value: 5 },
  { from: "Monica", to: "Ben", value: 3 },
  { from: "Monica", to: "Fun Bobby", value: 3 },
  { from: "Monica", to: "Richard", value: 16 },
  { from: "Monica", to: "Mrs Green", value: 1 },
  { from: "Monica", to: "Paolo2", value: 1 },
  { from: "Monica", to: "Pete", value: 10 },
  { from: "Monica", to: "Chip", value: 1 },
  { from: "Monica", to: "Timothy (Burke)", value: 1 },
  { from: "Monica", to: "Emily", value: 1 },
  { from: "Monica", to: "Dr. Roger", value: 3 },
  { from: "Rachel", to: "Chandler", value: 7 },
  { from: "Rachel", to: "Ross", value: 80 },
  { from: "Rachel", to: "Joey", value: 30 },
  { from: "Rachel", to: "Phoebe", value: 6 }
];

chart.dataFields.fromName = "from";
chart.dataFields.toName = "to";
chart.dataFields.value = "value";

chart.nodePadding = 0.5;
chart.minNodeSize = 0.01;
chart.startAngle = 80;
chart.endAngle = chart.startAngle + 360;
chart.sortBy = "value";
chart.fontSize = 10;

var nodeTemplate = chart.nodes.template;
nodeTemplate.readerTitle = "Click to show/hide or drag to rearrange";
nodeTemplate.showSystemTooltip = true;
nodeTemplate.propertyFields.fill = "color";
nodeTemplate.tooltipText = "{name}'s kisses: {total}";


// EVENT:CLICK -> Disable default click behavior and enable new behavior
nodeTemplate.properties.fillOpacity = 0.4; // default styles
nodeTemplate.properties.strokeWidth = 1;
nodeTemplate.properties.strokeOpacity = 0;
//nodeTemplate.properties.stroke = am4core.color('#000');

nodeTemplate.events.off("hit");
nodeTemplate.events.on("hit", function (event) {
  var node = event.target;
  toggleNode(node);
});

// HOVER
createHoverState(nodeTemplate);
// when rolled over the node, make all the links rolled-over
nodeTemplate.events.on("over", function (event) {
  var node = event.target;
  node.outgoingDataItems.each(function (dataItem) {
    if (dataItem.toNode) {
      dataItem.link.isHover = true;
      dataItem.toNode.label.isHover = true;
    }
  });
  node.incomingDataItems.each(function (dataItem) {
    if (dataItem.fromNode) {
      dataItem.link.isHover = true;
      dataItem.fromNode.label.isHover = true;
    }
  });

  node.label.isHover = true;
});
nodeTemplate.events.off("out");
nodeTemplate.events.on("out", function (event) {
  var node = event.target;
   node.outgoingDataItems.each(function (dataItem) {
    if (dataItem.toNode) {
      dataItem.link.isHover = false;
      dataItem.toNode.label.isHover = false;
    }
  });
  node.incomingDataItems.each(function (dataItem) {
    if (dataItem.fromNode) {
      dataItem.link.isHover = false;
      dataItem.fromNode.label.isHover = false;
    }
  });
  setTimeout(() => {
    keepSelected();
  }, 10);
});

var label = nodeTemplate.label;
label.relativeRotation = 90;

label.fillOpacity = 0.4;
let labelHS = label.states.create("hover");
labelHS.properties.fillOpacity = 1;

nodeTemplate.cursorOverStyle = am4core.MouseCursorStyle.pointer;
// this adapter makes non-main character nodes to be filled with color of the main character which he/she kissed most
nodeTemplate.adapter.add("fill", function (fill, target) {
  let node = target;
  let counters = {};
  let mainChar = false;
  node.incomingDataItems.each(function (dataItem) {
    if (colors[dataItem.toName]) {
      mainChar = true;
    }

    if (isNaN(counters[dataItem.fromName])) {
      counters[dataItem.fromName] = dataItem.value;
    } else {
      counters[dataItem.fromName] += dataItem.value;
    }
  });
  if (mainChar) {
    return fill;
  }

  let count = 0;
  let color;
  let biggest = 0;
  let biggestName;

  for (var name in counters) {
    if (counters[name] > biggest) {
      biggestName = name;
      biggest = counters[name];
    }
  }
  if (colors[biggestName]) {
    fill = colors[biggestName];
  }

  return fill;
});

// link template
var linkTemplate = chart.links.template;
linkTemplate.strokeOpacity = 0;
linkTemplate.fillOpacity = 0.15;
linkTemplate.tooltipText = "{fromName} & {toName}:{value.value}";
//https://www.amcharts.com/docs/v4/tutorials/manipulating-mouse-cursor-style/
linkTemplate.cursorOverStyle = am4core.MouseCursorStyle.pointer;

createHoverState(linkTemplate);

linkTemplate.events.off("hit");
linkTemplate.events.on("hit", function (event) {
  var link = event.target;
  toggleLink(link);
});
linkTemplate.events.on("over", function (event) {
  var link = event.target;
  link.dataItem.fromNode.isHover = true;
  link.dataItem.toNode.isHover = true;
});
linkTemplate.events.on("out", function (event) {
  var link = event.target;
  link.dataItem.fromNode.isHover = false;
  link.dataItem.toNode.isHover = false;
  
  setTimeout(() => {
    keepSelected();
  }, 10);
});

// data credit label
var creditLabel = chart.chartContainer.createChild(am4core.TextLink);
creditLabel.text = "Data source: notrudedude";
creditLabel.url = "https://www.reddit.com/user/notrudedude";
creditLabel.y = am4core.percent(99);
creditLabel.x = am4core.percent(99);
creditLabel.horizontalCenter = "right";
creditLabel.verticalCenter = "bottom";

var titleImage = chart.chartContainer.createChild(am4core.Image);
titleImage.href = "//www.amcharts.com/wp-content/uploads/2018/11/whokissed.png";
titleImage.x = 30;
titleImage.y = 30;
titleImage.width = 200;
titleImage.height = 200;

function createHoverState(item) {
  var state = item.states.create("hover");
  state.properties.fillOpacity = 0.7;
  state.properties.strokeOpacity = 0.7;
}

function setSelectedState(element) {
  if (!element.states.hasKey("selected")) {
    var state = element.states.create("selected");
    state.properties.fillOpacity = 1;
    state.properties.strokeOpacity = 1;
    // state.properties.stroke = am4core.color("#000", 0.5);
    state.properties.scale = 1.025;
  }
  element.setState("selected");
}

function setRelatedState(element) {
  if (!element.states.hasKey("related")) {
    var state = element.states.create("related");
    state.properties.fillOpacity = 1;
    state.properties.strokeOpacity = 1;
    state.properties.scale = 1.025;
    //state.properties.stroke = am4core.color("#ff0000");
  }
  element.setState("related");
}

function selectNode(node) {
  currentLink = null;
  currentNode = node;
  setSelectedState(currentNode);
  node.incomingDataItems.each(function (dataItem) {
    setRelatedState(dataItem.link);
  });
  node.outgoingDataItems.each(function (dataItem) {
    setRelatedState(dataItem.link);
  });
}

function toggleNode(node) {
  resetSelectedState();
  if (node === currentNode) {
    currentNode = null;
  } else {
    selectNode(node);
  }
}

function selectLink(link) {
  currentNode = null;
  currentLink = link;
  setSelectedState(currentLink);
  setRelatedState(link.dataItem.fromNode);
  setRelatedState(link.dataItem.toNode);
}

function toggleLink(link) {
  resetSelectedState();
  if (link === currentLink) {
    currentLink = null;
  } else {
    selectLink(link);
  }
}

function resetSelectedState() {
  setStateWithChildren(currentNode);
  setStateWithChildren(currentLink);
}

function setStateWithChildren(element, stateName) {
  if (!element) {
    return;
  }
  if (!stateName) {
    stateName = "default";
  }
  element.setState(stateName);

  if (element.incomingDataItems) {
    element.incomingDataItems.each(function (dataItem) {
      dataItem.link.setState(stateName);
    });
  }
  if (element.outgoingDataItems) {
    element.outgoingDataItems.each(function (dataItem) {
      dataItem.link.setState(stateName);
    });
  }
  if (element.dataItem.fromNode) {
    element.dataItem.fromNode.setState(stateName);
  }
  if (element.dataItem.toNode) {
    element.dataItem.toNode.setState(stateName);
  }
}

function keepSelected() {
  if (currentNode) {
    currentNode.setState("selected");
    currentNode.incomingDataItems.each(function (dataItem) {
      dataItem.link.setState("related");
    });

    currentNode.outgoingDataItems.each(function (dataItem) {
      dataItem.link.setState("related");
    });
  }
  if (currentLink) {
    currentLink.setState("selected");
    currentLink.dataItem.fromNode.setState("related");
    currentLink.dataItem.toNode.setState("related");
  }
}

function isSelected(target) {
  return currentLink === target || currentNode === target;
}

function isInSelected(target) {
  if (target.incomingDataItems) {
    if (target === currentNode) {
      return true;
    }
    var result = false;
    target.incomingDataItems.each(function (dataItem) {
      result = dataItem.link === currentLink;
    });
    return result;
  }
  if (target.outgoingDataItems) {
    if (target === currentNode) {
      return true;
    }
    var result = false;
    target.outgoingDataItems.each(function (dataItem) {
      result = dataItem.link === currentLink;
    });
    return result;
  }
  if (target.dataItem.fromNode) {
    if (target === currentLink) {
      return true;
    }
  }
  if (target.dataItem.toNode) {
    if (target === currentLink) {
      return true;
    }
  }
}

function clearNodesAndLinksState() {
  chart.nodes.each((k, v) => {
    var node = chart.nodes.getKey(k);
    node.setState("default");
    node.incomingDataItems.each(function (dataItem) {
      dataItem.link.setState("default");
    });
    node.outgoingDataItems.each(function (dataItem) {
      dataItem.link.setState("default");
    });
  });
}

              
            
!
999px

Console