gpt4 book ai didi

javascript - 如何使用 JSON 数据为 Google Charts 实现样式或属性?

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

使用 JSON 格式数据时,将样式/属性应用于 Google 可视化图表的各个点的正确方法是什么?具体来说,我需要根据数据更改图表上各个点的颜色。例如 - 使用下面的 JSFiddle,您将如何将第一个点更改为另一种颜色?我尝试了各种使用“p”(属性)字段的选项,但没有成功。有人知道怎么做吗?

https://jsfiddle.net/burtonrhodes/fpd08jrt/14/

Json 数据

{
"cols": [
{
"id": "Week",
"label": "Week",
"type": "string",
"pattern": null,
"p": null
},
{
"id": "Showings",
"label": "Showings",
"type": "number",
"pattern": null,
"p": null
}
],
"rows": [
{
"c": [
{
"v": "1",
"f": null,
"p": null
},
{
"v": 2,
"f": null,
"p": {"point" : "fill-color: #FF7A06;"}
}
]
},
{
"c": [
{
"v": "2",
"f": null,
"p": null
},
{
"v": 1,
"f": null,
"p": null
}
]
}
]
}

最佳答案

在使用json时,这与其他答案的概念相同,
在样式的系列列之后添加一列,
如下……

{
"cols": [
{
"id": "Week",
"label": "Week",
"type": "string",
"pattern": null,
"p": null
},
{
"id": "Showings",
"label": "Showings",
"type": "number",
"pattern": null,
"p": null
},
{
"id": "Sytle",
"role": "style",
"type": "string",
"pattern": null,
"p": null
}
],
"rows": [
{
"c": [
{
"v": "1",
"f": null,
"p": null
},
{
"v": 2,
"f": null,
"p": null
},
{
"v": "point {fill-color: #ff7a06;}",
"f": null,
"p": null
}
]
},
{
"c": [
{
"v": "2",
"f": null,
"p": null
},
{
"v": 1,
"f": null,
"p": null
},
{
"v": null,
"f": null,
"p": null
}
]
}
]
}

关键是要有一个属性 --> "role": "style"
样式应该是行的值。

style role reference

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

google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable({
"cols": [
{
"id": "Week",
"label": "Week",
"type": "string",
"pattern": null,
"p": null
},
{
"id": "Showings",
"label": "Showings",
"type": "number",
"pattern": null,
"p": null
},
{
"id": "Sytle",
"role": "style",
"type": "string",
"pattern": null,
"p": null
}
],
"rows": [
{
"c": [
{
"v": "1",
"f": null,
"p": null
},
{
"v": 2,
"f": null,
"p": null
},
{
"v": "point {fill-color: #ff7a06;}",
"f": null,
"p": null
}
]
},
{
"c": [
{
"v": "2",
"f": null,
"p": null
},
{
"v": 1,
"f": null,
"p": null
},
{
"v": null,
"f": null,
"p": null
}
]
}
]
});

let chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, {
title: "Showings by Week",
fontName: "Arial",
fontSize: 14,
titleTextStyle: {
color: "#2B557D"
},
pointSize: 6,
colors: ['#C6D9FD'],
lineWidth: 1,
curveType: "line",
vAxis: {
minValue:0,
viewWindow: {
min: 0
},
maxValue: 3
},
hAxis: {
maxAlternation: 1,
//title: 'Week'
},
legend: {
position: 'none'
},
tooltip: {
isHtml: true
},
animation:{
duration: 1500,
easing: 'out',
startup: true
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

关于javascript - 如何使用 JSON 数据为 Google Charts 实现样式或属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841275/

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