自定义点

概览

在许多 Google 图表中,数据值都显示在精确的点。折线图就是由线条连接的这些点的集合,而散点图只不过是多个点。

在除散点图之外的所有图表中,这些点的大小默认为零。您可以使用 pointSize 选项控制其大小,使用 pointShape 选项控制其形状。

在上方,您可以看到一个包含 6 个系列的图表,每个系列的 pointSize 为 30,但 pointShape 各不相同。

        var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 7 },
         
pointSize: 30,
          series
: {
               
0: { pointShape: 'circle' },
               
1: { pointShape: 'triangle' },
               
2: { pointShape: 'square' },
               
3: { pointShape: 'diamond' },
               
4: { pointShape: 'star' },
               
5: { pointShape: 'polygon' }
           
}

       
};
  <head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
      google
.charts.load("current", {packages:["corechart"]});
      google
.charts.setOnLoadCallback(drawChart);
     
function drawChart() {
       
var data = google.visualization.arrayToDataTable
           
([['X', '1', '2', '3', '4', '5', '6'],
             
[1, 2, null, null, null, null, null],
             
[2, null, 3, null, null, null, null],
             
[3, null, null, 4, null, null, null],
             
[4, null, null, null, 5, null, null],
             
[5, null, null, null, null, 6, null],
             
[6, null, null, null, null, null, 7]
       
]);

       
var options = {
          legend
: 'none',
         
pointSize: 30,
          series
: {
               
0: { pointShape: 'circle' },
               
1: { pointShape: 'triangle' },
               
2: { pointShape: 'square' },
               
3: { pointShape: 'diamond' },
               
4: { pointShape: 'star' },
               
5: { pointShape: 'polygon' }
           
}

       
};

       
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart
.draw(data, options);
     
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div" style="width: 900px; height: 500px;"></div>
 
</body>
</html>

一些简单示例

与上一部分中的图表不同,大多数图表只有一个系列。下面是一个包含 20 点圆形点的折线图表示例:

由于默认的 pointShape 是圆形,因此我们可以在选项中不添加它:

        var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 9 },
          curveType
: 'function',
         
pointSize: 20,
     
};
  <head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
      google
.charts.load("current", {packages:["corechart"]});
      google
.charts.setOnLoadCallback(drawChart);
     
function drawChart() {
       
var data = google.visualization.arrayToDataTable
           
([['X', 'Y'],
             
[1, 3],
             
[2, 2.5],
             
[3, 3],
             
[4, 4],
             
[5, 4],
             
[6, 3],
             
[7, 2.5],
             
[8, 3]
       
]);

       
var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 9 },
          curveType
: 'function',
         
pointSize: 20,
       
};

       
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart
.draw(data, options);
   
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div" style="width: 900px; height: 500px;"></div>
 
</body>
</html>

您可以将 pointShape 设置为“三角形”“方形”“菱形”“星形”或“多边形”,从而将其从“圆形”更改为其他形状:

        var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 9 },
          colors
: ['#795548'],
          pointSize
: 20,
         
pointShape: 'square'
       
};
  <head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
      google
.charts.load("current", {packages:["corechart"]});
      google
.charts.setOnLoadCallback(drawChart);
     
function drawChart() {
       
var data = google.visualization.arrayToDataTable
           
([['X', 'Y'],
             
[1, 3],
             
[2, 2.5],
             
[3, 3],
             
[4, 4],
             
[5, 4],
             
[6, 3],
             
[7, 2.5],
             
[8, 3]
       
]);

       
var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 9 },
          colors
: ['#795548'],
          pointSize
: 20,
         
pointShape: 'square'
       
};

       
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart
.draw(data, options);
   
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div" style="width: 900px; height: 500px;"></div>
 
</body>
</html>

“星形”和“多边形”形状可让您自定义边数,均默认设为 5。下面是一些四面星形:

        var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 9 },
          colors
