Highcharts redraw and sort data on draggable line graph












0















I have a line chart in Highcharts and want to redraw or the line when dragging a point past another point.



I want to redraw and sort the chart while dragging so the line updates and don't cross eachother. We can update the chart with chart.series[0].setData() or chart.series[0].update(); but i keep getting sorting errors or errors with the draggable point being null (dragPoint or hoverPoint) when updating the chart



Here is an example where I detect when a point is being dragged past another by changing the getNewPos(e) function in the draggable module
http://jsfiddle.net/u4xpmf2j/1/



 */
function getNewPos(e) {
toClose = false;
var originalEvent = e.originalEvent || e,
pageX = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageX : e.pageX,
pageY = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageY : e.pageY,
series = dragPoint.series,
draggableX = series.options.draggableX && dragPoint.draggableX !== false,
draggableY = series.options.draggableY && dragPoint.draggableY !== false,
dragSensitivity = pick(series.options.dragSensitiviy, 1),
dragMaxToPoint = pick(series.options.dragMaxToPoint, 0.3),
deltaX = draggableX ? dragX - pageX : 0,
deltaY = draggableY ? dragY - pageY : 0,
newPlotX = dragPlotX - deltaX,
newPlotY = dragPlotY - deltaY,
newX = dragX === undefined ? dragPoint.x : series.xAxis.toValue(newPlotX, true),
newY = dragY === undefined ? dragPoint.y : series.yAxis.toValue(newPlotY, true),
ret;
newX = filterRange(newX, series, 'X');
newY = filterRange(newY, series, 'Y');
if (dragPoint.index > 0 && (newX - dragPoint.series.data[dragPoint.index - 1].x) < dragMaxToPoint || dragPoint.index < dragPoint.series.data.length - 1 && (newX - dragPoint.series.data[dragPoint.index + 1].x) > -dragMaxToPoint) {
toClose = true; // DETECT POINT BEING DRAGGED PAST ANOTHER
}
if (Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)) > dragSensitivity) {
return {
x: draggableX ? newX : dragPoint.x,
y: draggableY ? newY : dragPoint.y
};
} else {
return null;
}
}


Any ideas?










