javascript - Anychart 不获取动态添加的数据

标签 javascript json charts anychart

我正在使用anychart在我的页面中绘制图表,我的代码是这样的

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>    
<script src="https://cdn.anychart.com/js/7.12.0/anychart-bundle.min.js"></script>
    <link rel="stylesheet" href="https://cdn.anychart.com/css/7.12.0/anychart-ui.min.css" />  
<input id="chart-charitytomoney" value="[[&quot;Charity 4&quot;,10.00],[&quot;Charity 2&quot;,20.00],[&quot;Charity Donate&quot;,100.00],[&quot;Donate Your Humanity&quot;,5920.00],[&quot;Gift your Work&quot;,3155.00],[&quot;Celebrate Baby Shower&quot;,770.00],[&quot;Refer Friends&quot;,110.00],[&quot;Gift Your Friends&quot;,200.00],[&quot;Celebrate B\u0027day With Us&quot;,220.00],[&quot;Celebrate Weekend&quot;,50.00],[&quot;Piggy Bank&quot;,4100.00],[&quot;Give a Single Gift&quot;,4050.00]]">
<div id="chart-container" style="height:550px!important"></div>
            <script type="text/javascript">

                $(document).ready(function(){
                anychart.onDocumentReady(function () {
                    var data = $("#chart-charitytomoney").val();
                 
                    // create column chart
                    chart = anychart.column();

                    // turn on chart animation
                    chart.animation(true);

                    // set chart title text settings
                    chart.title('Charities by donation');

                    // create area series with passed data

                    alert(data);
						var series = chart.column(data);

                    // set series tooltip settings
                    series.tooltip().titleFormatter(function () {
                        return this.x
                    });

                    series.tooltip().textFormatter(function () {
                        return '$' + parseInt(this.value).toLocaleString()
                    });
                    series.tooltip().position('top').anchor('bottom').offsetX(0).offsetY(5);

                    // set scale minimum
                    chart.yScale().minimum(0);

                    // set yAxis labels formatter
                    chart.yAxis().labels().textFormatter("${%Value}");

                    // tooltips position and interactivity settings
                    chart.tooltip().positionMode('point');
                    chart.interactivity().hoverMode('byX');

                    // axes titles
                    chart.xAxis().title('Product');
                    chart.yAxis().title('Revenue');

                    // set container id for the chart
                    chart.container('chart-container');

                    // initiate chart drawing
                    chart.draw();

                });
                });
            </script>

对我来说一切看起来都很好,但图表不起作用。 但如果我改变这一行

 var data = $("#chart-charitytomoney").val();

var data = [["Charity 4", 10.00], ["Charity 2", 20.00], ["Charity Donate", 100.00], ["Donate Your Humanity", 5920.00], ["Gift your Work", 3155.00], ["Celebrate Baby Shower", 770.00], ["Refer Friends", 110.00], ["Gift Your Friends", 200.00], ["Celebrate B\u0027day With Us", 220.00], ["Celebrate Weekend", 50.00], ["Piggy Bank", 4100.00], ["Give a Single Gift", 4050.00]]

一切正常。谁能指出我在这里做错了什么?我该如何克服它?

最佳答案

这是一种特殊的数据传递方式,但您可以这样做,只需:

选项 1

您应该在输入字段中使用引号:

<input id="chart-charitytomoney" value="[['Charity 4',10.00],['Charity 2',20.00],['Charity Donate',100.00],['Donate Your Humanity',5920.00],['Gift your Work',3155.00],['Celebrate Baby Shower',770.00],['Refer Friends',110.00],['Gift Your Friends',200.00],['Celebrate B\u0027day With Us',220.00],['Celebrate Weekend',50.00],['Piggy Bank',4100.00],['Give a Single Gift',4050.00]]">

并且您需要 eval() 结果:

var data = eval($("#chart-charitytomoney").val());

这是一个示例:http://jsfiddle.net/yr35w6nu/8/

但是,eval 并不是很安全,如果您想将数据存储在像这样的字段中的字符串中,请考虑使用如下代码:

选项 2

var data = JSON.parse($("#chart-charitytomoney").val().replace(/\'/g,'\"'));

此示例中所示:http://jsfiddle.net/yr35w6nu/9/

同样的情况也适用于您的代码:"e;:

var data = JSON.parse($("#chart-charitytomoney").val().replace(/\&quot;/g,'\"'));

解析引号示例:http://jsfiddle.net/yr35w6nu/10/

选项 3

还有一种存储 CSV 格式字符串的方法:

<input id="chart-charitytomoney" value="Charity 4,10.00;Charity 2,20.00;Charity Donate,100.00;Donate Your Humanity,5920.00;Gift your Work,3155.00;Celebrate Baby Shower,770.00\nRefer Friends,110.00;Gift Your Friends,200.00;Celebrate B\u0027day With Us,220.00;Celebrate Weekend,50.00\nPiggy Bank,4100.00\nGive a Single Gift,4050.00">

然后使用它:

var data = anychart.data.set($("#chart-charitytomoney").val(),{rowsSeparator: ';'});

http://jsfiddle.net/yr35w6nu/13/

关于javascript - Anychart 不获取动态添加的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41503735/

相关文章:

javascript - JavaScript 中的 Beget 函数 : The Good Parts

javascript - 您能想出一种方法来对具有该名称的自己的属性的对象调用 hasOwnProperty 吗?

php - 在php中使用json_encode的Mysql更新查询问题

javascript - 带有 id 的 ng-route 可以正常工作,但是 json 中带有 id 的 id 是错误的

从华夫饼图中删除空白方 block

javascript - 当用户使用 ShieldUI 单击 ChartLegend 时,如何禁用隐藏/显示图表的功能?

javascript - 如何在另一个变量中显示来自 XMLHttpRequest 的数据?

javascript - 实现 JavaScript 继承

json - 我的所有 .json 文件从 schemastore.azurewebsites.net 加载引用/架构时都出现问题

excel - 用于将 Chartsheets 打印到 PDF 的 VBA 宏以奇怪的比例生成截止图表