- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用“经典”corechart 包制作的柱形图。
但是,我不明白如何将列值作为标签放在每个栏的顶部。 (例如,第一天 8.50)。
这是我的代码:
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
[
"",
"Hamburgers",
"Pizzas",
"Flutes",
"Wraps"
],
["29-04-2017", 8.50, 8.2, 8.4, 5.5],
["13-05-2017", 8.60, 8.32, 8.53, 5.67],
["12-06-2017", 8.30, 8.72, 8.13, 5.37],
["23-08-2017", 8.50, 8.22, 8.43, 5.57]
]);
var options = {
chart: {
title: ' ',
animation: {
duration: 2000,
easing: "out",
startup: true
}
},
legend: {position:'top'},
hAxis: { format: "date" },
vAxis: {
format: 'decimal',
viewWindow:{
max:10,
min:0
}
},
height: 400,
bars: 'vertical',
colors: ["#0F5470", "#8EA3A4", "#3EB8BC", "#98D4F2"]
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
最佳答案
您可以使用注释 column role向条形图添加标签
annotation列应该在数据表中的series列之后
请参阅以下工作片段
在这里,DataView
用于为每个系列 添加注释 列...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart() {
var data = google.visualization.arrayToDataTable([
[
"",
"Hamburgers",
"Pizzas",
"Flutes",
"Wraps"
],
["29-04-2017", 8.50, 8.2, 8.4, 5.5],
["13-05-2017", 8.60, 8.32, 8.53, 5.67],
["12-06-2017", 8.30, 8.72, 8.13, 5.37],
["23-08-2017", 8.50, 8.22, 8.43, 5.57]
]);
// build view
var viewColumns = [0];
$.each(new Array(data.getNumberOfColumns() - 1), function (index) {
viewColumns.push(index + 1);
viewColumns.push({
calc: function (dt, row) {
return dt.getFormattedValue(row, index + 1);
},
role: 'annotation',
type: 'string'
});
});
var view = new google.visualization.DataView(data);
view.setColumns(viewColumns);
var options = {
chart: {
title: ' ',
animation: {
duration: 2000,
easing: "out",
startup: true
}
},
legend: {position:'top'},
hAxis: { format: "date" },
vAxis: {
format: 'decimal',
viewWindow:{
max:10,
min:0
}
},
height: 400,
bars: 'vertical',
colors: ["#0F5470", "#8EA3A4", "#3EB8BC", "#98D4F2"]
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(view, options);
$(window).resize(function() {
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="chart_div"></div>
编辑
如果知道数据表的列数,
那么你不需要动态构建它们
setColumns
方法接受一个数组,
数组中的每个条目都可以是列索引 --> 0
或对象,如果使用计算列...
{
calc: function (dt, row) {
return dt.getFormattedValue(row, 1);
},
role: 'annotation',
type: 'string'
}
在这种情况下,我们需要包含数据表中的列,
以及每个注释列的计算列
请参阅以下工作片段...
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});
function drawChart() {
var data = google.visualization.arrayToDataTable([
[
"",
"Hamburgers",
"Pizzas",
"Flutes",
"Wraps"
],
["29-04-2017", 8.50, 8.2, 8.4, 5.5],
["13-05-2017", 8.60, 8.32, 8.53, 5.67],
["12-06-2017", 8.30, 8.72, 8.13, 5.37],
["23-08-2017", 8.50, 8.22, 8.43, 5.57]
]);
// build view
var view = new google.visualization.DataView(data);
view.setColumns([
0, // <-- x-axis column index
1, // <-- first y-axis column index
{ // annotation column for first y-axis column
calc: function (dt, row) {
return dt.getFormattedValue(row, 1); // get formatted value from first y-axis column
},
role: 'annotation',
type: 'string'
},
2, // <-- second y-axis column index
{ // annotation column for second y-axis column
calc: function (dt, row) {
return dt.getFormattedValue(row, 2); // get formatted value from second y-axis column
},
role: 'annotation',
type: 'string'
},
3,
{
calc: function (dt, row) {
return dt.getFormattedValue(row, 3);
},
role: 'annotation',
type: 'string'
},
4,
{
calc: function (dt, row) {
return dt.getFormattedValue(row, 4);
},
role: 'annotation',
type: 'string'
}
]);
var options = {
chart: {
title: ' ',
animation: {
duration: 2000,
easing: "out",
startup: true
}
},
legend: {position:'top'},
hAxis: { format: "date" },
vAxis: {
format: 'decimal',
viewWindow:{
max:10,
min:0
}
},
height: 400,
bars: 'vertical',
colors: ["#0F5470", "#8EA3A4", "#3EB8BC", "#98D4F2"]
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(view, options);
$(window).resize(function() {
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="chart_div"></div>
注意
您无法使用常规 for
循环动态构建列的原因,
是因为我们在对象中分配列索引
在这种情况下,一个引用被保存到i
{
calc: function (dt, row) {
return dt.getFormattedValue(row, i);
},
role: 'annotation',
type: 'string'
}
当循环结束并且列在 setColumns
中使用时,所有列都引用 i
,这是循环的最后一个值
在循环中使用 i
的值,而不是对 i
的引用
然后被构建的对象必须在闭包或函数中
这就是为什么在原始答案中使用 $.each
语句的原因
这会创建一个闭包并使用 index
的值,
而不是对 index
$.each(new Array(data.getNumberOfColumns() - 1), function (index) { // <-- this creates the closure...
viewColumns.push(index + 1);
viewColumns.push({
calc: function (dt, row) {
return dt.getFormattedValue(row, index + 1);
},
role: 'annotation',
type: 'string'
});
});
关于javascript - 谷歌图表 : How do I add a label to grouped column chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241604/
在whatsapp中,如果消息很短,文本和时间在同一行。如果消息很长,时间在右下角 - 上面的文字。 我如何在 Ios 中使用 Storyboard 实现此目的 最佳答案 尝试使用类似这样的方法来定义
我有这段代码: label.control-label{ font-weight: bold; } label.control-label::after{ content: ":";
尊敬的社区成员, 我想将测试中的文本放在 div 的中心。代码如下所示: Testing everything: 现在,如果我尝试以下代码部分: Testing everything: 它不会在
我有一个 DIV 元素,它有一个 并在其中输入文本框。 基本上,我在 DIV 元素上启用了 jQuery .resizable(),但是当您使 DIV 元素小于当前大小时,文本框会被推到新的一行。 我
请考虑以下标记。 This is a label 对我来说,这个标记是在我的自定义工具提示控件之后生成的。我在 IE 上的 JAWS 上看到的问题是它只读取“标题,而不是标签”,但是对于其他屏幕阅读
我正在按照文档使用 ionic 2 构建应用程序。我已经实现了一个带有 fab-list 的 fab 按钮。我试图在包含按钮旁边放置一个描述性标签。开箱即用的 ionic 2 似乎无法在 float
通常我使用标签标签来指向这样的输入标签 First Name: 现在我有了这个 First Name: 由于我以前没有穿过这样的东西,是否可以为 label 添加 label 标签。当我应用 Ja
我有一个包含换行符(“\r”)的传入文本字符串。 当我输出它时: System.out.println(myString) , 回车被解释。 但是,当我将字符串设置为标签的内容时,它会忽略回车。 如何
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 1年前关闭。 Improve thi
在 Excel 2013 中,我使用单元格中的值标记散点图。我希望标签不重叠。我可以手动移动标签,但我创建了一个过滤器来自动创建新绘图,因此我希望标签冲突也能自动发生。 这可能吗?无需 VBA 的解决
在我的 Struts2 JSP 中,我想显示一个 id,所以我写道: A${id}B ( A 和 B 用于调试) 我希望它显示为 Id:A7B 但 HTML 中生成了以下内容:A7BId: 为什么标签
我想要一个带注释的 AST,所以我定义了那些递归数据结构 使用 Fix : data Term a = Abstraction Name a | Application a a | Var
这两种方法都没有记录,并且似乎没有达到我的预期。 mylabel.setFontScale(3f); 使明显文本变大 3 倍(我正在寻找的),但与 Align.center 一起使用时无法正确居中>.
ScrollView里面有两个Label(多边的),下面是TableView(其中行数可能不同) Label 和 TableView 的高度都没有设置。 所有 outlet 都对彼此上方和下方的缩进设
我很好奇是否有一种简单的方法可以使标签采用 CSS 样式属性的默认值。我的复选框采用了我的选项卡的属性,我只希望它们成为默认值。正如您将看到的,我更改了复选框的字体大小,使其小于选项卡。但是,我不想仅
asp:label 和 html label 有什么区别? 我知道第一个是在服务器上呈现的,所以基本上它会返回一个跨度选项卡,但它有什么用呢?在什么情况下需要使用 HTML 标记,在什么情况下需要使用
我需要从网站中提取所有城市名称。我在以前的项目中使用了 beautifulSoup 和 RE,但在这个网站上,城市名称是常规文本的一部分,没有特定的格式。我找到了满足我要求的地理包 ( https:/
您好,我正在尝试添加 到表格的每个单元格。我在这里使用 Material 表:https://material-table.com/#/docs/features/component-overridi
我想制作一个简单的 R 图,y 轴标签位于 y 轴刻度标签上方。我用下面的代码创建了我喜欢的东西。但是它需要对 at 进行一些摸索。图形参数。 问:有没有更简单的方法来做到这一点?有没有办法查询 y
我可以绘制以下 df 的标签使用 geom_text : df 1 8 var 2 426 -276 hours worked per week N
我是一名优秀的程序员,十分优秀!