[英]Displaying JSON in Chart.js
I'm fetching and parsing JSON data from a local file and I'm using Chart.js to display the data.我正在从本地文件中获取和解析 JSON 数据,并且我正在使用Chart.js来显示数据。 The data I want to be displayed is the number of times a given word is used and the word as the chart label.我想要显示的数据是给定单词的使用次数以及作为图表标签的单词。
Issues:问题:
I've managed to get the word and occurrence pair within the wordCount
function.我已经设法在wordCount
函数中获得了单词和出现对。 If I log out counts
, I can see the match.如果我注销counts
,我可以看到比赛。 What I'm having a pain point with is how to properly display the data with what I have so far.我的痛点是如何正确显示我目前拥有的数据。 I'm seeing some of it displayed, but it isn't correct.我看到其中一些显示出来,但它不正确。
Code:代码:
var ctx = document.getElementById('myChart').getContext('2d');
const tweets = []
const counts = {}
const keyz = []
// Fetch JSON file locally and return the data
fetch('../test_feed.json')
.then(res => res.json())
.then(data => {
handleTweetText(data)
compare()
wordCount()
createChart()
})
.catch(err => console.error(err));
function handleTweetText(data) {
for (var i = 0; i < data.content.length; i++) {
const tweetHtml = data.content[i].content.bodyHtml;
// If tweet exists, return it
if(tweetHtml && tweetHtml != '') {
// Regex to grab entire anchor tag
const regEx = /<a(\s[^>]*)?>.*?<\/a>/ig
// Replace anchor with empty string and remove empty space
const splitTweetText = tweetHtml.replace(regEx, '').trim()
tweets.push(splitTweetText)
}
}
}
function wordCount() {
const allWords = tweets.join('\n')
const tokens = allWords.split(/\W+/)
for (let i = 0; i < tokens.length; i++) {
var word = tokens[i].toLowerCase()
if (counts[word] === undefined) {
counts[word] = 1
keyz.push(word)
} else {
counts[word] = counts[word] + 1
}
}
}
// sort word occurrence
keyz.sort(compare)
function compare(a ,b,) {
const countA = counts[a]
const countB = counts[b]
return countB - countA
}
// Chart
function createChart() {
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: keyz,
datasets: [{
label: 'Word Count',
data: keyz,
backgroundColor: [
'rgba(255, 99, 132, 0.2)'
],
borderWidth: 1
}]
}
})}
The labels
fiels and data
field within the datasets
are where I need to place my paired data. data
datasets
的labels
字段和data
字段是我需要放置配对数据的地方。
I've been working at this for awhile now with no luck.我已经在这方面工作了一段时间,但没有运气。
for one, fetch
is asynchronous, so you have to wait for it to finish.一方面, fetch
是异步的,所以你必须等待它完成。
which is why you have then
methods.这就是为什么你有then
方法。
however, you're trying to sort keyz
before keyz
has any data.但是,您正在尝试在keyz
有任何数据之前对keyz
进行排序。
move the sort function into the then
method,将 sort 函数移动到then
方法中,
after wordCount
where keyz
is filled.在填充wordCount
keyz
之后。
and calling compare
on its own, does nothing...并自行调用compare
,什么都不做......
// Fetch JSON file locally and return the data
fetch('../test_feed.json')
.then(res => res.json())
.then(data => {
handleTweetText(data)
wordCount()
keyz.sort(compare)
createChart()
})
.catch(err => console.error(err));
but typically, you don't use the same data for both labels
and data
in the chart.但通常情况下,您不会对图表中的labels
和data
使用相同的数据。
is it possible you meant to use counts
for data
in the chart?您是否可能打算对图表中的data
使用counts
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.