gpt4 book ai didi

javascript - 谷歌图表 y 轴自定义

转载 作者:行者123 更新时间:2023-11-29 15:25:40 26 4
gpt4 key购买 nike

enter image description here

如何用每个 Y 轴编号显示星形图像或星形图标。例如,在条形图中,它会像 *5。所以每个数字都会有星号。

JS代码

google.charts.load("current", {packages: ["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var five = parseInt($("#fivestar").val());
var four = parseInt($("#fourstar").val());
var three = parseInt($("#threestar").val());
var two = parseInt($("#twostar").val());
var one = parseInt($("#onestar").val());
var data = google.visualization.arrayToDataTable([
["Element", "Stars", {role: "style"}],
["5", five, "#4F9808"],
["4", four, "#C4C62F"],
["3", three, "#E4C10D"],
["2", two, "color: #F6D219"]
]);
data.addRows([
["1", one, "color: #CA5006"]
]);

var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"},
2]);

var options = {
title: "Total Ratings",
width: 400,
height: 358,
bar: {groupWidth: "60%"},
chartArea: {top:10,left:30,width: '50%', height: '50%'},
legend: {position: "none"},
};
var chart = new google.visualization.BarChart(document.getElementById("chart_div"));
chart.draw(view, options);
}

我试过像这样在代码中加星标

var data = google.visualization.arrayToDataTable([
["Element", "Stars", {role: "style"}],
["<div class="star"><i class="fa fa-star" aria-hidden="true"></i></div>5", five, "#4F9808"],
["<div class="star"><i class="fa fa-star" aria-hidden="true"></i></div>4", four, "#C4C62F"],
["<div class="star"><i class="fa fa-star" aria-hidden="true"></i></div>3", three, "#E4C10D"],
["<div class="star"><i class="fa fa-star" aria-hidden="true"></i></div>2", two, "color: #F6D219"]
]);

但它不起作用..它只是将 html 标记打印为字符串。非常感谢任何帮助..

最佳答案

一种方法是手动添加和定位星形元素,
基于 Y 轴标签的 y 属性,
这可以在图表的 'ready' 事件触发时确定

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

google.charts.load("current", {packages: ["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var colors = ["#4F9808", "#C4C62F", "#E4C10D", "#F6D219", "#CA5006"];
var five = parseInt(1);
var four = parseInt(1);
var three = parseInt(2);
var two = parseInt(1);
var one = parseInt(1);
var data = google.visualization.arrayToDataTable([
["Element", "Stars", {role: "style"}],
["5", five, colors[0]],
["4", four, colors[1]],
["3", three, colors[2]],
["2", two, colors[3]]
]);
data.addRows([
["1", one, colors[4]]
]);

var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"},
2]);

var options = {
title: "Total Ratings",
width: 400,
height: 358,
bar: {groupWidth: "60%"},
chartArea: {top:10,left:30,width: '50%', height: '50%'},
legend: {position: "none"},
};

var container = document.getElementById("chart_div");
var chart = new google.visualization.BarChart(container);

// add stars
google.visualization.events.addListener(chart, 'ready', function () {
var colorIndex = 0;
Array.prototype.forEach.call(container.getElementsByTagName('text'), function(text) {
if ((text.getAttribute('text-anchor') === 'end') &&
(text.getAttribute('fill') === '#222222')) {
var stars = document.getElementById("star_div");
var star = stars.appendChild(document.createElement('i'));
star.className = "fa fa-star star";
star.style.color = colors[colorIndex];
star.style.top = (parseFloat(text.getAttribute('y'))) + 'px';
colorIndex++;
}
});
});

chart.draw(view, options);
}
.ratings {
display: inline-block;
padding: 6px;
text-align: right;
vertical-align: top;
}

.star {
position: absolute;
}

#star_div {
width: 16px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<div>
<div class="ratings" id="star_div"></div>
<div class="ratings" id="chart_div"></div>
</div>

关于javascript - 谷歌图表 y 轴自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39389618/

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