Creating a Chart.js line plot with discontinuous sections (jumps)

Using Chart.js, I have created a line chart and am looking to avoid rescaling the x-axis for more sample points.

What is the most effective method to showcase jumps when y axis values change abruptly?

Ideally, I want to be able to assign two y values per x value. For example, if my data array passed in as {datasets: [{data : []}]} is [1, 2, 4, 5] with a jump at index 1, then I would like to represent it as [1, [2, 3], 4, 5].

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

Answer №1

Here is the solution you were looking for: https://jsbin.com/kacecejade/1/edit?js,output

The function loops through the provided data array and in cases where an element is an array instead of a number, it duplicates the label in order to have two labels corresponding to the two data points.

var sLabels = ['2018-10-26', '2018-11-02', '2018-11-09', '2018-11-16', '2018-11-23', '2018-11-30', '2018-12-07', '2018-12-14', '2018-12-20', '2018-12-28', '2018-12-31', '2019-01-01', '2019-01-04', '2019-01-11', '2019-01-18']
var sData = [-4.43, [-3.47, -3.34], -3.62, [-4.20, -3.70], -4.04, -3.75, -4.46, -4.50, -4.50, -4.50, -4.05, [-3.76, -3.64], [-3.38, -3.09], -3.24, -2.88]

function formatData() {
  var newLabels = []
  var newData = []  
  for (var i = 0; i < sLabels.length; i++) {
    if (Array.isArray(sData[i])) {
      for (var j = 0; j < sData[i].length; j++) {
        newLabels.push(sLabels[i])
        newData.push(sData[i][j])
      }
    } else {
      newLabels.push(sLabels[i])
      newData.push(sData[i])
    }
  }  
  return [newLabels, newData]
}

var [labels, datasetData] = formatData()

var data = {
  labels: labels,
  datasets: [{
    data: datasetData,
    lineTension: 0
  }]
}

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

Tips for showcasing an array in nested elements within an Angular mat-tree

I'm new to Angular and I need help displaying an array's values within the child elements of a material tree. Specifically, I want to show the names of fruits (such as Apple, Banana...) in the child elements. The colors and discounts from the ar ...

Utilizing SVG with an integrated script to automatically adjust to the available space within different browser

I am attempting to develop a self-contained SVG document that automatically adjusts its size and centers itself when loaded in a web browser. It's important to note that this is not referring to embedding an SVG within an HTML document. Below is the ...

Tooltip displacement on the webpage

I'm encountering an issue with the placement of tooltips in my buttons. The problem is specifically with the last, right-most button tooltip - it seems to be displaying on the left side of the page instead of below the button as intended. Check out t ...

The statement ""x=x || 4" will result in a `ReferenceError: x is not defined` because the

What is the reason behind receiving a ReferenceError: x is not defined error when using x = x || 4 or even x=(x||5), while var x = x || 4 operates as intended? ...

Utilize the bodyParser middleware within a middleware to handle the request body parsing

Currently, I am developing an Express app where most routes have a limited body size except for admin users who require unlimited body size for certain routes. To manage this, I implemented a middleware called limitBodySize using the following syntax: app. ...

Issue with Photoswipe pswp class Not Getting Properly Cleared Upon Closing Image

My website has a Photoswipe image gallery from . The issue I'm facing is that the CSS class does not reset or clear after closing the gallery for the second time. For example, when a user opens item 1, the images are loaded into the picture div via A ...

Exploring the Implementation of Multiple Form Validations in SharePoint using PreSaveAction

My knowledge of Javascript is limited to what I can gather from online resources. Currently, I'm facing a challenge with a SharePoint form where I need to set up specific validations that trigger when a user hits the "Save" button. The validations I& ...

Swapping out JSON.simple in favor of Jackson

Looking to switch from JSON.simple to Jackson in the code snippet below: JSONObject request = new JSONObject(); request.put("String key", /String value/); request.put("String key", /int value/); ... The updated version using Jackson: ObjectMapper mapper ...

Refreshing a Node.js server page upon receiving a JSON update

My web application serves as a monitoring interface for tracking changes in "objects" processed by the computer, specifically when they exceed a certain threshold. The Node Js server is running on the same machine and is responsible for displaying data in ...

What could be the reason for fetch failing to reach a node.js route consistently?

I am currently developing a nodejs server with the purpose of providing images to my frontend. In order to achieve this, I am utilizing fetch to retrieve data from my express routes. However, I have noticed that fetch does not consistently pull in the data ...

Using Ionic 2 to fetch JSON data and display it using ngFor

My script.php generates a JSON array with data from mySQL; The next step involves retrieving this JSON array via AJAX; I am looking to dynamically create divs using ngFor, but I'm unsure of how to handle the callback for the JSON array in the Ajax s ...

Pass along a JSON array from Express to Angular2

I have been working on sending a custom array filled with mongoose errors, and I am successfully creating the array. Below is the code snippet: student.save(function(err, student) { if(err) var errors = []; for (field in err.errors) { ...

Tips for correctly loading API data into dependent tableViewCells

Working on a UITableView that makes 2 API calls for every cell has been successful until today. Recently, I encountered a major issue where there were more cells on the screen than actually loaded. While scrolling down the tableView, the screen froze for ...

Angular JS model not being updated when selecting a date value with datepicker

Whenever I try to use the datepicker, I encounter an issue where the model value doesn't bind to the model when I select a date. Nothing happens when I select a date. Can anyone point out where I might be going wrong? Any help would be greatly appreci ...

eliminate the gap in the MUI Accordion when it is expanded

How can I prevent the Accordion MUI component from moving and applying top and bottom margins to summary elements in expanded mode? I added the following code to the summary element but it doesn't seem to be working. Any suggestions on how to fix this ...

Showcasing just the initial two lines of a flexbox using CSS and HTML

My current project requires me to develop a search bar that displays search results in a grid format. The items should be arranged next to each other in two rows. If there are additional results, a button labeled +32 should be present to show all results. ...

jQuery .click() only triggering upon page load

I've been searching all over the place and I just can't seem to find a solution to my specific situation. But here's what I'm dealing with: Instead of using inline HTML onclick, I'm trying to use jQuery click() like this $(docume ...

Issues with Angular directive causing form field validation to malfunction

Currently, I am working with the most recent version of AngularJS, specifically 1.2rc3. Bootstrap is being utilized for styling purposes and I have a directive set up as follows: angular.module('form.field', []) .directive('efield', f ...

Tips for retrieving the return value from a function with an error handling callback

I am having an issue with my function that is supposed to return data or throw an error from a JSON web token custom function. The problem I am facing is that the data returned from the signer part of the function is not being assigned to the token const a ...

Aborting HTTP POST requests in IE due to error code 0x2ee2

Currently, I am utilizing angularjs for file uploads to my API. The page features a multi-file select option where users can choose one or multiple files. Upon selection, the page initiates calls to the api and uploads each file individually using requests ...