JavaScript Programmatically draw rect and line in Highcharts with zoom

I'm doing some programmatic drawing in Highcharts using Highcharts.Renderer using path() and rect(). In the code below I have manually plotted the coordinates for the line and rect. In reality the are related to the main data series (dates with values).

How can I programmatically draw something and make the zoom work?

The main graph, with zoom:

    chart: {
            zoomType: 'x',
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]

The programmtic drawing:

 chart.renderer.rect(100, 110, 100, 100, 5)
        .attr({
            'stroke-width': 2,
            stroke: 'red',
            fill: 'transparent',
            zIndex: 3
        })
        .add();
  var path = [
    'M', 100, 100,
    'L', 130, 110,
    'L', 160, 105,
    'L', 190, 150,
    ];
  chart.renderer.path(path)
        .attr({
        'stroke-width': 2,
        stroke: 'blue',
        zIndex: 4
      })
      .add();

http://jsfiddle.net/n8ro1b9m/1/

Answer:1

Right now you are not really using your chart values - you are drawing your rect and path independently from your series. You can connect your drawings with your chart using your point y and x values and Axis.toPixels() method: http://api.highcharts.com/highcharts/Axis.toPixels

$(function() {
  var addRect = function(chart) {
    $('.rect').remove();
    var xAxis = chart.xAxis[0],
      yAxis = chart.yAxis[0]
    chart.renderer.rect(xAxis.toPixels(1), 110, xAxis.toPixels(2) - xAxis.toPixels(1), 100, 5)
      .attr({
        'stroke-width': 2,
        stroke: 'red',
        fill: 'transparent',
        zIndex: 0
      }).addClass('rect')
      .add();
    chart.renderer.rect(0, 0, chart.plotLeft, chart.chartHeight + chart.plotTop, 5)
      .attr({
        fill: 'white',
        zIndex: 0
      }).addClass('rect')
      .add();
  };
  $('#container').highcharts({
    chart: {
      zoomType: 'x',
      events: {
        redraw: function() {
          addRect(this);
        },
        load: function() {
          addRect(this);
        }
      }
    },
    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    series: [{
      data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
  });
});

Here you can see an example how it can work: http://jsfiddle.net/n8ro1b9m/4/

Answer:2

I am working through NodeSchool's Functional Javascript course. The "Every Some" exercise provides a "goodUsers" array of objects argument to compare another list of users to (also an array of objects)...

I am working through NodeSchool's Functional Javascript course. The "Every Some" exercise provides a "goodUsers" array of objects argument to compare another list of users to (also an array of objects)...

I have a web server which uses SSL client authentication. A web page on that server makes a same-origin GET request using the fetch() API. In Chrome, the client cert is sent in the SSL handshake, as ...

I have a web server which uses SSL client authentication. A web page on that server makes a same-origin GET request using the fetch() API. In Chrome, the client cert is sent in the SSL handshake, as ...

I want to recreate the video animation in the background of the [Uber Developer webpage](link) programatically using JavaScript/CSS. I am defining a grid of paths (my own design, not the one on the ...

I want to recreate the video animation in the background of the [Uber Developer webpage](link) programatically using JavaScript/CSS. I am defining a grid of paths (my own design, not the one on the ...

I can't seem to get jquery to toggle multiple checkboxes. I want to grab all the checkboxes on the page with a certain name and toggle them. Here is what I am currently trying but it just generates ...

I can't seem to get jquery to toggle multiple checkboxes. I want to grab all the checkboxes on the page with a certain name and toggle them. Here is what I am currently trying but it just generates ...

  1. toggle multiple checkboxes jquery
  2. toggle multiple checkboxes
  3. javascript toggle multiple checkboxes