简体   繁体   English

如何使用for循环创建多个highchart并在HTML页面中打印?

[英]How to create multiple highchart and print in HTML page by using for loop?

I have below code in order for me to create multiple highchart: 我有以下代码,以便我创建多个highchart:

<script type='text/javascript'>
for(v=1;v<{{ uniindicatorproduct|safe }}+1;v++) {
    var container = "#container"+v;
    $(document).ready(function () {
       var title = { text: '{{ title|safe }}' };
        var xAxis = {categories: {{ t|safe }}, };
        var yAxis = {title: {text: ' '}, };
        var array ={{ y|safe }}
        var series = [],
            i = 0;
        var a = {{ x|safe }}
            lena = a.length
        for (i; i < lena; i++) {
            series.push({
                name: [a[i]],
                data: array[i]
            });
        }
        var json = {};
        .....
        $(container).highcharts(json);
    });
}
</script>

I have apply this on my above code How to make multiple charts using highcharts in a loop? 我已经在上面的代码中应用了此方法。 如何在循环中使用highcharts制作多个图表? , but this code only able to print the last chart, how to make my output that can print out all the highchart? ,但是此代码只能打印最后一张图表,如何使我的输出可以打印出所有图表?

One of the reasons it is not working is that you had not added the code in an anonymous function. 它不起作用的原因之一是您没有在匿名函数中添加代码。 Other was that you had added document.ready code in for loop of non document.ready codes. 另一种是,你已加入document.ready代码在非循环document.ready代码。 This makes the code skip the execution until document is ready. 这使代码跳过执行直到文档准备就绪。

<script type='text/javascript'>
    $(document).ready(function () {
        function createElem() {
            for(v=1;v<{{ uniindicatorproduct|safe }}+1;v++) {
                var container = "#container"+v;
                var title = { text: '{{ title|safe }}' };
                var xAxis = {categories: {{ t|safe }}, };
                var yAxis = {title: {text: ' '}, };
                var array ={{ y|safe }}
                var series = [],
                    i = 0;
                var a = {{ x|safe }}
                    lena = a.length
                for (i; i < lena; i++) {
                    series.push({
                        name: [a[i]],
                        data: array[i]
                    });
                }
                var json = {};
                .....
                $(container).highcharts(json);
            }
        }

        //Call the above function once html is ready
        createElem();
    });
</script>

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

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