我是整个开发领域的新手。您能否在以下情况下帮助我确定错误。我在学校项目中使用FusionCharts,发现很难填充图表。我已经基于提供的教程创建了文件,但是包含了MYSQL元素来检索数据。文件如下,

HTML ...

    <!DOCTYPE html>
    <html>
    <head>
     <title>Fusion Charts Column 2D Sample</title>
</head>
<body>
  <div id="chart-container">FusionCharts will render here</div>
  <script src="js/jquery-2.1.4.js"></script>
  <script src="js/fusioncharts.js"></script>
  <script src="js/fusioncharts.charts.js"></script>
  <script src="js/themes/fusioncharts.theme.zune.js"></script>
  <script src="js/app1.js"></script>
</body>
</html>


PHP ...

<?php
//address of the server where db is installed
$servername = "localhost";

//username to connect to the db
//the default value is root
$username = "xyz";

//password to connect to the db
//this is the value you would have specified during installation of WAMP stack
$password = "123";

//name of the db under which the table is created
$dbName = "test";

//establishing the connection to the db.
$conn = new mysqli($servername, $username, $password, $dbName);

//checking if there were any error during the last connection attempt
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

//the SQL query to be executed
$query = ("SELECT month, actuallikes, projected, profit FROM avp");

//storing the result of the executed query
$result = $conn->query($query);

//initialize the array to store the processed data
$jsonArray = array();

//check if there is any data returned by the SQL Query
if ($result->num_rows > 0) {
  //Converting the results into an associative array
  while($row = $result->fetch_assoc()) {
    $jsonArrayItem = array();
    $jsonArrayItem['lable'] = $row['month'];
    $jsonArrayItem['value'] = $row['actuallikes'];
    $jsonArrayItem['value1'] = $row['projected'];
    $jsonArrayItem['value2'] = $row['profit'];
    //append the above created object into the main array.
    array_push($jsonArray, $jsonArrayItem);
  }
}

//Closing the connection to DB
$conn->close();

//set the response content type as JSON
header('Content-type: application/json');
//output the return value of json encode using the echo function.
echo json_encode($jsonArray);
?>


JavaScript ...

var lables = [], values = [], value1s = [], value2s = [];
function getusers() {

    $.ajax({
            type: 'GET',
            async: false,
            url: "http://localhost/avp/chart_data.php",
            data: {},
            dataType: "json",
            success: function(result){
                if(result){
                    for(var i = 0 ; i < result.length; i++){
                        lables.push(result[i].lable);
                        values.push(result[i].value);
                        value1s.push(result[i].value1);
                        value2s.push(result[i].value2);
                    }
                }
            },
            error: function(errmsg) {
                alert("Ajax??????????!"+ errmsg);
            }
        });
    return lables, values, value1s, value2s;
    }

    getusers();

 salesAnlysisChart = new FusionCharts({
    type: 'mscombi2d',
    renderAt: 'chart-container',
    width: '600',
    height: '300',
    dataFormat: 'json',
    dataSource: {
      "chart": {
        "caption": "Harry's SuperMart",
        "subCaption": "Sales analysis of last year",
        "xAxisname": "Month",
        "yAxisName": "Amount (In USD)",
        "numberPrefix": "$",
        "showBorder": "0",
        "showValues": "0",
        "paletteColors": "#0075c2,#1aaf5d,#f2c500",
        "bgColor": "#ffffff",
        "showCanvasBorder": "0",
        "canvasBgColor": "#ffffff",
        "captionFontSize": "14",
        "subcaptionFontSize": "14",
        "subcaptionFontBold": "0",
        "divlineColor": "#999999",
        "divLineIsDashed": "1",
        "divLineDashLen": "1",
        "divLineGapLen": "1",
        "showAlternateHGridColor": "0",
        "usePlotGradientColor": "0",
        "toolTipColor": "#ffffff",
        "toolTipBorderThickness": "0",
        "toolTipBgColor": "#000000",
        "toolTipBgAlpha": "80",
        "toolTipBorderRadius": "2",
        "toolTipPadding": "5",
        "legendBgColor": "#ffffff",
        "legendBorderAlpha": '0',
        "legendShadow": '0',
        "legendItemFontSize": '10',
        "legendItemFontColor": '#666666'
      },
      "categories": [{
        "category": lables
      }],
      "dataset": [{
        "seriesName": "Actual Revenue",
        "showValues": "1",
        "data": values
      }, {
        "seriesName": "Projected Revenue",
        "renderAs": "line",
        "data": value1s
      }, {
        "seriesName": "Profit",
        "renderAs": "area",
        "data": value2s
      }]
    }
  });
    salesAnlysisChart.render(); }
});
});


希望有人可以帮助我发现错误。抱歉,如果我做任何愚蠢的事情。

干杯!

最佳答案

您创建的用于渲染图表的JSON结构不正确,例如因为“类别”应该具有一个对象数组作为值,但是您要向其传递值数组,因此数据集中的数据键也是如此。您需要修改代码,或者也可以使用我的code

关于javascript - FusionCharts不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42536026/

10-09 03:29