: ['#EF851C'],
          pointSize
: 30,
         
pointShape: { type: 'star', sides: 4 }
       
};
<html>
 
<head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
      google
.charts.load("current", {packages:["corechart"]});
      google
.charts.setOnLoadCallback(drawChart);
     
function drawChart() {
       
var data = google.visualization.arrayToDataTable
           
([['X', 'Y'],
             
[1, 3],
             
[2, 2.5],
             
[3, 3],
             
[4, 4],
             
[5, 4],
             
[6, 3],
             
[7, 2.5],
             
[8, 3]
       
]);

       
var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 9 },
          colors
: ['#EF851C'],
          pointSize
: 30,
         
pointShape: { type: 'star', sides: 4 }
       
};

       
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart
.draw(data, options);
   
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div" style="width: 900px; height: 500px;"></div>
 
</body>
</html>

可以使用 dent 选项进一步自定义星形,该选项用于控制星形的凹度。当凹陷接近零时,星体会呈海星状;当凹陷接近 1 时,它会膨胀超过等边多边形。

以下是五面星形的凹陷,范围为 0.05 至 0.8:

var options = {
    legend
: 'none',
    hAxis
: { textPosition: 'none' },
    vAxis
: { textPosition: 'none', gridlines: { count: 0 },
             baselineColor
: 'white' },
    colors
: ['#E94D20', '#ECA403', '#63A74A',
             
'#15A0C8', '#4151A3', '#703593', '#981B48'],
    pointSize
: 20,
    annotations
: { stemColor: 'white', textStyle: { fontSize: 16 } },
    series
: {
       
0: { pointShape: { type: 'star', sides: 5, dent: 0.05 } },
       
1: { pointShape: { type: 'star', sides: 5, dent: 0.1 } },
       
2: { pointShape: { type: 'star', sides: 5, dent: 0.2 } },
       
3: { pointShape: { type: 'star', sides: 5 } },
       
4: { pointShape: { type: 'star', sides: 5, dent: 0.5 } },
       
5: { pointShape: { type: 'star', sides: 5, dent: 0.7 } },
       
6: { pointShape: { type: 'star', sides: 5, dent: 0.8 } },
   
}
};
<html>
 
<head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
      google
.charts.load("current", {packages:["corechart"]});
      google
.charts.setOnLoadCallback(drawChart);
     
