Google Visualization 堆叠条形图中的标签值和总计

2023-11-24

I am trying to display the value of each bar and then the total value of all bars in a stacked bar chart The problem is, I get the last bar's value and the total both outside the bar. If I do not show the total, I get the value inside the bar. Using the code below the the last two annotations are outside the second bar even when the second bar is long enough to show its label. Full Code - this is close

<html>
  <head>
    <base target="_top">
  </head>
  <body>

         <h3>Registrations</h3>
         <div id="registrations_chart"></div>

  </body>
</html>
<script>
google.charts.load('current', {'packages':['corechart', 'bar', 'gauge', 'table']});

google.charts.setOnLoadCallback(drawStackedBar);

function drawStackedBar() {

  var myHeight = 350;
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Registrations');
  data.addColumn('number', 'Regular');
  data.addColumn('number', 'Work Study');

  data.addRows([
    ['Adult', 20, 12],
    ['Teen', 3, 0]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0,
    1, {
      calc: function (dt, row) {
      return dt.getValue(row, 1);
    },
    type: "number",
    role: "annotation"
    },
    2, {
      calc: function (dt, row) {
      return dt.getValue(row, 2);
    },
    type: "number",
    role: "annotation"
    },
    // series 1
    {
      calc: function (dt, row) {
      return dt.getValue(row, 1) + dt.getValue(row, 2);
    },
    type: "number",
    role: "annotation"
    }
  ]);


  var options = {
    animation:{
      duration: 1000,
      easing: 'out',
      startup: true
    },
    title: 'Registrations',
    backgroundColor: 'transparent',
    height: myHeight, width: 500,
    legend: {
      position: 'top',
      maxLines: 3
    },
    bar: { groupWidth: '75%' },
    isStacked: true
  };
  var chart = new google.visualization.BarChart(document.getElementById('registrations_chart'));
  chart.draw(view, options);
}
</script>

如果我删除了 setColumns 中的最后一个选项以使该部分如下所示:

  view.setColumns([0,
    1, {
      calc: function (dt, row) {
      return dt.getValue(row, 1);
    },
    type: "number",
    role: "annotation"
    },
    2, {
      calc: function (dt, row) {
      return dt.getValue(row, 2);
    },
    type: "number",
    role: "annotation"
    },
    // series 1
    {
      calc: function (dt, row) {
      return dt.getValue(row, 1) + dt.getValue(row, 2);
    },
    type: "number",
    role: "annotation"
    }
  ]);

I get the labels where I want them without the Total, as shown below enter image description here

What I am after is to add the Total to the end and the Labels consistently inside as shown below: enter image description here

我尝试了太多的方法来记住或在这里列出,但最后我没有得到总数。我怎样才能让最后一个标签出现,并在其他标签适合时将它们保留在栏中?请注意,我已将红色条设置为比蓝色条更长,并且数字仍然显示如图所示。


您可以为总计添加另一个系列列

然后使用以下选项将其从图表中隐藏...

    enableInteractivity: false,
    tooltip: "none",
    visibleInLegend: false

这将允许其他值的注释正常执行

总注释将始终显示在外面,
但需要调整一些选项以防止它覆盖其他选项......

    annotations: {
      stem: {
        color: "transparent",
        length: 28
      },
      textStyle: {
        color: "#000000",
      }
    },

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

