Personalización de puntos

Descripción general

En muchos gráficos de Google, los valores de datos se muestran en puntos precisos. Un gráfico de líneas es solo un conjunto de estos puntos conectados por líneas, y un gráfico de dispersión es solo puntos.

En todos los gráficos, excepto en el de dispersión, estos puntos tienen un tamaño cero de forma predeterminada. Puedes controlar su tamaño con la opción pointSize y su forma con la opción pointShape.

En la parte superior, puedes ver un gráfico con seis series, cada una con pointSize 30, pero un pointShape diferente.

Opciones
        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' }
            }
        };
HTML completo
  <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>

Algunos ejemplos simples

A diferencia del gráfico de la sección anterior, la mayoría de los gráficos tienen solo una serie. Este es un ejemplo de un gráfico de líneas con puntos circulares de 20 puntos:

Como el pointShape predeterminado es el círculo, podemos excluirlo de las opciones:

Opciones
        var options = {
          legend: 'none',
          hAxis: { minValue: 0, maxValue: 9 },
          curveType: 'function',
          pointSize: 20,
      };
HTML completo
  <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>

Puedes cambiarla de “círculo” a otra forma si configuras pointShape en “triángulo”, “cuadrado”, “diamante”, “estrella” o “polígono”:

Opciones
        var options = {
          legend: 'none',
          hAxis: { minValue: 0, maxValue: 9 },
          colors: ['#795548'],
          pointSize: 20,
          pointShape: 'square'
        };
HTML completo
  <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>

Las formas “estrella” y “polígono” te permiten personalizar la cantidad de caras, y ambas tienen un valor predeterminado de cinco. Algunas estrellas de cuatro lados:

Opciones
        var options = {
          legend: 'none',
          hAxis: { minValue: 0, maxValue: 9 },
          colors: ['#EF851C'],
          pointSize: 30,
          pointShape: { type: 'star', sides: 4 }
        };
HTML completo
<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>

Las estrellas se pueden personalizar aún más con la opción dent, que controla qué tan cóncava es la estrella. Cuando la abolladura está cerca de cero, la estrella será similar a una estrella de mar; mientras la abolladura se acerca a una, se desplazará sobre un polígono equilátero.

Estas son las abolladuras que van de 0.05 a 0.8 para cinco estrellas:

Opciones
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 completo
<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>

Rotaciones

Todas las formas de los puntos se pueden rotar con la opción rotation, especificada en grados. Por ejemplo, podemos hacer que nuestros triángulos giren hacia abajo en el siguiente gráfico de áreas 180 grados:

Opciones
        var options = {
          legend: 'none',
          colors: ['#15A0C8'],
          pointSize: 30,
          pointShape: { type: 'triangle', rotation: 180 }
        };
HTML completo
  <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>

Cómo personalizar puntos individuales

De forma predeterminada, los estilos aplicados a un punto se aplican a todos los puntos de la serie. Si deseas cambiar el aspecto de un dato en particular, puedes hacerlo mediante el diseño de este.

En el siguiente gráfico, aumentamos el tamaño de uno de los puntos, reducimos la opacidad a 0.3 y cambiamos la forma y el color:

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

Las siguientes opciones de personalización de estilo están disponibles:

  • fill-color (especificado como una string hexadecimal).
  • shape-dent
  • shape-rotation
  • shape-sides
  • shape-type
  • stroke-color (especificado como una string hexadecimal).
  • stroke-width (especificado como una string hexadecimal).
  • size
  • visible (si el punto es visible o no)

La opacidad no se controla a través de un estilo, sino mediante la opción dataOpacity.