ラインのカスタマイズ

概要

面グラフ、折れ線グラフ、複合グラフなど、一部の Google グラフではデータポイントを結ぶ線が表示されます。このページの手法を使用して、線の色、太さ、破線をカスタマイズできます。

色の変更

Google Chart のデータポイントを結ぶ線の色は、2 つの異なる方法で変更できます。1 つは、グラフのパレットを変更する colors オプションを使用する方法と、特定の系列の色を指定する series オプションを使用する方法です。

次のグラフでは、各系列の色を明示的に設定しています。

        var options = {
          legend
: 'none',
         
series: {
           
0: { color: '#e2431e' },
           
1: { color: '#e7711b' },
           
2: { color: '#f1ca3a' },
           
3: { color: '#6f9654' },
           
4: { color: '#1c91c0' },
           
5: { color: '#43459d' },
         
}

       
};
<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', '1', '2', '3', '4', '5', '6'],
             
[1, 2, 3, 4, 5, 6, 7],
             
[2, 3, 4, 5, 6, 7, 8],
             
[3, 4, 5, 6, 7, 8, 9],
             
[4, 5, 6, 7, 8, 9, 10],
             
[5, 6, 7, 8, 9, 10, 11],
             
[6, 7, 8, 9, 10, 11, 12]
       
]);

       
var options = {
          legend
: 'none',
         
series: {
           
0: { color: '#e2431e' },
           
1: { color: '#e7711b' },
           
2: { color: '#f1ca3a' },
           
3: { color: '#6f9654' },
           
4: { color: '#1c91c0' },
           
5: { color: '#43459d' },
         
}

       
};

       
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>

上記では、色は 16 進数値(例:'#e2431e'、飽和した赤の場合は '#f00')ですが、英語の名前でも指定できます。次の例は、これを示しており、個々の系列の色を設定するのではなく、colors オプションを使用してグラフのパレットを固定して色を制御する方法を示しています。違いの一つは、パレットを修正し、パレットの色の数よりもシリーズが多い場合、色は再利用されることです。パレットが赤と青で構成されている場合、シリーズの半分は赤、残りの半分は青になります。

        var options = {
          legend
: 'none',
         
colors: ['black', 'blue', 'red', 'green', 'yellow', 'gray']
       
};
<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', '1', '2', '3', '4', '5', '6'],
             
[1, 2, 3, 4, 5, 6, 7],
             
[2, 3, 4, 5, 6, 7, 8],
             
[3, 4, 5, 6, 7, 8, 9],
             
[4, 5, 6, 7, 8, 9, 10],
             
[5, 6, 7, 8, 9, 10, 11],
             
[6, 7, 8, 9, 10, 11, 12]
       
]);

       
var options = {
          legend
: 'none',
         
colors: ['black', 'blue', 'red', 'green', 'yellow', 'gray']
       
};

       
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>

太さの変更

lineWidth オプションを使用して、線の太さを制御できます。

        var options = {
          legend
: 'none',
          hAxis
: { maxValue: 7 },
          vAxis
: { maxValue: 13 },
         
lineWidth: 10,
          colors
: ['#e2431e', '#d3362d', '#e7711b',
                   
'#e49307', '#e49307', '#b9c246']
       
};
<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', '1', '2', '3', '4', '5', '6'],
             
[1, 2, 3, 4, 5, 6, 7],
             
[2, 3, 4, 5, 6, 7, 8],
             
[3, 4, 5, 6, 7, 8, 9],
             
[4, 5, 6, 7, 8, 9, 10],
             
[5, 6, 7, 8, 9, 10, 11],
             
[6, 7, 8, 9, 10, 11, 12]
       
]);

       
var options = {
          legend
: 'none',
          hAxis
: { maxValue: 7 },
          vAxis
: { maxValue: 13 },
         
lineWidth: 10,
          colors
: ['#e2431e', '#d3362d', '#e7711b',
                   
'#e49307', '#e49307', '#b9c246']
       
};

       
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>

系列の線幅を制御するには、series オプションを使用します。

        var options = {
          legend
: 'none',
          hAxis
: { maxValue: 7 },
          vAxis
: { maxValue: 13 },
         
series: {
           
0: { lineWidth: 1 },
           
1: { lineWidth: 2 },
           
2: { lineWidth: 4 },
           
3: { lineWidth: 8 },
           
4: { lineWidth: 16 },
           
5: { lineWidth: 24 }
         
},

          colors
: ['#e2431e', '#d3362d', '#e7711b',
                   
'#e49307', '#e49307', '#b9c246']
       
};
<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', '1', '2', '3', '4', '5', '6'],
             
