简体   繁体   English

添加笔划到谷歌图表

[英]adding stroke to google chart

I have created an google area chart and give a pointSize of 5 as my screenshot below. 我创建了一个谷歌区域图表,并给出了一个5的pointSize作为我的截图。 在此输入图像描述

Is there a way to add a stroke to my points like the image below. 有没有办法在我的点上添加笔划,如下图所示。 在此输入图像描述

Yes, but it's tricky. 是的,但这很棘手。

Executive Summary 执行摘要

Just create a script to find all the <circle> elements in the svg that's created inside the visualization div . 只需创建一个脚本来查找在可视化div创建的svg中的所有<circle>元素。

For all those circles, find the stroke property and set it to whatever color you want ( #000000 in your example), and set the stroke-width property to whatever linewidth you want (the default for Google Visualization is 2 for line charts). 对于所有这些圆圈,找到stroke属性并将其设置为您想要的任何颜色(示例中为#000000 ),并将stroke-width属性设置为您想要的任何线宽(对于折线图,Google Visualization的默认值为2 )。

Disable Chart Interactivity so it stays that way. 禁用图表交互以使其保持这种状态。

Detailed Explanation 详细说明

Let's start off with some simple code: 让我们从一些简单的代码开始:

function drawVisualization() {
  // Some raw data (not necessarily accurate)
  var data = google.visualization.arrayToDataTable([
    ['Series ID', 'Value'],
    ['A', 165],
    ['B', 135],
    ['C', 157],
    ['D', 139],
    ['E', 136]
  ]);

  // Create and draw the visualization.
  var ac = new google.visualization.AreaChart(document.getElementById('visualization'));
  ac.draw(data, {
    isStacked: true,
    width: 600,
    height: 400,
    pointSize: 10
  });
}

This looks like this: 这看起来像这样:

区域样本

Google Visualizations renders this object with the following generic code structure inside the visualization div element: Google Visualizations在可视化div元素中使用以下通用代码结构呈现此对象:

<div id="visualization" style="width: 600px; height: 400px; position: relative;">
  <div style="position: relative; width: 600px; height: 400px;" dir="ltr">
    <div style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%;">
      <svg width="600" height="400" style="overflow: hidden;">
        <defs id="defs">
          <clipPath id="_ABSTRACT_RENDERER_ID_0">
            <rect x="115" y="77" width="371" height="247">
          </clipPath>
        </defs>
        <rect x="0" y="0" width="600" height="400" stroke="none" stroke-width="0" fill="#ffffff">
        <g>
          <rect x="499" y="77" width="88" height="13" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
          <g>
            <rect x="499" y="77" width="88" height="13" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
            <g>
              <text text-anchor="start" x="517" y="88.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">Value</text>
            </g>
            <rect x="499" y="77" width="13" height="13" stroke="none" stroke-width="0" fill="#3366cc">
          </g>
        </g>
        <g>
          <rect x="115" y="77" width="371" height="247" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
          <g clip-path="url(http://savedbythegoog.appspot.com/retrieve_cache?unique_id=b65582b5691907b9b17033f29fdb4c4bfc18e583#_ABSTRACT_RENDERER_ID_0)">
            <g>
              <rect x="115" y="323" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
              <rect x="115" y="262" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
              <rect x="115" y="200" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
              <rect x="115" y="139" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
              <rect x="115" y="77" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
            </g>
            <g>
              <g>
                <path d="M115.5,323.5L115.5,120.55000000000001L208,157.45L300.5,130.39000000000001L393,152.53L485.5,156.22L485.5,323.5L393,323.5L300.5,323.5L208,323.5L115.5,323.5" stroke="none" stroke-width="0" fill-opacity="0.3" fill="#3366cc">
              </g>
            </g>
            <g>
              <rect x="115" y="323" width="371" height="1" stroke="none" stroke-width="0" fill="#333333">
            </g>
            <g>
              <path d="M115.5,120.55000000000001L208,157.45L300.5,130.39000000000001L393,152.53L485.5,156.22" stroke="#3366cc" stroke-width="2" fill-opacity="1" fill="none">
            </g>
          </g>
          <g>
            <circle cx="115.5" cy="120.55000000000001" r="6" stroke="none" stroke-width="0" fill="#3366cc">
            <circle cx="208" cy="157.45" r="6" stroke="none" stroke-width="0" fill="#3366cc">
            <circle cx="300.5" cy="130.39000000000001" r="6" stroke="none" stroke-width="0" fill="#3366cc">
            <circle cx="393" cy="152.53" r="6" stroke="none" stroke-width="0" fill="#3366cc">
            <circle cx="485.5" cy="156.22" r="6" stroke="none" stroke-width="0" fill="#3366cc">
          </g>
          <g>
            <g>
              <text text-anchor="middle" x="115.5" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">A</text>
            </g>
            <g>
              <text text-anchor="middle" x="208" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">B</text>
            </g>
            <g>
              <text text-anchor="middle" x="300.5" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">C</text>
            </g>
            <g>
              <text text-anchor="middle" x="393" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">D</text>
            </g>
            <g>
              <text text-anchor="middle" x="485.5" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">E</text>
            </g>
            <g>
              <text text-anchor="end" x="102" y="328.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">0</text>
            </g>
            <g>
              <text text-anchor="end" x="102" y="266.55" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">50</text>
            </g>
            <g>
              <text text-anchor="end" x="102" y="205.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">100</text>
            </g>
            <g>
              <text text-anchor="end" x="102" y="143.55" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">150</text>
            </g>
            <g>
              <text text-anchor="end" x="102" y="82.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">200</text>
            </g>
          </g>
        </g>
        <g>
      </svg>
    </div>
  </div>
  <div style="display: none; position: absolute; top: 410px; left: 610px; white-space: nowrap; font-family: Arial; font-size: 13px;">Value</div>
  <div></div>
</div>

That's a lot of stuff, but in general, the way it works is: 这是很多东西,但总的来说,它的工作方式是:

  1. Container div ('visualization' in this case) 容器div (在这种情况下是'可视化')
  2. 2 Internal div 2内部div
  3. svg element svg元素

The svg element itself generally follows this pattern: svg元素本身通常遵循以下模式:

  1. Clipping path for the entire svg 整个svg剪切路径
  2. Group #1: Title 第一组:标题
  3. Group #2: Legend 第2组:传奇
  4. Group #3: Chart Area 第3组:图表区域
  5. Group #4: Axis Labels 第4组:轴标签

There are slightly different variations depending on your chart (if you don't include a label, it may not generate the 'Title' group, and in general if you change the library, the way it renders charts will be slightly different. However, for the 'corecharts' package, generally everything will follow the above). 根据您的图表有不同的变化(如果您不包含标签,它可能不会生成'标题'组,并且通常如果您更改库,它呈现图表的方式将略有不同。但是,对于'corecharts'包,通常一切都将遵循上述内容)。

Since you want to fiddle with the actual chart area portion (the circles in the graph), let's look in to that element a bit more closely. 由于你想要摆弄实际的图表区域部分(图中的圆圈),让我们更仔细地看一下这个元素。 Here is the svg for that group. 这是该组的svg

<g>
  <rect x="115" y="77" width="371" height="247" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
  <g clip-path="url(http://savedbythegoog.appspot.com/retrieve_cache?unique_id=5c972f534cccaf429927a9b83381537e632ce3f8#_ABSTRACT_RENDERER_ID_0)">
    <g>
      <rect x="115" y="323" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
      <rect x="115" y="262" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
      <rect x="115" y="200" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
      <rect x="115" y="139" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
      <rect x="115" y="77" width="371" height="1" stroke="none" stroke-width="0" fill="#cccccc">
    </g>
    <g>
      <g>
        <path d="M115.5,323.5L115.5,120.55000000000001L208,157.45L300.5,130.39000000000001L393,152.53L485.5,156.22L485.5,323.5L393,323.5L300.5,323.5L208,323.5L115.5,323.5" stroke="none" stroke-width="0" fill-opacity="0.3" fill="#3366cc">
      </g>
    </g>
    <g>
      <rect x="115" y="323" width="371" height="1" stroke="none" stroke-width="0" fill="#333333">
    </g>
    <g>
      <path d="M115.5,120.55000000000001L208,157.45L300.5,130.39000000000001L393,152.53L485.5,156.22" stroke="#3366cc" stroke-width="2" fill-opacity="1" fill="none">
    </g>
  </g>
  <g>
    <circle cx="115.5" cy="120.55000000000001" r="6" stroke="none" stroke-width="0" fill="#3366cc">
    <circle cx="208" cy="157.45" r="6" stroke="none" stroke-width="0" fill="#3366cc">
    <circle cx="300.5" cy="130.39000000000001" r="6" stroke="none" stroke-width="0" fill="#3366cc">
    <circle cx="393" cy="152.53" r="6" stroke="none" stroke-width="0" fill="#3366cc">
    <circle cx="485.5" cy="156.22" r="6" stroke="none" stroke-width="0" fill="#3366cc">
  </g>
  <g>
    <g>
      <text text-anchor="middle" x="115.5" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">A</text>
    </g>
    <g>
      <text text-anchor="middle" x="208" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">B</text>
    </g>
    <g>
      <text text-anchor="middle" x="300.5" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">C</text>
    </g>
    <g>
      <text text-anchor="middle" x="393" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">D</text>
    </g>
    <g>
      <text text-anchor="middle" x="485.5" y="343.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#222222">E</text>
    </g>
    <g>
      <text text-anchor="end" x="102" y="328.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">0</text>
    </g>
    <g>
      <text text-anchor="end" x="102" y="266.55" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">50</text>
    </g>
    <g>
      <text text-anchor="end" x="102" y="205.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">100</text>
    </g>
    <g>
      <text text-anchor="end" x="102" y="143.55" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">150</text>
    </g>
    <g>
      <text text-anchor="end" x="102" y="82.05" font-family="Arial" font-size="13" stroke="none" stroke-width="0" fill="#444444">200</text>
    </g>
  </g>
</g>

This one shows: 这个显示:

  1. Rectangle (the chart area) 矩形(图表区域)
  2. Group A: Grid Lines A组:网格线
  3. Group B: Area Chart (the fill) B组:面积图(填充)
  4. Group C: Baseline (X Axis) C组:基线(X轴)
  5. Group D: Line Chart (the line) D组:折线图(线)
  6. Group E: Circles (the points) E组:圈子(分数)
  7. Group F: Sub-groups with axis labels F组:带有轴标签的子组

So we want to focus on Group E there. 所以我们希望专注于那里的E组。 Here is the code for the circles: 这是圈子的代码:

<g>
<circle cx="115.5" cy="120.55000000000001" r="6" stroke="none" stroke-width="0" fill="#3366cc">
<circle cx="208" cy="157.45" r="6" stroke="none" stroke-width="0" fill="#3366cc">
<circle cx="300.5" cy="130.39000000000001" r="6" stroke="none" stroke-width="0" fill="#3366cc">
<circle cx="393" cy="152.53" r="6" stroke="none" stroke-width="0" fill="#3366cc">
<circle cx="485.5" cy="156.22" r="6" stroke="none" stroke-width="0" fill="#3366cc">
</g>

The easiest way is just to change the "stroke" element. 最简单的方法就是改变“笔画”元素。

For instance, if I manually change the stroke-width to 2 (the same as the line-width), and the color to black (as in your example), I get this: 例如,如果我手动将笔触宽度更改为2(与线宽相同),并将颜色更改为黑色(如示例中所示),我会得到:

<g>
<circle cx="115.5" cy="120.55000000000001" r="6" stroke="#000000" stroke-width="2" fill="#3366cc">
<circle cx="208" cy="157.45" r="6" stroke="#000000" stroke-width="2" fill="#3366cc">
<circle cx="300.5" cy="130.39000000000001" r="6" stroke="#000000" stroke-width="2" fill="#3366cc">
<circle cx="393" cy="152.53" r="6" stroke="#000000" stroke-width="2" fill="#3366cc">
<circle cx="485.5" cy="156.22" r="6" stroke="#000000" stroke-width="2" fill="#3366cc">
</g>

Here is what it looks like: 这是它的样子:

修订区域

But there are a few issues with this method. 但是这种方法存在一些问题。 Any time you mouseover the chart, it redraws the SVG element (because it highlights any circle that is clicked, or all circles if the legend is clicked), and becomes something like this: 每当您将鼠标悬停在图表上时,它都会重新绘制SVG元素(因为它会突出显示所单击的任何圆圈,或者如果单击图例则突出显示所有圆圈),并且变为如下所示:

<g>
<circle cx="393" cy="152.53" r="8" stroke="#3366cc" stroke-width="1" fill="#ffffff">
<circle cx="393" cy="152.53" r="6" stroke="none" stroke-width="0" fill="#3366cc">
</g>

So it just redraws an even bigger circle around it, but this means your fancy formatting is all gone! 所以它只是在它周围重新绘制一个更大的圆圈,但这意味着你的花式格式化全部消失了!

Basically, you would need to either disable interactivity, or create a script that can run even when there is that additional circle (because you don't want both circles to end up with black rings around them, as it will be super big and ugly). 基本上,你需要禁用交互性,或者创建一个即使有另外一个圆圈也可以运行的脚本(因为你不希望两个圆圈最终都有黑色环绕它们,因为它会超级大而且丑陋)。

So what I'd suggest is look for circles, with a radius of X (where X is whatever the majority of circles are on the page, with a tie going to the smaller number), and store all those elements in an array. 所以我建议寻找半径为X的圆(其中X是页面上大多数圆圈的任何一个,并且带有一个小的数字),并将所有这些元素存储在一个数组中。 Then just go through the array, changing the stroke-width and stroke-color properties whenever the chart is interacted with at all. 然后只需浏览数组,在图表完全交互时更改stroke-widthstroke-color属性。

Realize that this will become far more complicated the more complex your chart gets, as you are doing this on the fly for all circles. 意识到这将变得更加复杂,你的图表变得越复杂,因为你正在为所有圈子动态做这件事。 If there are any other circles in the chart they will get the same treatment, or screw up the math. 如果图表中有任何其他圆圈,他们将得到相同的处理,或搞砸数学。 Which would suck. 哪个会很糟糕。

Here is a simple solution. 这是一个简单的解决方案。 The style of each point is defined programmatically. 每个点的样式都是以编程方式定义的。 This is done by adding a column to the datatable having the role 'style', then adding that style to all points using a simple loop. 这是通过向具有角色“style”的数据表添加列,然后使用简单循环将该样式添加到所有点来完成的。 Try on JSFiddle: https://jsfiddle.net/m45z7mmp/ 试试JSFiddle: https ://jsfiddle.net/m45z7mmp/

  google.setOnLoadCallback(drawChart);

  function drawChart() {

    var dataArray = [
      ['X', 'Y', {
        'type': 'string',
        'role': 'style'
      }],
      [1, 3],
      [2, 2.5],
      [3, 3],
      [4, 4],
    ];

    for (i = 1; i < dataArray.length; i++) {
      dataArray[i].push('point {stroke-width: 2;  stroke-color: #e97b2c; fill-color: #eeeeee }');
    }

    var data = google.visualization.arrayToDataTable(dataArray);

    var options = {
      colors: ['#e97b2c'],
      pointSize: 8,
    };

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

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

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