Using numerical symbols in multiple tooltips to convert dates into timestamps in the R programming language

When using the highcharter package in R, I am attempting to plot multiple line charts simultaneously. The challenge arises when dealing with different datasets containing values ranging from millions to billions, requiring a numeric symbols formatter implemented through JS. However, this approach unintentionally modifies date formats and groups all information into a single box.

My goal is to generate line plots resembling those depicted in this image.

Data:

structure(list(Date = structure(c(17256, 17347, 17439, 17531, 
17621, 17712, 17804, 17896, 17986, 18077, 18169, 18261, 18352, 
18443, 18535, 18627, 18717, 18808, 18900), class = "Date"), Amount = c(23046000, 
22207000, 16127000, 15341000, 16529000, 16646000, 27638000, 25777000, 
28478000, 20579000, 13703000, 24954000, 27012000, 18010000, 13332000, 
21300000, 15122000, 17606000, 17110000)), row.names = c(NA, -19L
), class = c("data.table", "data.frame"))


structure(list(Date = structure(c(17256, 17347, 17439, 17531, 
17621, 17712, 17804, 17896, 17986, 18077, 18169, 18261, 18352, 
18443, 18535, 18627, 18717, 18808, 18900), class = "Date"), Amount = c(2025000, 
2217000, 2122000, 2893000, 3352000, 2837000, 4073000, 1916000, 
2170000, 2663000, 11215000, 11834000, 10065000, 5382000, 6461000, 
4929000, 5282000, 4386000, 5186000)), row.names = c(NA, -19L), class = c("data.table", 
"data.frame"))

Code:

library(highcharter)
library(dplyr)
highchart(type = "stock") %>% 
   hc_add_series(data= data1,hcaes(x= Date, y = Amount),type = "line") %>% 
   hc_add_series(data= data2,hcaes(x= Date, y = Amount),type = "line")

Sample utilizing a tooltip formatter:

library(highcharter)
library(dplyr)
js <- JS("function () {
function test(labelValue) {
  const sign = Math.sign(Number(labelValue));
  // Nine Zeroes for Billions
  return Math.abs(Number(labelValue)) >= 1.0e12
    ? sign * (Math.abs(Number(labelValue)) / 1.0e12).toFixed(2) + 'T'
    : // Six Zeroes for Millions
    Math.abs(Number(labelValue)) >= 1.0e9
    ? sign * (Math.abs(Number(labelValue)) / 1.0e9).toFixed(2) + ' B'
    : // Three Zeroes for Thousands
    Math.abs(Number(labelValue)) >= 1.0e6
    ? sign * (Math.abs(Number(labelValue)) / 1.0e6).toFixed(2) + ' M'
    : // Three Zeroes for Thousands
    Math.abs(Number(labelValue)) >= 1.0e3
    ? sign * (Math.abs(Number(labelValue)) / 1.0e3).toFixed(2) + ' K'
    : Math.abs(Number(labelValue));
    
};
            return this.points.reduce(function (s, point) {
                return  s   + '<br/>' + point.series.name + ': ' +
                    test(point.y);
            }, '<b>' + this.x + '</b>');
            }
        ")

highchart(type = "stock") %>% 
   hc_add_series(data= data1,hcaes(x= Date, y = Amount),type = "line") %>% 
   hc_add_series(data= data2,hcaes(x= Date, y = Amount),type = "line") %>%
  hc_tooltip(formatter = js)


Output: with formatted tooltips

Answer №1

Consider utilizing the tooltip.split configuration option if you wish to maintain the split of the tooltip data. Find more information here

library('highcharter')

