javascript - 谷歌可视化 - 仅在特定点显示图例

标签 javascript charts google-visualization

我的页面中有下面的图表,但我需要显示一个图例,仅在第一个或最后一个点上显示“Meta”线,如下图所示。

可以吗?

google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawVisualization);


      function drawVisualization() {
        // Some raw data (not necessarily accurate)
        var data = google.visualization.arrayToDataTable([
         ['Year', 	'Anual',	'Mensal',	'Meta'],
         ['2012',  	46.17,  	,      		52.5],
         ['2013',  	45.34,  	,      		52.5],
         ['2014',  	45.24,  	,      		52.5],
         ['2015',  	45.05,  	,      		52.5],
         ['2016',  	46.69,  	,      		52.5],
         ['2016/01',  	,      		49.4,      	52.5],
         ['2016/02',  	,      		49.4,      	52.5],
         ['2016/03',  	,      		49.4,      	52.5],
         ['2016/04',  	,      		49.4,      	52.5],
         ['2016/05',  	,      		49.84,      	52.5],
         ['2016/06',  	,      		45.34,      	52.5],
         ['2016/07',  	,      		43.94,      	52.5],
         ['2016/08',  	,      		49.6,      	52.5]
      ]);

    var view = new google.visualization.DataView(data);
      view.setColumns([0, 1,
                       { calc: "stringify",
                         sourceColumn: 1,
                         type: "string",
                         role: "annotation" },
                       2, 
                       { calc: "stringify",
                         sourceColumn: 2,
                         type: "string",
                         role: "annotation" },
                       3, 
                       { calc: "stringify",
                         sourceColumn: 3,
                         type: "string",
                         role: "scope" } ]);

    var options = {
      title : 'OEE - KPI LB3',
      vAxis: {title: '%'},
      hAxis: {title: 'Período'},
      seriesType: 'bars',
      series: {
	1: {type: 'line'},
	2: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(view, options);
  }
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
       <div id="chart_div" style="width: 1200px; height: 500px;"></div>

我需要将此线显示为下图中的红色或紫色线。

enter image description here

最佳答案

认为您指的是在第一个或最后一个点上放置注释...

如果是这样,请使用calc函数仅返回第一行的注释...

{
  calc: function (dt, row) {
    if (row === 0) {
      return dt.getFormattedValue(row, 3);
    }
    return null;
  },
  type: 'string',
  role: 'annotation'
}

请参阅以下工作片段...

google.charts.load('current', {
  callback: function () {
    drawVisualization();
    window.addEventListener('resize', drawVisualization, false);
  },
  packages:['corechart']
});

function drawVisualization() {
  var data = google.visualization.arrayToDataTable([
    ['Year',   'Anual',  'Mensal', 'Meta'],
    ['2012',   46.17,    ,         52.5],
    ['2013',   45.34,    ,         52.5],
    ['2014',   45.24,    ,         52.5],
    ['2015',   45.05,    ,         52.5],
    ['2016',   46.69,    ,         52.5],
    ['2016/01',    ,         49.4,       52.5],
    ['2016/02',    ,         49.4,       52.5],
    ['2016/03',    ,         49.4,       52.5],
    ['2016/04',    ,         49.4,       52.5],
    ['2016/05',    ,         49.84,        52.5],
    ['2016/06',    ,         45.34,        52.5],
    ['2016/07',    ,         43.94,        52.5],
    ['2016/08',    ,         49.6,       52.5]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1,
    {
      calc: 'stringify',
      sourceColumn: 1,
      type: 'string',
      role: 'annotation'
    },
    2,
    {
      calc: 'stringify',
      sourceColumn: 2,
      type: 'string',
      role: 'annotation'
    },
    3,
    {
      calc: function (dt, row) {
        if (row === 0) {
          return dt.getFormattedValue(row, 3);
        }
        return null;
      },
      type: 'string',
      role: 'annotation'
    }
  ]);

  var options = {
    title : 'OEE - KPI LB3',
    vAxis: {title: '%'},
    hAxis: {title: 'Período'},
    seriesType: 'bars',
    series: {
      1: {
        color: 'green',
        type: 'line'
      },
      2: {
        color: 'red',
        type: 'line'
      }
    }
  };

  var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
  chart.draw(view, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

关于javascript - 谷歌可视化 - 仅在特定点显示图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42905333/

相关文章:

javascript - 图像 map 鼠标悬停突出显示

javascript - 多系列图表——时间(年)区间x轴叠加多年数据

javascript - 从 JSON 动画化 Google Chart

google-visualization - 缩放谷歌可视化图表的选定区域

javascript - 如何将本地 Excel 文件数据传递给 Javascript 数组(用于谷歌图表)?

javascript - NodeJS Express Passport-facebook 无法加载页面回调

javascript - 可扩展的多选下拉列表

javascript - 通过使用工具栏粘贴链接进行超文本处理

android - 我建立一个 Activity 图表是可以的,但是当我建立它的 fragment 时出现一些错误

c# - WinForm 图表控件 : Change size of chart when saving it to a file