Skip to content Skip to sidebar Skip to footer

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

Solution 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/


Solution 2:

You can use the series.data values in your function by using:

chart.series[0].data[i].y

With i the number of the point in the series. Is that what you want to do ?


Post a Comment for "Programmatically Draw Rect And Line In Highcharts With Zoom"