简体   繁体   English

在折线图上绘制矩形

[英]Drawing rectangles on a line graph

I have a straightforward line chart. 我有一个简单的折线图。

I'd like to let a user draw rectangles over the top of the graph that snap to the gridlines. 我想让用户在图的顶部绘制与网格线对齐的矩形。 See here for a visual example: 请参见此处的直观示例:

高图,上面有盒子

I've had a look at Highcharts and d3 but neither have a facility that allows the user to 'draw' on top of graphs. 我看过Highcharts和d3,但都没有一种工具可以让用户在图形上方进行“绘制”。

Before diving into the APIs and/or reinventing the wheel with lots of code, I was hoping someone has achieved this (or something similar) before, or could point me in the right direction? 在深入研究API和/或用大量代码重新发明轮子之前,我希望有人在之前已经做到这一点(或类似的东西),或者可以指出正确的方向吗?

Speaking from my experience with Highcharts, you can absolutely draw on top of graphs. 从我在Highcharts的经验来看,您绝对可以在图表之上绘制。 It's simply a matter of how you want to go about it. 这只是您要如何处理的问题。

I can think of two ways to pursue this goal: 我可以想到两种实现该目标的方法:

  1. Draw several "dummy" line series across the chart that will create the effect you're looking for. 在图表上绘制几个“虚拟”线系列,以创建您想要的效果。 The benefit of doing this is that the lines will stay fixed with the chart grid, will remain responsive along with the rest of the chart when the browser window or viewport is resized, and will export cleanly in various formats. 这样做的好处是,这些行将与图表网格保持固定,在调整浏览器窗口或视口的大小时,它们将与图表的其余部分保持响应,并以各种格式清晰地导出。 There are a few parameters you can use to keep the "dummy" series out of the legend ( showInLegend: false ) and prevent them from being interacted with by the user ( enableMouseTracking: false ). 您可以使用一些参数来使“虚拟”系列脱离图例( showInLegend: false ),并防止它们与用户进行交互( enableMouseTracking: false )。
  2. Use the renderer.rect method to draw rectangles in your chart (see http://api.highcharts.com/highcharts#Renderer.rect ). 使用renderer.rect方法在图表中绘制矩形(请参阅http://api.highcharts.com/highcharts#Renderer.rect )。 Here's a basic example taken from Highcharts' API documentation: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/renderer-rect-on-chart/ 这是摘自Highcharts API文档的基本示例: http : //jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/renderer-rect图表/

The API documentation is chock full of examples. API文档中包含许多示例。 I hope this has been helpful in your quest. 我希望这对您的追求有所帮助。

在此处输入图片说明

You can use the the columnRange series in combination with line. 您可以将columnRange系列与line结合使用。

plotOptions: {
  columnrange: {
            pointPadding: 0,
    groupPadding: 0,
    borderWidth: 1,
    borderColor: 'black'
  }
},

legend: {
  enabled: false
},

series: [{
  type: 'line',
  data: [
    [12, -20],
    [2, -10]
  ]
}, {
    color: 'rgba(0,0,0,0)',
  name: 'Temperatures',
  data: [
    [-9.7, 9.4],
    [-8.7, 6.5],
    [-3.5, 9.4],
    [-1.4, 19.9],
    [0.0, 22.6],
    [2.9, 29.5],
    [9.2, 30.7],
    [7.3, 26.5],
    [4.4, 18.0],
    [-3.1, 11.4],
    [-5.2, 10.4],
    [-13.5, 9.8]
  ]
}]

Example: 例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM