gpt4 book ai didi

javascript - 谷歌图表 : One Tooltip for Entire Column

转载 作者:可可西里 更新时间:2023-11-01 01:48:49 25 4
gpt4 key购买 nike

我有一个使用 Google Charts 的基本面积图.我可以为图表上的每个点设置工具提示,但有没有办法为一列中的所有点设置一个工具提示。

这是所需行为的屏幕截图:

One tooltip for entire column as per Highcharts

您可以看到,当光标位于图表上的一个点上时,会绘制一条垂直线,并显示一个工具提示来描述该列中的所有数据(两条线的数据)。查看live example如果需要,请点击此处。

这可以通过 Google Charts 实现吗?

最佳答案

您需要做的就是将以下内容添加到您的选项中(在折线图的情况下):focusTarget: 'category'

这是一个例子(只需打开 Google Playground 并将以上行添加到末尾的选项中):

function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['x', 'Cats', 'Blanket 1', 'Blanket 2'],
['A', 1, 1, 0.5],
['B', 2, 0.5, 1],
['C', 4, 1, 0.5],
['D', 8, 0.5, 1],
['E', 7, 1, 0.5],
['F', 7, 0.5, 1],
['G', 8, 1, 0.5],
['H', 4, 0.5, 1],
['I', 2, 1, 0.5],
['J', 3.5, 0.5, 1],
['K', 3, 1, 0.5],
['L', 3.5, 0.5, 1],
['M', 1, 1, 0.5],
['N', 1, 0.5, 1]
]);

// Create and draw the visualization.
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {curveType: "function",
width: 500, height: 400,
vAxis: {maxValue: 10},
// This line will make you select an entire row of data at a time
focusTarget: 'category'
}
);
}

简单易行!有关详细信息,请参阅 focusTarget in the Google Documentation

如果你想要更复杂的东西,你可以摆弄 domain Data Role

下面是一段代码示例:

 google.load('visualization', '1.1', {'packages':['corechart']});

google.setOnLoadCallback(drawChart_C6);

function drawChart_C6() {
var data = new google.visualization.DataTable();
data.addColumn({type: 'string', role: 'domain'}, '2009 Quarter');
data.addColumn('number', '2009 Sales');
data.addColumn('number', '2009 Expenses');
data.addColumn({type: 'string', role: 'domain'}, '2008 Quarter');
data.addColumn('number', '2008 Sales');
data.addColumn('number', '2008 Expenses');
data.addRows([
['Q1 \'09', 1000, 400, 'Q1 \'08', 800, 300],
['Q2 \'09', 1170, 460, 'Q2 \'08', 750, 400],
['Q3 \'09', 660, 1120, 'Q3 \'08', 700, 540],
['Q4 \'09', 1030, 540, 'Q4 \'08', 820, 620]
]);

var chart = new google.visualization.LineChart(document.getElementById('chart_C6'));
chart.draw(data, {width: 400, height: 240, legend:'right', focusTarget: 'category'});
}

关于javascript - 谷歌图表 : One Tooltip for Entire Column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15351243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com