Home Forums Report Bugs Line Graph Point Connection Bug

Line Graph Point Connection Bug

Viewing 15 posts - 1 through 15 (of 22 total)
  • #13869

    I’ve just tried posting this but I believe there was an error, so my apologies if this is posted twice

    I’m trying to use CanvasJS to display some data in real-time from a SQL DB.
    This data is added periodically every 10th of a second.

    I am achieving this by sending an AJAX request to the PHP server with the ID of the last record previously retrieved, which then replies with all the records currently adter this ID. Upon the JS (JavaScript) getting this, the data is then added to the graph in CanvasJS. The JS-side keeps track of 24 hours worth of data, so it will only ever contain a maximum of 24 hour-old data at once; however, I am encountering some unusual issues with the graph when the points are added. In the following screenshot(s) the data is shown in real-time, but only 1 minute’s worth of data is shown.

    Unusual behaviour
    As seen in the screenshot above, some of the points are connected to unrelated previous points. Focusing on the time stamp, if the page is refreshed (the past 24 hour’s worth of data is retrieved again), this behaviour is not replicated again, as seen below:
    Unusual behaviour gone

    My first assumption was that there was some mistake with the data in the database; however the subsequent screenshot shows that the data is in the correct order and hence should not be causing the problem:
    Data from database
    (Yes, I know some of the IDs are missing – this is due to multiple machines feeding data to ensure that the 10th-second-interval is met, even so, this should not cause an issue that I can think of).

    I have also implemented a “pause” button to the graph, which stops the interval of continuously requesting recent data until “played” again. This uses the same request system, retrieving all after the previous ID (as it would do in real-time). The below screenshot indicates the time period spent paused by the red box:
    Paused graph
    The problem of the connecting points does not seem to occur when the graph is “paused” (the interval is cleared and started again at a later date).

    This problem does not occur when the data is submitted at 1 second intervals, only at 0.1 second intervals. Despite this, I would like to pursue with the 0.1 second intervals for the increased level of accuracy.


    For further context to the situation, as I explained previously, only 24 hours of data is kept in memory. This is done by fetching the most recent 24 hours of data initially, then splicing by the length of the new data received each time from the start of the dictionary – essentially keeping a fixed length dictionary of the points.

    I have also implemented a method of changing the amount of data displayed on the graph through variables. If the limit were set to a minute, when updating the graph, the script would iterate through the stored 24 hours of data backwards until it find an item before the limit, at this point the data points for the graph will be set to a slice of the stored 24 hours of data from the found index to the last item.
    I wanted to explain this in case the points on the graph were being mixed up somehow as a result of the shifting of the data points for the limits.



    Can you kindly create jsfiddle with the issue and sample-data that helps in creating the issue, so that we can look into it and help you out?

    Vishwas R
    Team CanvasJS


    Hi @kieran , Have solved your problem?
    Because I´m having the same issue but in my case i´m doing a ECG Wave .
    If you have time, may you tell me an advice?
    Thanks in advice , best regards!



    Can you kindly create jsfiddle with the issue, so that we can look into it and help you out.

    Vishwas R


    Hi Vishwar R , I share you my jsfiddle
    but in this example I don´t have the problem. This only appear when I get data from mysql database.
    Best regards.



    It seems data received from database is not parsed properly (not in order/sorted according to x-value) before adding it to chart. Kindly check the data you receive from database.

    Vishwas R


    Hi @Vishwas R, I use this code to update my chart:
    function updateEcg() {
    $.getJSON(“/data_ecg.php”, function (result) {
    if (dataLength !== result.length) {
    for (var i = dataLength; i < result.length; i++) {
    x: parseInt(result[i].valuex),
    y: parseFloat(result[i].valuey)});
    if (data.length > 478) {
    data.shift(); }}
    dataLength = result.length;
    How you can see I parse my valueX to Int , and yes some of my X values are not in order, I attach you an image ( data order ) where you can see that the counter is not having order when it is inserting. Do you know how can I sort my X values to have a good chart?



    Sorting data before calling chart.render method in updateECG() will work fine in your case. Kindly refer this page for tutorial on array-sorting in JavaScript.

    Vishwas R


    @Vishwas R ,Thank you for replay question.
    I added the sort before the chart,render() , I leave you the new code :
    function updateEcg() {
    $.getJSON(“/data_ecg4.php”, function (result) {
    if (dataLength !== result.length) {
    for (var i = dataLength; i < result.length; i++) {
    x: parseInt(result[i].valuex),
    y: parseFloat(result[i].valuey),
    if (data.length > 478) {
    data.shift(); }}
    dataLength = result.length;
    chart.render();} });
    Is correct my idea? or I need to change something? , thanks in advice.



    data.sort((a,b)=>a-b); works on array but not on array of objects. You need to sort it manually to handle it. Please chek out the code-snippet to sort datapoints below.

    for (var i = 0; i < dataPoints.length - 1; i++) {
      if (dataPoints[i].x > dataPoints[i + 1].x) {
        var temp = dataPoints[i];
        dataPoints[i] = dataPoints[i + 1];
        dataPoints[i + 1] = temp;
        i = 0;

    Please check this JSFiddle for complete code.

    Also please refer to this documentation page for tutorial on dynamically updating chart from JSON data.
    Live Chart with Data from External JSON

    Vishwas R


    Hi @Vishwas R,
    Thank you for your help ,I leave you my code example jsFiddle I saw some examples that you gave me in the tutorial but this still working bad, I added in your code this part “setTimeout(updateEcg,updateInterval); ” .
    I share you a video when I show you how it´s working my code ECG chart.
    Any comment or suggestion in my code is perfect for me , thanks in advice.



    Can you kindly create jsfiddle that reproduces the issue you are facing, which you have showed in the video.

    The jsfiddle which you have provided seems to be working fine with JSON data and seems to be something different than the issue you have mentioned.

    Vishwas R


    Hi Again @Vishwas R,
    I leave you my code jsfiddle and some examples of my data_ecg4.php file that I use :

    Best regards!



    The dataPoints in your chart are scrambled as the dataPoints parsed from the JSON aren’t sorted. As mentioned in previous reply, sorting the datapoints should work fine in this case. Please take a look at this JSFiddle.
    Line Chart with Hidden Axis - Sparkline Chart

    Team CanvasJS

Viewing 15 posts - 1 through 15 (of 22 total)

You must be logged in to reply to this topic.