简体   繁体   English

谷歌图表:线图+积分?

[英]Google Charts: Line graph + points?

I have got line graph that shows value change in time. 我有线图显示价值随时间的变化。 It works but I thought it would be great if I could add points that shows tooltip on hover. 它有效,但我认为如果我能在悬停时添加显示工具提示的点数会很棒。 Something like this: 像这样的东西: 在此输入图像描述 But I cannot use tooltip directly on one of those points. 我不能直接在其中一个点上使用工具提示。

var data = google.visualization.arrayToDataTable([
    ['time', 'value'],
    ['12:00',   1],
    ['13:00',   5],
    ['14:00',   8],
    ['15:00',   12],
    ['16:00',   11],
    ['17:00',   15],

]);

new google.visualization.LineChart(document.getElementById('visualization')).
  draw(data, {});

As stated in my comment you can use the annotation role to do this. 如我的评论中所述,您可以使用注释角色来执行此操作。

Your original code: 你原来的代码:

var data = google.visualization.arrayToDataTable([
    ['time', 'value'],
    ['12:00',   1],
    ['13:00',   5],
    ['14:00',   8],
    ['15:00',   12],
    ['16:00',   11],
    ['17:00',   15],

]);

new google.visualization.LineChart(document.getElementById('visualization')).
  draw(data, {});

You need to add two columns -- one for the annotation marker, one for the annotation text. 您需要添加两列 - 一列用于注释标记,一列用于注释文本。 Assuming you want two comments at 14:00 and 16:00 for the sake of example: 假设您希望在14:00和16:00发表两条评论,例如:

function drawVisualization() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'time');
  data.addColumn('number', 'value');
  data.addColumn({type: 'string', role:'annotation'});
  data.addColumn({type: 'string', role:'annotationText'});
  data.addRows([
    ['12:00',   1, null, null],
    ['13:00',   5, null, null],
    ['14:00',   8, 'A', 'This is Point A'],
    ['15:00',   12, null, null],
    ['16:00',   11, 'B', 'This is Point B'],
    ['17:00',   15, null, null],
  ]);

  new google.visualization.LineChart(document.getElementById('visualization')).
    draw(data, {});
}

This is the result: 这是结果:

样本注释

To add asgallant's solution for adding points to the chart, you can do as follows: 要添加asgallant的解决方案以向图表添加点,您可以执行以下操作:

function drawVisualization() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'time');
  data.addColumn('number', 'value');
  data.addColumn('number', 'points');
  data.addColumn({type: 'string', role:'annotation'});
  data.addColumn({type: 'string', role:'annotationText'});
  data.addRows([
    ['12:00',   1, null, null, null],
    ['13:00',   5, null, null, null],
    ['14:00',   8, 8, 'A', 'This is Point A'],
    ['15:00',   12, null, null, null],
    ['16:00',   11, 11, 'B', 'This is Point B'],
    ['17:00',   15, null, null, null],
  ]);

  new google.visualization.LineChart(document.getElementById('visualization')).
    draw(data, {
      series: {
        0: {
          // set any applicable options on the first series
        },
        1: {
          // set the options on the second series
          lineWidth: 0,
          pointSize: 5,
          visibleInLegend: false
        }
      }
    });
}

Here is the result of that: 结果如下:

asgallant样品

If I read your question correctly, you want points to appear on the line for each data point, and hovering over these points should spawn a tooltip. 如果我正确地阅读了您的问题,您希望每个数据点的线都出现点,并且将鼠标悬停在这些点上会产生工具提示。 If that is what you are after, the chart already does both of those things, you just can't see the points because by default they have a size of 0. Set the "pointSize" option in your LineChart to make the points larger: 如果这就是你所追求的,那么图表已经完成了这两件事,你只是看不到这些点,因为默认情况下它们的大小为0.在LineChart中设置“pointSize”选项以使点更大:

new google.visualization.LineChart(document.getElementById('visualization')).
    draw(data, {
        pointSize: 5
    });

Edit: 编辑:

To emphasize only some of the points in the chart, you need to add a new data series that contains only those values (you can either add this series directly to the DataTable, or create it on the fly with a DataView if you can somehow distinguish the points you want to emphasize from the others). 要仅强调图表中的某些点,您需要添加一个仅包含这些值的新数据系列(您可以将此系列直接添加到DataTable,或者如果您能以某种方式区分,则可以使用DataView动态创建它你想要从其他人强调的要点)。 Then you want to set the chart's series option to hide the line in the second series, remove the second series from the legend, and make its points larger (you can also set its color here if you want to match colors), like this: 然后你想设置图表的series选项来隐藏第二个系列中的线条,从图例中删除第二个系列,并使其点更大(如果你想匹配颜色,你也可以在这里设置它的颜色),如下所示:

series: {
    0: {
        // set any applicable options on the first series
    },
    1: {
        // set the options on the second series
        lineWidth: 0,
        pointSize: 5,
        visibleInLegend: false
    }
} 

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

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