gpt4 book ai didi

javascript - 如何在 Google 图表的 x 轴上显示两个标题?

转载 作者:行者123 更新时间:2023-11-29 20:43:14 25 4
gpt4 key购买 nike

我正在使用 Google Charts 制作简单的散点图。我在 hAxis 中绘制了两个标题,我想将 hAxis 标题左右设置。

喜欢

Like

当前

current

我想在a轴上显示标题

function drawChart() {
// Define the chart to be drawn.
var data = new google.visualization.DataTable();
data.addColumn('number', '');
data.addColumn('number', '');
data.addRows([
[-0.5, 1],
[100, 1],
[-80, 2],
[25, 2],

[60, 8],
]);

// Set chart options
var options = {
title: 'guilt experience Vs eat satisfaction',
titlePosition: 'none',
position: 'center',
hAxis: {
title: '',
minValue: 0,
maxValue: 15,
ticks: [0, 20, 40, 60, 80, 100, -20, -40, -60, -80, -100]
},

vAxis: {
title: '',
minValue: 0,
ticks: [0, 1, 2, 3, 4, 5, 6, 7, 8]
},
legend: {
position: 'none'
},
};

// Instantiate and draw the chart.
var container = document.getElementById('chart_div');
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));

google.visualization.events.addListener(chart, 'ready', function() {
var layout = chart.getChartLayoutInterface();

for (var i = -0; i < data.getNumberOfRows(); i++) {
// add image above every fifth element
var xPos = layout.getXLocation(data.getValue(i, 0));
var yPos = layout.getYLocation(data.getValue(i, 1));
var whiteHat = container.appendChild(document.createElement('img'));
if (data.getValue(i, 0) < 0) {
whiteHat.src = 'https://i.imgur.com/LqiTeQI.png';
} else {
whiteHat.src = 'https://i.imgur.com/rACTObW.png';
}
whiteHat.style.position = 'absolute';
whiteHat.style.height = '15px';
whiteHat.style.width = '15px';
// 16x16 (image size in this example)
whiteHat.style.top = (yPos) + 'px';
whiteHat.style.left = (xPos) + 'px';
}
});
chart.draw(data, options);
}
google.charts.setOnLoadCallback(drawChart);
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js">
</script>
<script type="text/javascript">
google.charts.load('current', {
packages: ['corechart', 'scatter']
});
</script>
<div id="chart_div" ></div>

我正在使用 Google Charts 制作简单的散点图。我在 hAxis 中绘制了两个标题,我想将 hAxis 标题左右设置。

最佳答案

开箱即用的任何东西都不会让您添加两个 x 轴标题。

这里有几个选项...

1) 使用与放置图像相同的方法,放置几个 <span>具有所需标题的元素。

2) 使用 Unicode 不间断空格字符 ( \u00A0 ) 创建一个长标题。

例如

Guilt Value\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0Eat Value

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

function drawChart() {
// Define the chart to be drawn.
var data = new google.visualization.DataTable();
data.addColumn('number', '');
data.addColumn('number', '');
data.addRows([
[-0.5, 1],
[100, 1],
[-80, 2],
[25, 2],

[60, 8],
]);

// Set chart options
var options = {
title: 'guilt experience Vs eat satisfaction',
titlePosition: 'none',
position: 'center',
hAxis: {
title: 'Guilt Value\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0Eat Value',
minValue: 0,
maxValue: 15,
ticks: [0, 20, 40, 60, 80, 100, -20, -40, -60, -80, -100]
},

vAxis: {
title: '',
minValue: 0,
ticks: [0, 1, 2, 3, 4, 5, 6, 7, 8]
},
legend: {
position: 'none'
},
};

// Instantiate and draw the chart.
var container = document.getElementById('chart_div');
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));

google.visualization.events.addListener(chart, 'ready', function() {
var layout = chart.getChartLayoutInterface();

for (var i = -0; i < data.getNumberOfRows(); i++) {
// add image above every fifth element
var xPos = layout.getXLocation(data.getValue(i, 0));
var yPos = layout.getYLocation(data.getValue(i, 1));
var whiteHat = container.appendChild(document.createElement('img'));
if (data.getValue(i, 0) < 0) {
whiteHat.src = 'https://i.imgur.com/LqiTeQI.png';
} else {
whiteHat.src = 'https://i.imgur.com/rACTObW.png';
}
whiteHat.style.position = 'absolute';
whiteHat.style.height = '15px';
whiteHat.style.width = '15px';
// 16x16 (image size in this example)
whiteHat.style.top = (yPos) + 'px';
whiteHat.style.left = (xPos) + 'px';
}
});
chart.draw(data, options);
}
google.charts.setOnLoadCallback(drawChart);
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js">
</script>
<script type="text/javascript">
google.charts.load('current', {
packages: ['corechart', 'scatter']
});
</script>
<div id="chart_div" ></div>

关于javascript - 如何在 Google 图表的 x 轴上显示两个标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55039117/

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