Elements in the rCharts flow chart

I have implemented the code below to create an rCharts Sankey diagram, sourced from https://github.com/timelyportfolio/rCharts_d3_sankey:

if(!require(rCharts)){
  library(devtools)
  install_github('ramnathv/rCharts')
}
library(rCharts)

sankeyPlot <- rCharts$new()
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$set(
  data = data.frame(source=c('Cold','Warm','Total'),target=c('Total','Total','End'),value=c(20,80,100)),
  nodeWidth = 15,
  nodePadding = 10,
  layout = 32,
  width = 500,
  height = 300,
  units = "TWh",
  labelFormat = ".1%"
)
sankeyPlot$setTemplate(
  afterScript = "
  <script>
  // to be specific in case you have more than one chart
  d3.selectAll('#{{ chartId }} svg path.link')
  .style('stroke', function(d){
  //here we will use the source color
  //if you want target then sub target for source
  //or if you want something other than gray
  //supply a constant
  //or use a categorical scale or gradient
  return d.source.color;
  })
  </script>
  ")

sankeyPlot

In my implementation of Sankeyplot$set, I specified a value for units. However, I am unable to see either the units or the values on the chart produced. The example with units is taken from the official github documentation (example_hirst_f1.R). How can I display both the values and the units in my chart?

https://i.sstatic.net/ZPLm2.png

Answer №1

The sankeyPlot output generates an svg g element with the class of "node". Within this element, the value and its corresponding unit are included within a hidden title element nested inside a rect element that represents the node. The name, on the other hand, is displayed in a visible text element (in this case, "warm").

To view this structure, one can right-click in the Rstudio's view window and select "inspect".

Image showing the node structure in Web Inspector

A simple solution would be to add the value and its unit to the text element as well. This can be achieved by replacing line 105 in layout/charts.html with:

.text(function (d) { return d.name + " " + format(d.value); })

and using this updated code as a template going forward.

While there may be more optimal solutions available, it's possible that the title element serves a specific purpose (such as for a mouseover event). Nonetheless, this adjustment is a step in the right direction. Hopefully, it proves to be helpful.

Answer №2

Inserting JeroenDM's answer into the afterscript allows for seamless integration of the Github repository without any modifications needed.

sankeyPlot$setTemplate(
  afterScript = "
  <script>
  // Make specific adjustments if you have multiple charts
  d3.selectAll('#{{ chartId }} svg path.link')
  .style('stroke', function(d){
  // Use the color of the source
  // Adjust for target or specify a different color if desired
  return d.source.color;
  })
  units = ' TWh'

  var formatNumber = d3.format('0,.0f'),    // zero decimal places
  format = function(d) { return formatNumber(d) + units; }

  d3.selectAll('#{{ chartId }} svg .node text')
  .text(function (d) { return d.name + ' (' + format(d.value) + ')'; })
  </script>
  ")

https://i.sstatic.net/ybTCp.png

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

Navigating the route on express.js using two strings can be done by matching them effectively

I have a single route function that should match two different paths. The goal is to create profile pages for team members on a website, which can be accessed at www.domain.com/name. However, some team members prefer to use nicknames instead of their actua ...

unable to retrieve data from the local node.js server

When attempting to retrieve data from my local nodejs server, I encountered a strange error. The server I set up is a simple one using express. let express = require('express') let server = express() server.listen(3100) server.get("/", (req, res) ...

Issues with using a personalized font in a Stenciljs project

Looking for guidance on implementing a custom font in my Stenciljs app. I have the otf file, unsure if an npm package is necessary. Here's my code: filestructure: -src --components --assets ---Anurti-Regular.tiff ---Anurti-Regular.ttf friends-l ...

Activate scroll function exclusively upon hovering over specific object

I am working on a page with an object that allows users to zoom in and out. I want to create a special zoom function that will be triggered when the user scrolls while their cursor is hovering over this object. If the cursor moves away from the object, th ...

Ways to align the label at the center of an MUI Textfield

My goal is to center the label and helper text in the middle of the Textfield. I managed to achieve this by adding padding to MuiInputLabel-root, but it's not responsive on different screen sizes (the Textfield changes size and the label loses its cen ...

What is the process for generating an API endpoint in Next.js that includes a query string?

I am currently working on setting up an API endpoint in Next.js, specifically the following one: /api/products/[name]?keyword=${anykeyword}. To accomplish this, I understand that I have to create it within the pages/api/products/[name] directory in index ...

Sorting functionality malfunctioning after dynamically adding new content using AJAX

Greetings to all, I have a question about my views. 1- Index 2- Edit In the index view, I have a button and AJAX functionality. When I click the button, it passes an ID to the controller which returns a view that I then append to a div. The Edit view con ...

Closing the menu on image click in Ionic 2

If you're using Ionic 2, you have the ability to include the menuClose directive on a button to automatically close the side menu when the button is clicked. However, what if you want to close the menu when an image is clicked instead of a button ...

How to deactivate the <a> tag with Ant Design UI Library

Is there a method in the antd UI library to disable a link? The disabled attribute is not supported by the a tag according to MDN. This code snippet works in React but the link remains clickable when using Next.js. <Tooltip title={tooltip}> <a ...

Ruby application requires refreshing for Ajax deletions to take effect

I am currently working on developing a task management app using Rails. Each to-do list in the app contains multiple tasks, and my issue lies in deleting a completed task with Ajax without having to manually refresh the page for it to vanish. As I am still ...

Utilizing AngularJS: Dynamically employ custom directives for enhanced reusability

I am currently developing my debut single page Angular.js application and finding myself in a bit of a rut when it comes to programmatically compiling/evaluating a custom directive to insert it into the DOM from within a controller. The custom directive I ...

The post request with Postman is functional, however the AJAX post request is not working. I have thoroughly reviewed the client-side JavaScript

I encountered a problem with an endpoint designed to create an item in my application. Sending a POST request through Postman works perfectly fine, as I am using Node.js and Express for the backend: router.post("/", jwtAuth, (req, res) => { console.lo ...

Learn how to automatically retrieve messages without refreshing the page by leveraging the power of AJAX

displaying notifications with:- $call = "SELECT * FROM `chat` WHERE fromthe = '$email' and tothe='theadmin' UNION ALL SELECT * FROM `chat` WHERE fromthe = 'theadmin' and tothe='$email' order by id desc"; mysqli_quer ...

Utilizing JavaScript for the removal or hiding of span elements with specific class attributes

I am currently working on a software project that involves compiling HTML fragments and then exporting them to Microsoft Word. My goal is to create a script that will cycle through these compiled fragments and remove specific tags that have a particular CS ...

The onChange event will not be triggered in an input component that is not intended to be uncontrolled

Could someone please assist me in understanding why the onChange event is not being triggered? I am customizing ChakraUI's Input component to retrieve a value from localStorage if it exists. The component successfully retrieves the value from localS ...

Is the express.json() middleware for parsing JSON request body designed to handle synchronous calls?

According to Express.js's documentation, it is recommended to avoid using synchronous functions as much as possible. This is because in high-traffic websites, the accumulation of synchronous calls can negatively impact the performance of the applicati ...

What is preventing me from altering the array one element at a time?

I am working with an array and a class let questions = [ { questionText: '', answerOptions: [], }, ]; class Questions { constructor(questionText,answerOptions) { this.questionText = questionText; this.answerOptio ...

Executing a secondary API based on the data received from the initial API call

Currently, I am diving into the world of RxJS. In my project, I am dealing with 2 different APIs where I need to fetch data from the first API and then make a call to the second API based on that data. Originally, I implemented this logic using the subscri ...

using JQuery, add a class on click event or on page load

Solved It! After encountering a problem created by some sloppy moves on my part, I managed to solve it. However, another issue arose: adding the class current to li with data-tab="tab-1 upon page load. $('ul.tabs li').click(function(){ ...

What are some methods for creating nested asynchronous calls in jQuery?

Here is an example in main.js: $.when( $.get('foo/bar.html'), $.get('lorem/ipsum.html') ).done(function(data1, data2){ someCode(); }); In lorem/ipsum.html: $.when( $.get('otherStuff.html'), $.get(' ...