简体   繁体   English

更新 workCloud 数据 - amcharts

[英]Update workCloud data - amcharts

With static data the word cloud works fine.对于静态数据,词云效果很好。
When the data change and I update html, the cloud doesn't update.当数据更改并且我更新 html 时,云不会更新。

This is my code in HTML这是我的 HTML 代码

<div id="chartdiv"></div>
<script type="text/javascript" src="../wordCloud.js"></script>
<script type="text/javascript">wordcloud(myData)</script>

This is How I initialize the chart这就是我初始化图表的方式

function wordcloud(myData) {        
    am4core.ready(function () {    
        // Themes begin
        am4core.useTheme(am4themes_animated);
        am4core.useTheme(am4themes_kelly);
        // Themes end

        var chart = am4core.create("chartdiv", am4plugins_wordCloud.WordCloud);
        chart.fontFamily = "Courier New";

        var series = chart.series.push(new am4plugins_wordCloud.WordCloudSeries());
        series.randomness = 0.1;
        series.rotationThreshold = 0.5;
        series.angles = [0];

        series.data = myData;

        series.dataFields.word = "tag";
        series.dataFields.value = "count";

        series.heatRules.push({
            "target": series.labels.template,
            "property": "fill",
            "min": am4core.color("#0000CC"),
            "max": am4core.color("#CC00CC"),
            "dataField": "value"
        });

        series.labels.template.tooltipText = "{word}: {value}";

        var hoverState = series.labels.template.states.create("hover");
        hoverState.properties.fill = am4core.color("#FF0000");

        var title = chart.titles.create();
        title.text = "Most frequent words in corpus";
        title.fontSize = 20;
        title.fontWeight = "800";

    });
}

Users can use a button to have more or less words displayed in the tag cloud.用户可以使用一个按钮在标签云中显示更多或更少的单词。
The new data is then calculated in the back end.然后在后端计算新数据。 But how can I update the cloud?但是我怎样才能更新云呢?
Thanks for any help.谢谢你的帮助。

Edit:编辑:

I read the documentation and also read this thread .我阅读了文档,也阅读了这个线程 But this is not helping me because the difference btw.但这对我没有帮助,因为顺便说一句。 word cloud and chart is that the data are added via series variable and not the chart variable.词云和图表是数据是通过series变量而不是chart变量添加的。

The same update rules mentioned in the linked thread apply to series-level data - replacing the array, calling addData or updating in place with invalidateRawData (both of which have series-level methods) will enable you to update the WordCloud.链接线程中提到的相同更新规则适用于系列级数据 - 替换数组、调用addData或使用invalidateRawData就地更新(两者都具有系列级方法)将使您能够更新 WordCloud。 Your code seems to have the same limitation as the previous thread's code where you don't have access to the chart variable outside of your method, so I'm not seeing how you would exactly update that instance without making similar changes.您的代码似乎与前一个线程的代码具有相同的限制,您无法在方法之外访问图表变量,因此我看不到您如何在不进行类似更改的情况下准确地更新该实例。

Basic demo below using a button that sets a new data array on the series:下面的基本演示使用在系列上设置新数据数组的按钮:

 var myData = [{ 'tag': 'Yes', 'count': 50 }, { 'tag': 'No', 'count': 50 }, { 'tag': 'Maybe', 'count': 50 }] am4core.useTheme(am4themes_animated); // Themes end var chart = am4core.create("chartdiv", am4plugins_wordCloud.WordCloud); chart.fontFamily = "Courier New"; var series = chart.series.push(new am4plugins_wordCloud.WordCloudSeries()); series.randomness = 0.1; series.rotationThreshold = 0.5; series.angles = [0]; series.data = myData; series.dataFields.word = "tag"; series.dataFields.value = "count"; series.heatRules.push({ "target": series.labels.template, "property": "fill", "min": am4core.color("#0000CC"), "max": am4core.color("#CC00CC"), "dataField": "value" }); series.labels.template.tooltipText = "{word}: {value}"; var hoverState = series.labels.template.states.create("hover"); hoverState.properties.fill = am4core.color("#FF0000"); var title = chart.titles.create(); title.text = "Most frequent words in corpus"; title.fontSize = 20; title.fontWeight = "800"; document.getElementById('change').addEventListener('click', function() { series.data = [{ 'tag': 'Yes', 'count': 50 }, { 'tag': 'No', 'count': 50 }, { 'tag': 'Maybe', 'count': 50 }, { 'tag': 'Sorta', 'count': 50 }, { 'tag': 'Kinda', 'count': 50 }] })
 #chartdiv { width: 100%; height: 350px; }
 <script src="//www.amcharts.com/lib/4/core.js"></script> <script src="//www.amcharts.com/lib/4/charts.js"></script> <script src="//www.amcharts.com/lib/4/plugins/wordCloud.js"></script> <script src="//www.amcharts.com/lib/4/themes/animated.js"></script> <div id="chartdiv"></div> <button id='change'>Change data</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM