首页 > 解决方案 > Highcharts,5个小图表,有些随机不画

问题描述

我在高图表时遇到了一些奇怪的问题。我连续有 5 个小图表,有时其中一两个随机不会在屏幕上绘制。

这是我使用的代码

   // Twitter
// The file twitter_history.tsv is being manually generated by a given excel
            $.ajax({
                type: "GET",
                url: "downloads/twitter_history.tsv",
                dataType: "text",
                success: function(data) {

                        var twitter_hist_new_followers_options = {
                            chart: {
                                    renderTo: 'fol_mini',
                                    backgroundColor: null,
                                    borderWidth: 0,
                                    type: 'area',
                                    margin: [2, 0, 2, 0],
                                    style: {
                                        overflow: 'visible'
                                    },
                                    // small optimalization, saves 1-2 ms each sparkline
                                    skipClone: true,
                                    zoomType: 'x'
                                },
                                exporting: { enabled: false },
                                title: null,
                                credits: {
                                    enabled: false
                                },
                                xAxis: {
                                    categories: [],
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    startOnTick: false,
                                    endOnTick: false,
                                    tickPositions: []                                 
                                },
                                yAxis: {
                                    endOnTick: false,
                                    startOnTick: false,
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    tickPositions: [0]
                                },
                                legend: {
                                    enabled: false
                                },
                                series: [{
                                    name: 'Followers',
                                    data: []
                                }]
                        };

                        var twitter_hist_tweet_impressions_options = {
                            chart: {
                                    renderTo: 'imp_mini',
                                    backgroundColor: null,
                                    borderWidth: 0,
                                    type: 'area',
                                    margin: [2, 0, 2, 0],
                                    style: {
                                        overflow: 'visible'
                                    },
                                    // small optimalization, saves 1-2 ms each sparkline
                                    skipClone: true,
                                    zoomType: 'x'
                                },
                                exporting: { enabled: false },
                                title: null,
                                credits: {
                                    enabled: false
                                },
                                xAxis: {
                                    categories: [],
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    startOnTick: false,
                                    endOnTick: false,
                                    tickPositions: []                                 
                                },
                                yAxis: {
                                    endOnTick: false,
                                    startOnTick: false,
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    tickPositions: [0]
                                },
                                legend: {
                                    enabled: false
                                },
                                series: [{
                                    name: 'Impressions',
                                    data: []
                                }]
                        };

                        var twitter_hist_number_of_tweets_options = {
                            chart: {
                                    renderTo: 'twe_mini',
                                    backgroundColor: null,
                                    borderWidth: 0,
                                    type: 'area',
                                    margin: [2, 0, 2, 0],
                                    style: {
                                        overflow: 'visible'
                                    },
                                    // small optimalization, saves 1-2 ms each sparkline
                                    skipClone: true,
                                    zoomType: 'x'
                                },
                                exporting: { enabled: false },
                                title: null,
                                credits: {
                                    enabled: false
                                },
                                xAxis: {
                                    categories: [],
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    startOnTick: false,
                                    endOnTick: false,
                                    tickPositions: []                                 
                                },
                                yAxis: {
                                    endOnTick: false,
                                    startOnTick: false,
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    tickPositions: [0]
                                },
                                legend: {
                                    enabled: false
                                },
                                series: [{
                                    name: 'Tweets',
                                    data: []
                                }]
                        };

                        var twitter_hist_profile_visits_options = {
                            chart: {
                                    renderTo: 'vis_mini',
                                    backgroundColor: null,
                                    borderWidth: 0,
                                    type: 'area',
                                    margin: [2, 0, 2, 0],
                                    style: {
                                        overflow: 'visible'
                                    },
                                    // small optimalization, saves 1-2 ms each sparkline
                                    skipClone: true,
                                    zoomType: 'x'
                                },
                                exporting: { enabled: false },
                                title: null,
                                credits: {
                                    enabled: false
                                },
                                xAxis: {
                                    categories: [],
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    startOnTick: false,
                                    endOnTick: false,
                                    tickPositions: []                                 
                                },
                                yAxis: {
                                    endOnTick: false,
                                    startOnTick: false,
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    tickPositions: [0]
                                },
                                legend: {
                                    enabled: false
                                },
                                series: [{
                                    name: 'Visits',
                                    data: []
                                }]
                        };

                        var twitter_hist_mentions_options = {
                            chart: {
                                    renderTo: 'men_mini',
                                    backgroundColor: null,
                                    borderWidth: 0,
                                    type: 'area',
                                    margin: [2, 0, 2, 0],
                                    style: {
                                        overflow: 'visible'
                                    },
                                    // small optimalization, saves 1-2 ms each sparkline
                                    skipClone: true,
                                    zoomType: 'x'
                                },
                                exporting: { enabled: false },
                                title: null,
                                credits: {
                                    enabled: false
                                },
                                xAxis: {
                                    categories: [],
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    startOnTick: false,
                                    endOnTick: false,
                                    tickPositions: []                                 
                                },
                                yAxis: {
                                    endOnTick: false,
                                    startOnTick: false,
                                    labels: {
                                        enabled: false
                                    },
                                    title: {
                                        text: null
                                    },
                                    tickPositions: [0]
                                },
                                legend: {
                                    enabled: false
                                },
                                series: [{
                                    name: 'Mentions',
                                    data: []
                                }]
                        };

                        var lines=data.split('\n');
                        $.each(lines, function(lineNo, line) {
                        if (lineNo !== 0) {
                        var items = line.split('\t');
                        // New Followers
                        twitter_hist_new_followers_options.xAxis.categories.push(items[0]);
                        twitter_hist_new_followers_options.series[0].data.push(parseFloat(items[5]));
                        // Impressions
                        twitter_hist_tweet_impressions_options.xAxis.categories.push(items[0]);
                        twitter_hist_tweet_impressions_options.series[0].data.push(parseFloat(items[2]));
                        // Number of Tweets
                        twitter_hist_number_of_tweets_options.xAxis.categories.push(items[0]);
                        twitter_hist_number_of_tweets_options.series[0].data.push(parseFloat(items[1]));
                        // Profile Visits
                        twitter_hist_profile_visits_options.xAxis.categories.push(items[0]);
                        twitter_hist_profile_visits_options.series[0].data.push(parseFloat(items[3]));
                        // Mentions
                        twitter_hist_mentions_options.xAxis.categories.push(items[0]);
                        twitter_hist_mentions_options.series[0].data.push(parseFloat(items[4]));
                        }
                    });
                            var new_followers = new Highcharts.Chart(twitter_hist_new_followers_options);
                            var tweet_impressions = new Highcharts.Chart(twitter_hist_tweet_impressions_options);
                            var number_of_tweets = new Highcharts.Chart(twitter_hist_number_of_tweets_options);
                            var profile_visits = new Highcharts.Chart(twitter_hist_profile_visits_options);
                            var hist_mentions = new Highcharts.Chart(twitter_hist_mentions_options);

                }
            });

我究竟做错了什么?为什么有时第二个,有时第五个图表不显示?它让我疯狂。数据是静态的。

标签: javascripthighcharts

解决方案


尝试像这样在每次渲染之间添加一些时间:

var new_followers = new Highcharts.Chart(twitter_hist_new_followers_options);
setTimeout(function(){
   var tweet_impressions = new Highcharts.Chart(twitter_hist_tweet_impressions_options);
}, 200);
setTimeout(function(){
    var number_of_tweets = new Highcharts.Chart(twitter_hist_number_of_tweets_options);
}, 400);
setTimeout(function(){
    var profile_visits = new Highcharts.Chart(twitter_hist_profile_visits_options);
}, 600);
setTimeout(function(){
    var hist_mentions = new Highcharts.Chart(twitter_hist_mentions_options);
}, 800);

推荐阅读