function drawChart() {
         
var data = new google.visualization.DataTable();
          data
.addColumn('string', 'Element');
          data
.addColumn('number', 'A');
          data
.addColumn( { type: 'string', role: 'annotation' });
          data
.addColumn('number', 'B');
          data
.addColumn( { type: 'string', role: 'annotation' });
          data
.addColumn('number', 'C');
          data
.addColumn( { type: 'string', role: 'annotation' });
          data
.addColumn('number', 'D');
          data
.addColumn( { type: 'string', role: 'annotation' });
          data
.addColumn('number', 'E');
          data
.addColumn( { type: 'string', role: 'annotation' });
          data
.addColumn('number', 'F');
          data
.addColumn( { type: 'string', role: 'annotation' });
          data
.addColumn('number', 'G');
          data
.addColumn( { type: 'string', role: 'annotation' });
          data
.addRow(['A', 1, "dent: 0.05", , , , , , , , , , , , null]);
          data
.addRow(['B', , , 1, "dent: 0.1", , , , , , , , , , null]);
          data
.addRow(['C', , , , , 1, "dent: 0.2", , , , , , , , null]);
          data
.addRow(['D', , , , , , , 1, "default", , , , , , null]);
          data
.addRow(['E', , , , , , , , , 1, "dent: 0.5", , , , null]);
          data
.addRow(['F', , , , , , , , , , , 1, "dent: 0.7", , null]);
          data
.addRow(['G', , , , , , , , , , , , , 1, "dent: 0.8"]);

         
var options = {
              legend
: 'none',
              hAxis
: { textPosition: 'none' },
              vAxis
: { textPosition: 'none', gridlines: { count: 0 },
                       baselineColor
: 'white' },
              colors
: ['#E94D20', '#ECA403', '#63A74A',
                       
'#15A0C8', '#4151A3', '#703593', '#981B48'],
              pointSize
: 20,
              annotations
: { stemColor: 'white', textStyle: { fontSize: 16 } },
              series
: {
                 
0: { pointShape: { type: 'star', sides: 5, dent: 0.05 } },
                 
1: { pointShape: { type: 'star', sides: 5, dent: 0.1 } },
                 
2: { pointShape: { type: 'star', sides: 5, dent: 0.2 } },
                 
3: { pointShape: { type: 'star', sides: 5 } },
                 
4: { pointShape: { type: 'star', sides: 5, dent: 0.5 } },
                 
5: { pointShape: { type: 'star', sides: 5, dent: 0.7 } },
                 
6: { pointShape: { type: 'star', sides: 5, dent: 0.8 } },
             
}
         
};

         
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
          chart
.draw(data, options);
   
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div" style="width: 900px; height: 500px;"></div>
 
</body>
</html>

旋转

所有的点形状均可通过 rotation 选项进行旋转(以度为单位)。例如,我们可以将三角形旋转 180 度,让这些三角形在以下面积图中向下指:

        var options = {
          legend
: 'none',
          colors
: ['#15A0C8'],
          pointSize
: 30,
         
pointShape: { type: 'triangle', rotation: 180 }
       
};
  <head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
      google
.charts.load("current", {packages:["corechart"]});
      google
.charts.setOnLoadCallback(drawChart);
     
function drawChart() {
       
var data = google.visualization.arrayToDataTable
           
([['X', 'Y'],
             
[1, 3],
             
[2, 2.5],
             
[3, 2],
             
[4, 3],
             
[5, 4.5],
             
[6, 6.5],
             
[7, 9],
             
[8, 12]
       
]);

       
var options = {
          legend
: 'none',
          colors
: ['#15A0C8'],
          pointSize
: 30,
         
pointShape: { type: 'triangle', rotation: 180 }
       
};

       
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart
.draw(data, options);
   
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div" style="width: 900px; height: 500px;"></div>
 
</body>
</html>

自定义单个点

默认情况下,应用于点的样式会应用于系列中的所有点。如果您想更改特定数据点的外观,可以通过设置样式来实现。

在下图中,我们增大了其中一个点的大小,将不透明度降低至 0.3,然后更改形状和颜色:

<html>
 
<head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
      google
.charts.load('current', {'packages':['corechart']});
      google
.charts.setOnLoadCallback(drawChart);

     
function drawChart() {
       
var data = google.visualization.arrayToDataTable
           
([['X', 'Y', {'type': 'string', 'role': 'style'}],
             
[1, 3, null],
             
[2, 2.5, null],
             
[3, 3, null],
             
[4, 4, null],
             
[5, 4, null],
             
[6, 3, 'point { size: 18; shape-type: star; fill-color: #a52714; }'],
             
[7, 2.5, null],
             
[8, 3, null]
       
]);

       
var options = {
          legend
: 'none',
          hAxis
: { minValue: 0, maxValue: 9 },
          curveType
: 'function',
          pointSize
: 7,
          dataOpacity
: 0.3
       
};

       
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart
.draw(data, options);
   
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div" style="width: 900px; height: 500px;"></div>
 
</body>
</html>

可以进行以下样式自定义:

  • fill-color(以十六进制字符串形式指定。)
  • shape-dent
  • shape-rotation
  • shape-sides
  • shape-type
  • stroke-color(以十六进制字符串形式指定。)
  • stroke-width(以十六进制字符串形式指定。)
  • size
  • visible(点是否可见。)

不透明度并非通过样式控制,而是通过 dataOpacity 选项控制。