Google图表是否支持向图表添加标签的功能?我需要在图表上添加标签,但是我不知道该怎么做。
我正在使用这种功能绘制图表:
function drawChart(node, rows) {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Index');
data.addRows(rows);
var options = {
titleTextStyle: {
color: '#00FF00',
},
height: 350,
width: $('#' + node).width(),
pointsVisible: true,
colors:['#00FF00'],
backgroundColor: '#1b1b1b',
legend: {position: 'right', textStyle: {color: '#fff', fontSize: 12}},
pointShape: 'square',
annotations: {
textStyle: {
fontName: 'Times-Roman',
fontSize: 18,
bold: true,
italic: true,
color: '#e5d385',
},
stem: {
length: 0
}
},
hAxis: {
title: "Date",
titleTextStyle: {
color: '#fff'
},
format: 'dd.MM.yyyy',
gridlines: {count: 15, color: '#7d7d7d'},
baselineColor: '#fff',
slantedTextAngle: 45,
slantedText: true,
baselineColor: '#fff',
minorGridlines:{
color: "#494949"
},
textStyle:{color: '#FFF'},
ticks: ticksX
},
vAxis: {
title: "Index",
titleTextStyle: {
color: '#fff'
},
gridlines: {color: '#7d7d7d'},
minValue: 0,
baselineColor: '#fff',
minorGridlines:{
color: "#494949"
},
textStyle:{color: '#FFF'},
ticks: ticksY
},
};
var dataView = new google.visualization.DataView(data);
dataView.setColumns([
// reference existing columns by index
0, 1,
// add function for line color
{
calc: function(data, row) {
var colorDown = '#FF0000';
var colorUp = '#00FF00';
if ((row === 0) && (data.getValue(row, 1) < data.getValue(row + 1, 1))) {
return colorDown;
} else if ((row > 0) && (data.getValue(row - 1, 1) < data.getValue(row, 1))) {
return colorDown;
}
return colorUp;
},
type: 'string',
role: 'style',
}
]);
var chart = new google.visualization.LineChart(document.getElementById(node));
chart.draw(dataView, options);
}
我找不到有关如何向图表添加标签的信息。所有文档均包含有关如何向图表的轴添加标签的信息。我需要在折线图点上方添加标签。
您能帮我吗?
答案 0 :(得分:0)
注释或标签是使用annotation column role添加的,
与样式列角色相似,已经在使用。
在这里,我向数据视图添加注释列角色,
显示最小值和最大值的标签...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var rows = [];
var oneDay = (24 * 60 * 60 * 1000);
var currentEndDate = new Date();
var currentStartDate = new Date(currentEndDate.getTime() - (oneDay * 365.25));
for (var i = currentStartDate.getTime(); i <= currentEndDate.getTime(); i = i + oneDay) {
var direction = (i % 2 === 0) ? 1 : -1;
var rowDate = new Date(i);
rows.push([rowDate, rowDate.getMonth() + (rowDate.getDate() * direction)]);
}
drawChart('chart_div', rows);
});
function drawChart(node, rows) {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Index');
data.addRows(rows);
var options = {
titleTextStyle: {
color: '#00FF00',
},
height: 350,
width: $('#' + node).width(),
pointsVisible: true,
colors:['#00FF00'],
backgroundColor: '#1b1b1b',
legend: {position: 'right', textStyle: {color: '#fff', fontSize: 12}},
pointShape: 'square',
annotations: {
textStyle: {
fontName: 'Times-Roman',
fontSize: 18,
bold: true,
italic: true,
color: '#e5d385',
},
stem: {
length: 0
}
},
hAxis: {
title: "Date",
titleTextStyle: {
color: '#fff'
},
format: 'dd.MM.yyyy',
gridlines: {count: 15, color: '#7d7d7d'},
baselineColor: '#fff',
slantedTextAngle: 45,
slantedText: true,
baselineColor: '#fff',
minorGridlines:{
color: "#494949"
},
textStyle:{color: '#FFF'}
},
vAxis: {
title: "Index",
titleTextStyle: {
color: '#fff'
},
gridlines: {color: '#7d7d7d'},
minValue: 0,
baselineColor: '#fff',
minorGridlines:{
color: "#494949"
},
textStyle:{color: '#FFF'}
},
};
var range = data.getColumnRange(1);
var dataView = new google.visualization.DataView(data);
dataView.setColumns([
// reference existing columns by index
0, 1,
// add function for line color
{
calc: function(data, row) {
var colorDown = '#FF0000';
var colorUp = '#00FF00';
if ((row === 0) && (data.getValue(row, 1) < data.getValue(row + 1, 1))) {
return colorDown;
} else if ((row > 0) && (data.getValue(row - 1, 1) < data.getValue(row, 1))) {
return colorDown;
}
return colorUp;
},
type: 'string',
role: 'style',
},
// add annotation
{
calc: function(data, row) {
var value = data.getValue(row, 1);
if (value === range.min) {
return 'Min: ' + data.getFormattedValue(row, 1);
} else if (value === range.max) {
return 'Max: ' + data.getFormattedValue(row, 1);
}
return null;
},
type: 'string',
role: 'annotation',
}
]);
var chart = new google.visualization.LineChart(document.getElementById(node));
chart.draw(dataView, options);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://www.gstatic.com/charts/loader.js'></script>
<div id="chart_div"></div>