简体   繁体   English

在 highcharts 中检索 JSON 数据

[英]Retrieving JSON data in highcharts

I have been trying to customize an excellent jsfiddle that I was fortunate enough to be directed to in an earlier question here: Switching between many Highcharts using buttons or link text我一直在尝试自定义一个出色的 jsfiddle,我有幸在此处的早期问题中被引导到该 jsfiddle: 使用按钮或链接文本在许多 Highcharts 之间切换

I am very new to javascript programming (and highcharts also) and I am having some difficulties in retrieving my own data from a database.我对 javascript 编程(以及 highcharts 也是如此)非常陌生,并且在从数据库中检索我自己的数据时遇到了一些困难。 Currently I have set up my charts like the following:目前我已经设置了如下图表:

 $('chart1').ready(function() {
        var options = {
            chart: {
                renderTo: 'chart1',
                type: 'column',
                marginTop: 40,
                marginBottom: 75
            },
            legend: {
                enabled: false
            },
            title: {
                text: 'Revenues',
                x: 25 //center
            },

            xAxis: {
                title: {
                    text: ''
                },
                categories: []
            },
            yAxis: {
                showInLegend: false,
                tickAmount: 11,
                endOnTick: false,
                startOnTick: true,
                labels: {
                    formatter: function () {
                    return Highcharts.numberFormat(this.value, 0, '.', ',');
                                }
                },
                title: {
                    text: '<?php echo $unitCurr; ?>'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        this.x +': '+ Highcharts.numberFormat(this.y, 0,'.',',');
                }
            },
            series: []
        }
        var tableName = '<?php echo $tableName; ?>'
        $.getJSON("../../companies/charts/data.php", {id: escape(tableName)}, function(json) {
            options.xAxis.categories = json[0]['data'];
            options.series[0] = json[1];
            chart = new Highcharts.Chart(options);
        });
    });

At the bottom you will notice that I am using JSON to retrieve information from my database and everything works just fine.在底部,您会注意到我正在使用 JSON 从我的数据库中检索信息,并且一切正常。 In my earlier question I was asking about how to switch charts using buttons instead and was directed to the following jsfiddle: http://jsfiddle.net/jlbriggs/7ntyzo6u/在我之前的问题中,我问的是如何使用按钮切换图表,并被定向到以下 jsfiddle: http : //jsfiddle.net/jlbriggs/7ntyzo6u/

This example consists of 3 charts but I have just been trying to manipulate the first chart in order to find out if I could make my own data display instead of the random data that is being generated:此示例包含 3 个图表,但我一直在尝试操作第一个图表,以了解是否可以显示自己的数据而不是正在生成的随机数据:

var chart,
  chartOptions = {},
  chartData = {};

chartData.chart1 = randomData(25);
chartData.chart2 = randomData(10, true);
chartData.chart3 = randomData(65, true, 300);

chartOptions.chart1 = {
  chart: {
    type: 'column'
  },
  title: {
    text: 'Chart 1 Title'
  },
  yAxis: {
    title: {
      text: 'Chart 1<br/>Y Axis'
    }
  },
  series: [{
    name: 'Chart 1 Series',
    data: chartData.chart1
  }]
};

But no matter how much I tried, I just can't seem to change the "data: chartData.chart1" in such a way that it retrieve the arrays I get from my $.getJSON function.但是无论我尝试了多少,我似乎都无法更改“data:chartData.chart1”以检索我从 $.getJSON 函数中获取的数组。 Can any of you help me explain why, for instance, the below code doesn't work?.你们中的任何人都可以帮我解释为什么,例如,下面的代码不起作用?。 Here I try to exchange the ChartData.chart1 array for my database data.在这里,我尝试将 ChartData.chart1 数组交换为我的数据库数据。 I'm not experienced enough to tell whether its the whole random number generation part of the code that prevents it from working or if it's my understanding thats severely lacking.我没有足够的经验来判断是代码的整个随机数生成部分阻止了它的工作,还是我的理解严重缺乏。 (I have made sure that the data from data.php is indeed available, since I can display it in a normal array when I try). (我已经确保 data.php 中的数据确实可用,因为我可以在尝试时将其显示在普通数组中)。

var chart,
  chartOptions = {},
  chartData = {};

chartData.chart2 = randomData(10, true);
chartData.chart3 = randomData(65, true, 300);

$.getJSON("../../companies/charts/data.php", {id: escape(tableName)}, function(json) {
    chartData.chart1 = json[6]['data'];
});

    chartOptions.chart1 = {
      chart: {
        type: 'column'
      },
      title: {
        text: 'Chart 1 Title'
      },
      yAxis: {
        title: {
          text: 'Chart 1<br/>Y Axis'
        }
      },
      series: [{
        name: 'Chart 1 Series',
        data: chartData.chart1
      }]
    };

Any assistance you can provide will be greatly appreciated!您能提供的任何帮助将不胜感激!

You're actually very close to something that will work.你实际上非常接近可行的东西。 Your problem is related to the timing of async calls relative to inline code, and also the way assignments work in javascript.您的问题与异步调用相对于内联代码的时间以及分配在 javascript 中的工作方式有关。

As a quick example, here's some code:作为一个简单的例子,这里有一些代码:

x = {foo:5};
y = x.foo;
x.foo = 9;

At the end of this, x.foo is 9, but y is still 5.最后, x.foo是 9,但y仍然是 5。

Your line of code你的代码行

chartData.chart1 = json[6]['data'];

doesn't execute until after the call to the server completes;直到对服务器的调用完成后才执行; it's contained within a call back function.它包含在回调函数中。 However, this section of code但是,这部分代码

chartOptions.chart1 = {
  chart: {
    type: 'column'
  },
  title: {
    text: 'Chart 1 Title'
  },
  yAxis: {
    title: {
      text: 'Chart 1<br/>Y Axis'
    }
  },
  series: [{
    name: 'Chart 1 Series',
    data: chartData.chart1
  }]
};

executes immediately.立即执行。 See the problem?看到问题了吗? You've cooked the current value of chartData.chart into chartOptions.chart1 BEFORE the server call has completed.在服务器调用完成之前,您已经将chartData.chart的当前值chartData.chartchartOptions.chart1 That's why you're not seeing your data.这就是为什么你没有看到你的数据。

Instead, try something like this:相反,尝试这样的事情:

chartOptions.chart1 = {
  chart: {
    type: 'column'
  },
  title: {
    text: 'Chart 1 Title'
  },
  yAxis: {
    title: {
      text: 'Chart 1<br/>Y Axis'
    }
  },
  series: [{
    name: 'Chart 1 Series',
    data: []
  }]
};

$.getJSON("../../companies/charts/data.php", {id: escape(tableName)}, function(json) {
    chartOptions.chart1.series[0].data = json[6]['data'];
});

Now when your data comes back, you're putting it into the object that is actually being used to render the chart (once you click on the right button).现在,当您的数据返回时,您将其放入实际用于呈现图表的对象中(单击右侧按钮后)。 Keep in mind that it's going to be empty until the server call completes.请记住,在服务器调用完成之前它将是空的。

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

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