[1, 2, 3, 4, 5, 6, 7],
             
[2, 3, 4, 5, 6, 7, 8],
             
[3, 4, 5, 6, 7, 8, 9],
             
[4, 5, 6, 7, 8, 9, 10],
             
[5, 6, 7, 8, 9, 10, 11],
             
[6, 7, 8, 9, 10, 11, 12]
       
]);

       
var options = {
          legend
: 'none',
          hAxis
: { maxValue: 7 },
          vAxis
: { maxValue: 13 },
         
series: {
           
0: { lineWidth: 1 },
           
1: { lineWidth: 2 },
           
2: { lineWidth: 4 },
           
3: { lineWidth: 8 },
           
4: { lineWidth: 16 },
           
5: { lineWidth: 24 }
         
},

          colors
: ['#e2431e', '#d3362d', '#e7711b',
                   
'#e49307', '#e49307', '#b9c246']
       
};

       
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>

破線

数値の配列を受け取る lineDashStyle オプションを使用すると、さまざまな形式の破線を使用できます。最初の数字はダッシュの長さを示し、2 番目の数字はその後のギャップを示します。3 つ目の数字がある場合は次のダッシュの長さ、4 つ目の数字がある場合は次の間隔の長さです。

グラフが描画されると、これらの長さが繰り返されるため、[4, 4] は 4 つの長さのダッシュと 4 つの長さのギャップの連続を意味します。[5, 1, 3] は、長さ 5 のダッシュ、1 長さのギャップ、3 長さのダッシュ、5 長さのギャップなどを意味します。たとえば次のような例が考えられます。

        var options = {
          hAxis
: { maxValue: 10 },
          vAxis
: { maxValue: 18 },
          chartArea
: { width: 380 },
          lineWidth
: 4,
         
series: {
           
0: { lineDashStyle: [1, 1] },
           
1: { lineDashStyle: [2, 2] },
           
2: { lineDashStyle: [4, 4] },
           
3: { lineDashStyle: [5, 1, 3] },
           
4: { lineDashStyle: [4, 1] },
           
5: { lineDashStyle: [10, 2] },
           
6: { lineDashStyle: [14, 2, 7, 2] },
           
7: { lineDashStyle: [14, 2, 2, 7] },
           
8: { lineDashStyle: [2, 2, 20, 2, 20, 2] }
         
},

          colors
: ['#e2431e', '#f1ca3a', '#6f9654', '#1c91c0',
                   
'#4374e0', '#5c3292', '#572a1a', '#999999', '#1a1a1a'],
       
};
<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', 'lineDashStyle: [1, 1]', 'lineDashStyle: [2, 2]',
               
'lineDashStyle: [4, 4]', 'lineDashStyle: [5, 1, 3]',
               
'lineDashStyle: [4, 1]',
               
'lineDashStyle: [10, 2]', 'lineDashStyle: [14, 2, 7, 2]',
               
'lineDashStyle: [14, 2, 2, 7]',
               
'lineDashStyle: [2, 2, 20, 2, 20, 2]'],
             
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
             
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
             
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
             
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
             
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
             
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
             
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
             
[8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
             
[9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
       
]);

       
var options = {
          hAxis
: { maxValue: 10 },
          vAxis
: { maxValue: 18 },
          chartArea
: { width: 380 },
          lineWidth
: 4,
         
series: {
           
0: { lineDashStyle: [1, 1] },
           
1: { lineDashStyle: [2, 2] },
           
2: { lineDashStyle: [4, 4] },
           
3: { lineDashStyle: [5, 1, 3] },
           
4: { lineDashStyle: [4, 1] },
           
5: { lineDashStyle: [10, 2] },
           
6: { lineDashStyle: [14, 2, 7, 2] },
           
7: { lineDashStyle: [14, 2, 2, 7] },
           
8: { lineDashStyle: [2, 2, 20, 2, 20, 2] }
         
},

          colors
: ['#e2431e', '#f1ca3a', '#6f9654', '#1c91c0',
                   
'#4374e0', '#5c3292', '#572a1a', '#999999', '#1a1a1a'],
       
};

       
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>