简体   繁体   English

使用JSON为数据库驱动的图表(highchart.js)获取图表的值

[英]Fetch Values for Chart using JSON for Database driven Chart (highchart.js)

I am creating a simple Database driven Graph using Libraries from Codepen that are : 我正在使用Codepen中的库创建一个简单的数据库驱动图:

THIS and Highchart.js Highchart.js

I have a HTML File that simply displays the Chart, the main Data and processing is done through index.js file that contains the Static Values to draw the Graph. 我有一个仅显示图表的HTML文件,主要数据和处理是通过index.js文件完成的,该文件包含用于绘制图形的静态值。 The file is : 该文件是:

$(function () {
var options = {

    chart: {
        renderTo: 'container'
    },

    subtitle: {
        text: 'Subtitle'
    },

    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
      tickInterval: 4
    },

/***  The following Values are needed to be fetched from DB which are now static */ 

    series: [{
        data: [89.9, 21.5, 16.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]

},
chart = new Highcharts.Chart(options);

$('#redraw').click(function() {
    console.log(chart.xAxis[0]);
    chart.redraw();
});
});

Now I want to create PHP File that will Fetch the Data from the database and will send it to the JS File. 现在,我想创建PHP文件,该文件将从数据库中获取数据并将其发送到JS文件。

 $test=mysql_query("select id from tbl_graph");
 $rowtest=mysql_fetch_array($test);

Now what I want is the data that is fetched from the Database is needed to be send to the Index.js file so that the following static Values in index.js can be connected directly from the database. 现在,我需要从数据库中获取的数据需要发送到Index.js文件,以便可以直接从数据库连接index.js中的以下静态值。

 series: [{
        data: [89.9, 21.5, 16.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]

I would appreciate any kind of help.. 我将不胜感激。

The answer is not so straight forward. 答案不是那么直接。 To put it simple, you need ajax request to grab data from the server. 简单来说,您需要ajax请求来从服务器获取数据。

Here are the step in details 这是详细步骤

  1. You need to create php, that returns json 您需要创建php,返回json

     <?php $return_arr = array(); $fetch = mysql_query("SELECT * FROM tbl_graph"); while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) { $row_array[] = $row['value']; array_push($return_arr,$row_array); } echo json_encode($return_arr); // This will return [4,5,6,7] in json ?> 
  2. Update your script to grab data from php 更新您的脚本以从php获取数据

     $(function () { var options = { chart: { renderTo: 'container' }, subtitle: { text: 'Subtitle' }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], tickInterval: 4 }, /*** The following Values are needed to be fetched from DB which are now static */ series: [{ data: [89.9, 21.5, 16.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }] }, chart = new Highcharts.Chart(options); $('#redraw').click(function() { // whenever you click redraw, it will grab json from php file above $.getJSON('http://yourpath.com/test.php', function (csv) { chart.series[0].setData(csv,true);//then set data and return csv } }); 

    }); });

I haven't tested the code. 我还没有测试代码。 Probably you need to figure it out if there is any syntax error. 可能您需要弄清楚是否存在语法错误。 but hopefully you get sort of idea how to get json from php 但希望您能对如何从php获取json有所了解

In the PHP script, you need to prepare arrays, and convert to the json, by json_encode() function. 在PHP脚本中,您需要准备数组,并通过json_encode()函数转换为json。 Then only what you need is use $.getJSON() in javascript and use your data in highcharts. 然后,仅需要使用javascript中的$ .getJSON()并在highcharts中使用您的数据。

Other solution is inject php in javascript like here: 其他解决方案是在javascript中注入php,如下所示:

http://www.highcharts.com/docs/working-with-data/preprocessing-data-from-a-database/ http://www.highcharts.com/docs/working-with-data/preprocessing-data-from-a-database/

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

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