duanpin2034 2016-04-05 15:25
浏览 43
已采纳

使用PHP在Google Line图表中使用浮点值

Line chart is drawn very fine using (int) values using Google Charts API (see exact below code) but one of the percentage column value is in float so I tried (floatval) instead of (int) but PHP server file throws 500 internal server error.

What is the solution in especially at to use float value?

(int)str_replace("%", "", $row['percentage'])

Also note that, my values are coming as JSON from MySQL.

PHP (server file):

   $rows[] = array(explode(" - ", $row['started_on'])[0].trim(), 'Coverage');
        while($row =  mysqli_fetch_array($result)) {
            $rows[] = array(explode(" - ", $row['started_on'])[0].trim(), (int)str_replace("%", "", $row['percentage']));
        }

JS (in php):

// Draw line chart
function drawLineChart(chartType, chartTitle) {
    google.charts.setOnLoadCallback(lineChartData);

    function lineChartData() {
        var lineChartJsonData = $.ajax({
            type: 'GET',
            url: "<?php echo $general_scripts; ?>",
            data: { id1: chartType, id2: "Chart", id100: "<?php echo $getPage; ?>" },
            dataType:"json",
            async: false,
            beforeSend: function() {
                $("#progress").show();
            },
            success: function(data) {
                $("#progress").hide();
            },
        }).responseText;

        var options = {
            title: chartTitle,
            width: '390',
            height: '300',
            backgroundColor: '#f5fffa',
        };

        var data = new google.visualization.arrayToDataTable(($.parseJSON(lineChartJsonData)));
        var chart = new google.visualization.LineChart(document.getElementById(chartType));
        chart.draw(data, options);
    }
}
  • 写回答

1条回答 默认 最新

  • dongpeihui1051 2016-08-09 09:14
    关注

    (float) works fine, here it is:

    $rows[] = array(explode(" - ", $row['started_on'])[0].trim(), (float)str_replace("%", "", $row['data']));
    
    echo json_encode($rows);
    

    Give this data to chart.draw(data, options);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动