简体   繁体   English

在highcharts中输入异步数据

[英]Asynchronous data input in highcharts

I am loading data from a MySQL database using a $.getJSON method. 我使用$ .getJSON方法从MySQL数据库加载数据。 All data is then displayed in highcharts. 然后所有数据都显示在高图中。 There are different buttons which the user can click in order to change between datasets. 用户可以单击不同的按钮以在数据集之间切换。 My problem is that when the page is first loaded, the chart will always load up empty. 我的问题是,当首次加载页面时,图表将始终加载为空。 I found out that this is probably because the $.getJSON method i am using for retrieving the data is asynchronous. 我发现这可能是因为我用于检索数据的$ .getJSON方法是异步的。 I am not sure whether this can be resolved by switching the getJSON method to an AJAX method. 我不确定是否可以通过将getJSON方法切换为AJAX方法来解决这个问题。 I have tried but did not succeed. 我试过但没有成功。 Or whether it is my javascript function for switching chart data upon button-clicks that is the problem. 或者是否是我的javascript函数,用于在按钮单击时切换图表数据,这是问题所在。 Below is all the code. 以下是所有代码。 The fragments of code I think are responsible for the problem is then showed below. 我认为负责问题的代码片段如下所示。 I know you might want a JSFiddle but since I am using database data I do now know how to properly display the problem. 我知道你可能想要一个JSFiddle,但由于我使用的是数据库数据,我现在知道如何正确显示问题。

<script>
  var d = new Date();
  var pointStart = d.getTime();
  //-------------------------------------------------------
  //set a 'line' marker type for use in bullet charts 
  Highcharts.Renderer.prototype.symbols.vline = function(x, y, width, height) {
    return ['M', x, y + width / 2, 'L', x + height, y + width / 2];
  };
  Highcharts.Renderer.prototype.symbols.hline = function(x, y, width, height) {
    return ['M', x, y + height / 2, 'L', x + width, y + width / 2];
  };
  //-------------------------------------------------------
  Highcharts.setOptions({
    global: {
      useUTC: false
    },
    colors: [
      'rgba( 253, 99,  0,   0.9 )', //bright orange
      'rgba( 40,  40,  56,  0.9 )', //dark
      'rgba( 253, 0,   154, 0.9 )', //bright pink
      'rgba( 154, 253, 0,   0.9 )', //bright green
      'rgba( 145, 44,  138, 0.9 )', //mid purple
      'rgba( 45,  47,  238, 0.9 )', //mid blue
      'rgba( 177, 69,  0,   0.9 )', //dark orange
      'rgba( 140, 140, 156, 0.9 )', //mid
      'rgba( 238, 46,  47,  0.9 )', //mid red
      'rgba( 44,  145, 51,  0.9 )', //mid green
      'rgba( 103, 16,  192, 0.9 )' //dark purple
    ],
    chart: {
      alignTicks: false,
      type: '',
      margin: [70, 100, 70, 90],
      //borderRadius:10,
      //borderWidth:1,
      //borderColor:'rgba(156,156,156,.25)',
      //backgroundColor:'rgba(204,204,204,.25)',
      //plotBackgroundColor:'rgba(255,255,255,1)',
      style: {
        fontFamily: 'Abel,serif'
      },
      events: {
        load: function() {
          this.credits.element.onclick = function() {
            window.open(
              ''
            );
          }
        }
      }
    },

  });
</script>



<script>
var chart,
  chartOptions = {},
  chartData = {};


chartOptions.chart1 = {
  chart: {
    type: 'column'
  },
  title: {
    text: '<?php echo $companyName; ?>',
    style: {
      fontSize: '20px'
    }
  },
  subtitle: {
      text: 'Revenues',
      align: 'left',
      x: 55,
      style: {
        fontSize: '16px'
      }
  },
  xAxis: {
    categories: []
  },
  yAxis: {
    title: {
      text: '<?php echo $unitCurr; ?>'
    }
  },
  series: [{
    name: 'Revenues',
    color: '#006699',
    data: []
  }]
};
var tableName = '<?php echo $tableName; ?>'
$.getJSON("../../companies/charts/Data.php", {id: escape(tableName)}, function(json) {
    chartOptions.chart1.xAxis.categories = json[0]['data'];
    chartOptions.chart1.series[0].data = json[1]['data'];
});