google.charts.load('50', {
  packages:['corechart']
}).then(function () {
  var myHeight = 350;
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Registrations');
  data.addColumn('number', 'Regular');
  data.addColumn('number', 'Work Study');

  data.addRows([
    ['Adult', 20, 12],
    ['Teen', 3, 0]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0,
    1, {
      calc: function (dt, row) {
        return dt.getValue(row, 1);
      },
      type: "number",
      role: "annotation"
    },
    2, {
      calc: function (dt, row) {
        return dt.getValue(row, 2);
      },
      type: "number",
      role: "annotation"
    },
    {
      calc: function (dt, row) {
        return 0;
      },
      label: "Total",
      type: "number",
    },
    {
      calc: function (dt, row) {
        return dt.getValue(row, 1) + dt.getValue(row, 2);
      },
      type: "number",
      role: "annotation"
    }
  ]);


  var options = {
    animation:{
      duration: 1000,
      easing: 'out',
      startup: true
    },
    title: 'Registrations',
    backgroundColor: 'transparent',
    height: myHeight, width: 500,
    legend: {
      position: 'top',
      maxLines: 3
    },
    bar: { groupWidth: '75%' },
    isStacked: true,
    series: {
      2: {
        annotations: {
          stem: {
            color: "transparent",
            length: 28
          },
          textStyle: {
            color: "#000000",
          }
        },
        enableInteractivity: false,
        tooltip: "none",
        visibleInLegend: false
      }
    }
  };
  var chart = new google.visualization.BarChart(document.getElementById('registrations_chart'));
  chart.draw(view, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="registrations_chart"></div>

EDIT

注释位置没有标准选项
但一旦图表出现,您可以手动移动它们'ready'事件引发
或者在这种情况下,'animationfinish' event

请参阅以下工作片段,
总注释向下移动...

google.charts.load('50', {
  packages:['corechart']
}).then(function () {
  var myHeight = 350;
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Registrations');
  data.addColumn('number', 'Regular');
  data.addColumn('number', 'Work Study');

  data.addRows([
    ['Adult', 20, 12],
    ['Teen', 3, 0]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0,
    1, {
      calc: function (dt, row) {
        return dt.getValue(row, 1);
      },
      type: "number",
      role: "annotation"
    },
    2, {
      calc: function (dt, row) {
        return dt.getValue(row, 2);
      },
      type: "number",
      role: "annotation"
    },
    {
      calc: function (dt, row) {
        return 0;
      },
      label: "Total",
      type: "number",
    },
    {
      calc: function (dt, row) {
        return dt.getValue(row, 1) + dt.getValue(row, 2);
      },
      type: "number",
      role: "annotation"
    }
  ]);


  var options = {
    animation:{
      duration: 1000,
      easing: 'out',
      startup: true
    },
    title: 'Registrations',
    backgroundColor: 'transparent',
    height: myHeight, width: 500,
    legend: {
      position: 'top',
      maxLines: 3
    },
    bar: { groupWidth: '75%' },
    isStacked: true,
    series: {
      2: {
        annotations: {
          stem: {
            color: "transparent",
          },
          textStyle: {
            color: "#000000",
          }
        },
        enableInteractivity: false,
        tooltip: "none",
        visibleInLegend: false
      }
    }
  };
  var chart = new google.visualization.BarChart(document.getElementById('registrations_chart'));

  google.visualization.events.addListener(chart, 'animationfinish', function () {
    $('#registrations_chart text[fill="#000000"]').each(function (index, annotation) {
      if (!isNaN(parseFloat(annotation.textContent))) {
        var yCoord = parseFloat($(annotation).attr('y'));
        $(annotation).attr('y', yCoord + 18);
      }
    });
  });

  chart.draw(view, options);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="registrations_chart"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Visualization 堆叠条形图中的标签值和总计 的相关文章

随机推荐

  • 将 HttpPostedFileBase 转换为 byte[]

    在我的 MVC 应用程序中 我使用以下代码来上传文件 MODEL public HttpPostedFileBase File get set VIEW Html TextBoxFor m gt m File new type file 一
  • 如何在 Gnome Shell 中设置应用程序标题?

    我是 Gtk 开发的新手 正在尝试使用 PyGObject 和 Gtk 3 0 编写一个应用程序 然而 当我从命令行在 Gnome Shell 中运行应用程序时 出现在左上角 紧邻 活动 热角右侧 的应用程序名称仅设置为 Python 源文
  • 在 Google 地图上制作可点击的多边形(适用于 Android)

    我有一个城市各个区域的连续纬度 有什么方法可以用它创建可点击的多边形吗 一次可行的方法是 使用可用的 LatLngs 生成多边形 我想用颜色编码在地图上直观地显示多边形 Set up setOnMapClickListener 做多边形内的
  • 如何使用打字稿文件运行 gulp

    有一个使用 gulp js 文件的 gulp 项目 但我的项目是用 typescript 编写的 所以我宁愿有一个用 typescript 编写的 gulp 文件 可以将这个过程分为两个步骤 其中我 1 手动将typescript gulp
  • 第一次使用 SpriteKit 播放声音时出现轻微延迟

    当我使用 self playSoundFileNamed 播放声音时 第一次播放声音时会有一点延迟 整个应用程序冻结大约半秒 但之后就没问题了 我怎样才能摆脱这个 在我的游戏设置方法中 我做了类似的事情 看起来效果很好 拥有 iVar SK
  • 解压缩和 * 运算符[重复]

    这个问题在这里已经有答案了 python 文档将此代码提供为 zip 的逆操作 gt gt gt x2 y2 zip zipped 尤其 zip 与 运算符结合使用可用于解压缩列表 有人可以向我解释 运算符在这种情况下如何工作吗 据我了解
  • 如何在Android Studio项目中使用最新的FFMPEG?

    I have a simple task to make a video from multiple images and an audio file After searching a lot found that its possibl
  • 显示动画 GIF

    我想在我的应用程序中显示动画 GIF 图像 我发现 Android 本身并不支持动画 GIF 但是它可以使用显示动画动画绘图 开发 gt 指南 gt 图像和图形 gt 可绘制对象概述 该示例使用在应用程序资源中保存为帧的动画 但我需要的是直
  • 在 C# 中创建气球工具提示

    我可以知道如何在用 C 编码的应用程序中制作弹出气泡消息吗 例如 当我启动我的应用程序时 它会弹出 欢迎使用 UbuntuSE 应用程序 是的 弹出窗口不是消息框弹出窗口 而是托盘菜单中的弹出窗口 与此类似的东西 附言 如果我没记错的话 这
  • 使用 TypeScript 和注入的 AngularJS 过滤器

    有人可以给我提供一个示例 说明如何在 TypeScript 中创建使用依赖注入的 Angular 过滤器 底部是我目前拥有的 工作正常 但我想做的是我想要访问 filter 的函数 以便我可以将 return date ToString 行
  • 如何将 TRC20 交易发送到某个地址

    我正在使用 tron web 查询某个地址的交易 但它不会返回发送到该地址的交易 其中传输的代币为 TRC20 这是行不通的 我想获取某个地址上的交易并获取 TRX trc10 和 trc20 交易 我做错了什么或者该怎么做 这是我的代码块
  • 如何使用 JDBC 执行 .sql 脚本文件[重复]

    这个问题在这里已经有答案了 可能的重复 使用 MySQL 和 JDBC 运行 sql 脚本 我有一个 SQL 脚本文件 其中包含 40 50 个 SQL 语句 是否可以使用 JDBC 运行此脚本文件 此链接可能会帮助您 http paste
  • np.empty 与 np.zeros 的速度

    我正在使用 numpy 版本 1 14 3 和 python 2 7 12 参考文献this问题 我发现使用 np zeros 和 np empty 初始化数组之间的速度显着不同 但是 输出是相同的 import numpy as np r
  • 修改 ASP.NET 服务器端的 html 输出

    第三方的webcontrol生成以下代码来显示自己 div div
  • 如何在管道步骤中使用 Jenkins 侧边栏链接插件?

    我正在开发这个插件https plugins jenkins io sidebar link 在詹金斯侧栏中添加链接 该插件与 jenkins 项目配置一起使用 现在我正在尝试添加一个管道步骤来调用此插件 我已经尝试过下面的代码行 但它不起
  • Apache HttpClient 制作多部分表单帖子

    我对 HttpClient 很陌生 而且我发现缺乏 和 或明显不正确 文档非常令人沮丧 我正在尝试使用 Apache Http Client 实现以下帖子 如下所列 但不知道如何实际执行 下周我将埋头于文档中 但也许更有经验的 HttpCl
  • 在 Python 3 中使用 XPath 解析 XML

    我有以下 xml
  • 使用 Stream 并关闭流时出现错误的 WebFaultException

    我们有一个使用 WCF 构建的 REST API 我们使用 WebFaultException 处理所有后端异常 如下所示 throw new WebFaultException
  • 在 Maven 项目中包含非 Java 源

    我正在开始一个项目 我预计该项目将包含大量非 Java 代码 主要是 shell 和 SQL 脚本 我仍然想用 Maven 来管理这个项目 非 Java 源代码和 Maven 的最佳实践是什么 源码应该去哪里 它们在生命周期的不同阶段会发生
  • Google Visualization 堆叠条形图中的标签值和总计

    I am trying to display the value of each bar and then the total value of all bars in a stacked bar chart The problem is