通过缩放以编程方式在Highcharts中绘制rect和line [英] Programmatically draw rect and line in Highcharts with zoom

查看:133
本文介绍了通过缩放以编程方式在Highcharts中绘制rect和line的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Highcharts.Renderer 在Highcharts中进行一些程序绘制,并使用path()rect().在下面的代码中,我手动绘制了直线和矩形的坐标.实际上,它们与主要数据系列(带有值的日期)有关.

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).

如何以编程方式绘制某些内容并使缩放正常工作?

具有缩放功能的主图:

    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]
    }]

程序绘图:

 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/

推荐答案

现在,您并没有真正使用图表值-绘制的rect和路径与系列无关.您可以使用点y和x值以及Axis.toPixels()方法将图形与图表连接: http://api.highcharts.com/highcharts/Axis.toPixels

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]
    }]
  });
});

在这里,您将看到一个示例其工作方式的示例: http://jsfiddle.net/n8ro1b9m/4/

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

这篇关于通过缩放以编程方式在Highcharts中绘制rect和line的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