chartOptions.chart2 = {
  chart: {
    type: 'column'
  },
  title: {
    text: '<?php echo $companyName; ?>',
    style: {
      fontSize: '20px'
    }
  },
  subtitle: {
      text: 'Earnings before interest and taxes',
      align: 'left',
      x: 55,
      style: {
        fontSize: '16px'
      }
  },
  xAxis: {
    categories: []
  },
  yAxis: {
    title: {
      text: '<?php echo $unitCurr; ?>'
    }
  },
  series: [{
    name: 'EBIT',
    color: '#006699',
    data: []
  }]
};
var tableName = '<?php echo $tableName; ?>'
$.getJSON("../../companies/charts/Data.php", {id: escape(tableName)}, function(json) {
    chartOptions.chart2.xAxis.categories = json[0]['data'];
    chartOptions.chart2.series[0].data = json[4]['data'];
});

$(function() {

    //common options
  Highcharts.setOptions({
    chart: {
      marginRight: 0
    },
    legend: {
      enabled: false
    },
    plotOptions: {
      bar: {
        pointPadding: .01
      },
      column: {
        borderWidth: 0.5
      },
      line: {
        lineWidth: 1
      },
    },
    series: [{
        color: '#027ff7',
    }]
  });
      $('#container').highcharts(chartOptions.chart1);
      chart = $('#container').highcharts();

      $(document).on('click', '.chart-update', function() {
        $('button').removeClass('selected');
            $(this).addClass('selected');
            chart.destroy();
        $('#container').highcharts(chartOptions[$(this).data('chartName')]);
        chart = $('#container').highcharts();
      });
});



</script>
</head>



<body>

    <span class="wrapper">
        <span class="block chart"><div id="container" style="width:400px;height:300px;margin:1.5em 1em;float:left;"></div></span>
        <span class="block buttons">
            <div><button class="chart-update selected" data-chart-name="chart1">CHART1</button></div>
            <div><button class="chart-update" data-chart-name="chart2">CHART2</button></div>

        </span>
    </span>



</body>

</html>

So specifically I am wondering whether of the two segments below are responsible for the problem: 所以我特别想知道以下两个部分是否对这个问题负责:

var tableName = '<?php echo $tableName; ?>'
$.getJSON("../../companies/charts/Data.php", {id: escape(tableName)}, function(json) {
    chartOptions.chart2.xAxis.categories = json[0]['data'];
    chartOptions.chart2.series[0].data = json[4]['data'];
});

or 要么

      $('#container').highcharts(chartOptions.chart1);
      chart = $('#container').highcharts();

      $(document).on('click', '.chart-update', function() {
        $('button').removeClass('selected');
            $(this).addClass('selected');
            chart.destroy();
        $('#container').highcharts(chartOptions[$(this).data('chartName')]);
        chart = $('#container').highcharts();
      });

I tried to switch out the JSON method by using an AJAX method instead with async set to false. 我试图通过使用AJAX方法而不是将async设置为false来切换JSON方法。 This is showed below (didn't work and no data now displays even upon clicking the buttons). 如下所示(不起作用,即使单击按钮也不显示数据)。

var tableName = '<?php echo $tableName; ?>'
$.ajax({
  url: "../../companies/charts/Data.php",
  data: {id: escape(tableName)},
  dataType: "json",
  async: false,
  succes: function(data) {
  chartOptions.chart1.xAxis.categories = json[0]['data'];
  chartOptions.chart1.series[0].data = json[6]['data'];
  }
});

Thanks in advance for any help! 在此先感谢您的帮助!

If you use ajax, I suppose there is no need to set the async property to false. 如果你使用ajax,我想不需要将async属性设置为false。 Also, seems like you have mispelled success on your ajax call. 此外,似乎你在ajax电话上有错误的成功。

Just declare all your highchart initialization when your ajax call had returned success. 当你的ajax调用返回成功时,只需声明所有的高级图初始化。 So basically, ajax method on top of your highchart initialization. 所以基本上,ajax方法在你的高图初始化之上。

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

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