share|improve this question



























    0















    I have a line chart in Highcharts and want to redraw or the line when dragging a point past another point.



    I want to redraw and sort the chart while dragging so the line updates and don't cross eachother. We can update the chart with chart.series[0].setData() or chart.series[0].update(); but i keep getting sorting errors or errors with the draggable point being null (dragPoint or hoverPoint) when updating the chart



    Here is an example where I detect when a point is being dragged past another by changing the getNewPos(e) function in the draggable module
    http://jsfiddle.net/u4xpmf2j/1/



     */
    function getNewPos(e) {
    toClose = false;
    var originalEvent = e.originalEvent || e,
    pageX = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageX : e.pageX,
    pageY = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageY : e.pageY,
    series = dragPoint.series,
    draggableX = series.options.draggableX && dragPoint.draggableX !== false,
    draggableY = series.options.draggableY && dragPoint.draggableY !== false,
    dragSensitivity = pick(series.options.dragSensitiviy, 1),
    dragMaxToPoint = pick(series.options.dragMaxToPoint, 0.3),
    deltaX = draggableX ? dragX - pageX : 0,
    deltaY = draggableY ? dragY - pageY : 0,
    newPlotX = dragPlotX - deltaX,
    newPlotY = dragPlotY - deltaY,
    newX = dragX === undefined ? dragPoint.x : series.xAxis.toValue(newPlotX, true),
    newY = dragY === undefined ? dragPoint.y : series.yAxis.toValue(newPlotY, true),
    ret;
    newX = filterRange(newX, series, 'X');
    newY = filterRange(newY, series, 'Y');
    if (dragPoint.index > 0 && (newX - dragPoint.series.data[dragPoint.index - 1].x) < dragMaxToPoint || dragPoint.index < dragPoint.series.data.length - 1 && (newX - dragPoint.series.data[dragPoint.index + 1].x) > -dragMaxToPoint) {
    toClose = true; // DETECT POINT BEING DRAGGED PAST ANOTHER
    }
    if (Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)) > dragSensitivity) {
    return {
    x: draggableX ? newX : dragPoint.x,
    y: draggableY ? newY : dragPoint.y
    };
    } else {
    return null;
    }
    }


    Any ideas?










    share|improve this question

























      0












      0








      0








      I have a line chart in Highcharts and want to redraw or the line when dragging a point past another point.



      I want to redraw and sort the chart while dragging so the line updates and don't cross eachother. We can update the chart with chart.series[0].setData() or chart.series[0].update(); but i keep getting sorting errors or errors with the draggable point being null (dragPoint or hoverPoint) when updating the chart



      Here is an example where I detect when a point is being dragged past another by changing the getNewPos(e) function in the draggable module
      http://jsfiddle.net/u4xpmf2j/1/



       */
      function getNewPos(e) {
      toClose = false;
      var originalEvent = e.originalEvent || e,
      pageX = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageX : e.pageX,
      pageY = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageY : e.pageY,
      series = dragPoint.series,
      draggableX = series.options.draggableX && dragPoint.draggableX !== false,
      draggableY = series.options.draggableY && dragPoint.draggableY !== false,
      dragSensitivity = pick(series.options.dragSensitiviy, 1),
      dragMaxToPoint = pick(series.options.dragMaxToPoint, 0.3),
      deltaX = draggableX ? dragX - pageX : 0,
      deltaY = draggableY ? dragY - pageY : 0,
      newPlotX = dragPlotX - deltaX,
      newPlotY = dragPlotY - deltaY,
      newX = dragX === undefined ? dragPoint.x : series.xAxis.toValue(newPlotX, true),
      newY = dragY === undefined ? dragPoint.y : series.yAxis.toValue(newPlotY, true),
      ret;
      newX = filterRange(newX, series, 'X');
      newY = filterRange(newY, series, 'Y');
      if (dragPoint.index > 0 && (newX - dragPoint.series.data[dragPoint.index - 1].x) < dragMaxToPoint || dragPoint.index < dragPoint.series.data.length - 1 && (newX - dragPoint.series.data[dragPoint.index + 1].x) > -dragMaxToPoint) {
      toClose = true; // DETECT POINT BEING DRAGGED PAST ANOTHER
      }
      if (Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)) > dragSensitivity) {
      return {
      x: draggableX ? newX : dragPoint.x,
      y: draggableY ? newY : dragPoint.y
      };
      } else {
      return null;
      }
      }


      Any ideas?










      share|improve this question














      I have a line chart in Highcharts and want to redraw or the line when dragging a point past another point.



      I want to redraw and sort the chart while dragging so the line updates and don't cross eachother. We can update the chart with chart.series[0].setData() or chart.series[0].update(); but i keep getting sorting errors or errors with the draggable point being null (dragPoint or hoverPoint) when updating the chart



      Here is an example where I detect when a point is being dragged past another by changing the getNewPos(e) function in the draggable module
      http://jsfiddle.net/u4xpmf2j/1/



       */
      function getNewPos(e) {
      toClose = false;
      var originalEvent = e.originalEvent || e,
      pageX = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageX : e.pageX,
      pageY = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageY : e.pageY,
      series = dragPoint.series,
      draggableX = series.options.draggableX && dragPoint.draggableX !== false,
      draggableY = series.options.draggableY && dragPoint.draggableY !== false,
      dragSensitivity = pick(series.options.dragSensitiviy, 1),
      dragMaxToPoint = pick(series.options.dragMaxToPoint, 0.3),
      deltaX = draggableX ? dragX - pageX : 0,
      deltaY = draggableY ? dragY - pageY : 0,
      newPlotX = dragPlotX - deltaX,
      newPlotY = dragPlotY - deltaY,
      newX = dragX === undefined ? dragPoint.x : series.xAxis.toValue(newPlotX, true),
      newY = dragY === undefined ? dragPoint.y : series.yAxis.toValue(newPlotY, true),
      ret;
      newX = filterRange(newX, series, 'X');
      newY = filterRange(newY, series, 'Y');
      if (dragPoint.index > 0 && (newX - dragPoint.series.data[dragPoint.index - 1].x) < dragMaxToPoint || dragPoint.index < dragPoint.series.data.length - 1 && (newX - dragPoint.series.data[dragPoint.index + 1].x) > -dragMaxToPoint) {
      toClose = true; // DETECT POINT BEING DRAGGED PAST ANOTHER
      }
      if (Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)) > dragSensitivity) {
      return {
      x: draggableX ? newX : dragPoint.x,
      y: draggableY ? newY : dragPoint.y
      };
      } else {
      return null;
      }
      }


      Any ideas?







      javascript highcharts






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 11:42









      KoiskiKoiski

      3182620




      3182620
























          1 Answer
          1






          active

          oldest

          votes


















          1














          How about sorting data after drop event and then use series.setData to update the chart:



            plotOptions: {
          series: {
          point: {
          events: {
          drop: function() {
          var series = this.series,
          xData = series.xData.slice(),
          yData = series.yData.slice(),
          newData = ;

          xData.forEach(function(data, i) {
          newData.push([
          data,
          yData[i]
          ]);
          });

          newData.sort(function(a, b) {
          return a[0] - b[0];
          });

          series.setData(newData, false, false);
          series.chart.redraw();
          }
          }
          },
          stickyTracking: false
          },
          }


          Demo:
          http://jsfiddle.net/BlackLabel/6kyt3roL/






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53518657%2fhighcharts-redraw-and-sort-data-on-draggable-line-graph%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            How about sorting data after drop event and then use series.setData to update the chart:



              plotOptions: {
            series: {
            point: {
            events: {
            drop: function() {
            var series = this.series,
            xData = series.xData.slice(),
            yData = series.yData.slice(),
            newData = ;

            xData.forEach(function(data, i) {
            newData.push([
            data,
            yData[i]
            ]);
            });

            newData.sort(function(a, b) {
            return a[0] - b[0];
            });

            series.setData(newData, false, false);
            series.chart.redraw();
            }
            }
            },
            stickyTracking: false
            },
            }


            Demo:
            http://jsfiddle.net/BlackLabel/6kyt3roL/






            share|improve this answer




























              1














              How about sorting data after drop event and then use series.setData to update the chart:



                plotOptions: {
              series: {
              point: {
              events: {
              drop: function() {
              var series = this.series,
              xData = series.xData.slice(),
              yData = series.yData.slice(),
              newData = ;

              xData.forEach(function(data, i) {
              newData.push([
              data,
              yData[i]
              ]);
              });

              newData.sort(function(a, b) {
              return a[0] - b[0];
              });

              series.setData(newData, false, false);
              series.chart.redraw();
              }
              }
              },
              stickyTracking: false
              },
              }


              Demo:
              http://jsfiddle.net/BlackLabel/6kyt3roL/






              share|improve this answer


























                1












                1








                1







                How about sorting data after drop event and then use series.setData to update the chart:



                  plotOptions: {
                series: {
                point: {
                events: {
                drop: function() {
                var series = this.series,
                xData = series.xData.slice(),
                yData = series.yData.slice(),
                newData = ;

                xData.forEach(function(data, i) {
                newData.push([
                data,
                yData[i]
                ]);
                });

                newData.sort(function(a, b) {
                return a[0] - b[0];
                });

                series.setData(newData, false, false);
                series.chart.redraw();
                }
                }
                },
                stickyTracking: false
                },
                }


                Demo:
                http://jsfiddle.net/BlackLabel/6kyt3roL/






                share|improve this answer













                How about sorting data after drop event and then use series.setData to update the chart:



                  plotOptions: {
                series: {
                point: {
                events: {
                drop: function() {
                var series = this.series,
                xData = series.xData.slice(),
                yData = series.yData.slice(),
                newData = ;

                xData.forEach(function(data, i) {
                newData.push([
                data,
                yData[i]
                ]);
                });

                newData.sort(function(a, b) {
                return a[0] - b[0];
                });

                series.setData(newData, false, false);
                series.chart.redraw();
                }
                }
                },
                stickyTracking: false
                },
                }


                Demo:
                http://jsfiddle.net/BlackLabel/6kyt3roL/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 '18 at 15:52









                Wojciech ChmielWojciech Chmiel

                2,3461212




                2,3461212
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53518657%2fhighcharts-redraw-and-sort-data-on-draggable-line-graph%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Lallio

                    Futebolista

                    Jornalista