highchart() %>%
  hc_add_series(
    type = "line",
    data = list(5, 4, 3, 5)
  ) %>%
  hc_add_series(
    type = "line",
    data = list(15, 14, 13, 15)
  ) %>%
  hc_tooltip(formatter = JS("function () {
            // The first returned item is the header, subsequent items are the
            // points
            return ['<b>' + this.x + '</b>'].concat(
                this.points ?
                    this.points.map(function (point) {
                        return point.series.name + ': ' + point.y + 'm';
                    }) : []
            );
        }"), split = TRUE) %>%
  hc_chart(events = list(load = JS("function() {
      var chart = this; chart.update({
        chart: {
          backgroundColor: '#FCFFC5'
        }
      });
    }
    ")
  ))

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

403 Malicious Path Middleware Error in Express.js

Encountering an error when sending a post request to my server, but only on the production server - whereas the staging server is functioning properly. Both servers are hosted on AWS Ubuntu instances. Investigating the stack trace, it appears that the err ...

Issue with displaying PDF files on Google Chrome due to a software glitch

Recently, I encountered a puzzling issue on Google Chrome. I am not sure if the error lies with Google or within my code. When I set the div as display: none; and then show it again, the PDF view only shows a grey background. However, if I zoom in or out, ...

How can you modify Jquery show_hide so that the page automatically scrolls to the bottom of the concealed field when the button is clicked?

On my website, there is a button that reveals a map when clicked. The button is visible when the page loads, but once clicked, the map slides open and goes out of sight since the page loads at the top as usual. My goal is to have the page automatically scr ...

Exploring the Google Maps API Search feature

I am currently developing a JavaScript application that interfaces with the Google Maps API. The program has the following requirements: Enable users to input a location. Upon clicking the "find" button, convert the user's entered location into long ...

Embed an array within a div using JavaScript

I'm looking to make a small adjustment to this code, acknowledging that it's far from perfect. Instead of simply writing the array contents into a single div, I'd like to create a new div for each number in the array and then add it to the c ...

Generating an in-page anchor using CKeditor 5

We are currently integrating CKEditor 5 into one of our projects. Our goal is to enable the end-user to generate an in-page anchor tag that can be accessed through other links (e.g., <a name='internalheading'> which can be navigated to via ...

The exported NextJS URL isn't functioning properly

Recently, I delved into the world of Next JS by following a tutorial on YouTube by Brad Traversy. In his guidance, I used next export to export the program and ran it using serve -s out -p 8000. While the page loads perfectly on localhost:8000, the issue a ...

What is the essential Angular 2 script that must be included for a simple Angular 2 application to function properly?

I'm currently working through the latest Tuts+ tutorial on Angular 2 In the tutorial, the author references adding this script: <script src="node_modules/angular2/bundles/angular2.sfx.dev.js"></script> However, in the most recent beta re ...

What could be the reason for my function not being executed in this particular scenario with my calculator HTML code?

Memory = "0"; Current = "0"; Operation = 0; MAXLENGTH = 30; alert("yea"); function AddDigit(digit) { alert("yea"); if (Current.length > MAXLENGTH) { Current = "Aargh! Too long"; } else { if (eval(Current) == 0) { Current = dig; ...

Leveraging R within a Snakemake pipeline powered by Mambaforge

As I construct a pipeline using Snakemake, one of my rules involves an R script that utilizes the readr package to read a CSV file. However, I encounter an error when executing the pipeline with both --use-singularity and --use-conda. Error: Unknown TZ UTC ...

Timing of Vue mounting and initialization phases

I am currently working on a component where I pass the reference to an internal element to the parent using defineExpose. In this example, I simply log the element, but in my actual project, I intend to manipulate it. SFC Playground // Comp.vue <scrip ...

Disable the ability to click on the indicators in the Bootstrap carousel

Seeking assistance with customizing tweeters bootstrap Carousel functionality. I have removed the controls and now want to switch slides according to my own logic while displaying slide indicators. How can this be achieved? Preferably seeking a solution ...

Encountering a peculiar problem with the Bootstrap Carousel where the first slide fails to load

There seems to be an issue with the bootstrap carousel where the first slide appears empty initially, but once you slide to the second slide it starts working fine. Navigating through all slides is still possible. <div id="mediakit_carousel" class="car ...

Having trouble retrieving the `Content-Disposition` information from the backend response

I've been attempting to retrieve the value of Content-Disposition from the backend response, but unfortunately, I have not been successful. Here is the code I have been working with: public getQuoteImage(sharedQuote):Observable<any> { retu ...

Fill the drop-down menu with the present day's date, month, and year

I'm new to this, so please bear with me. I have some html and jQuery code: $(document).ready(function() { var d = new Date(); var month = d.getMonth() + 1; var year = d.getFullYear(); $("#month").val(month); $("#year").val(year) ...

Guide on properly documenting custom function types in JSDoc or TypeScript to ensure accurate referencing for VSCode IntelliSense functionality

I am currently working on documenting custom function types within an object and would greatly appreciate any assistance: A Closer Look at the Issue Consider this basic object declaration with several function properties (addCoordinate, addCoordinateOne, ...

Firefox compatibility issue caused by jQuery's appendTo function disrupting hyperlink functionality

I've successfully implemented the material design ripple effect using jQuery, which functions well in IE11 and Chrome 46. However, I've encountered an issue in Firefox 39 where applying the effect to links prevents redirection. Upon investigation ...

Having trouble sending a function as a prop to a child component in React

Something odd is happening, I'm confident that the syntax is correct, but an error keeps popping up: Error: chooseMessage is not a function // MAIN COMPONENT import React, { useState } from 'react' export default function LayoutMain(prop ...

Tips for optimizing node_module file size

Currently, I'm developing 2-3 react applications and noticed that every time I run npm install, it ends up downloading numerous dependencies into the cumbersome "node-modules" folder, totaling around 250-300mb in size! This large size makes it challen ...

Webpack returns an undefined error when attempting to add a JavaScript library

I am a newcomer to webpack and I am attempting to incorporate skrollr.js into my webpack setup so that I can use it as needed. However, I am unsure of the correct approach for this. After some research, I have found that I can either use an alias or export ...