php 数据从数据库到laravel中的fusioncharts

xjreopfe  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(131)

我想在laravel中显示数据库中的数据到myFusionCharts,但不知何故我卡住了。我无法从文档中找到任何答案,因为他们在样本中使用了预定义的数据集。
如果你能帮忙的话,我将不胜感激。
这是我的控制器:

public function index()
{
    $subj = DB::Table('tbl_subjects')               
            ->select('subject')
            ->get();
            // ->toArray()

    foreach ( $subj as $s){
        // $c = $s->subject;
        $c[] = DB::table('tbl_ordinances')
            ->where('subject', 'like', '%'.$s->subject.'%')
            ->get()
            ->count();

    }     
        // dd($subj,$c);
    return view('statistics',compact('subj','c'));
}

$subj,$c);“并返回以下值:

Collection {#302 ▼
#items: array:9 [▼
0 => {#309 ▼
  +"subject": "Peace & Order and Public Safety"
}
1 => {#311 ▼
  +"subject": "Infrastructure"
}
2 => {#312 ▼
  +"subject": "Livelihood/ Poverty Reduction"
}
3 => {#313 ▼
  +"subject": "Finance/ Investment"
}
4 => {#314 ▼
  +"subject": "Administrative Development"
}
5 => {#315 ▼
  +"subject": "Agricultural Development"
}
6 => {#316 ▼
  +"subject": "Social Services"
}
7 => {#317 ▼
  +"subject": "Environment and Disaster Management"
}
8 => {#318 ▼
  +"subject": "Tourism"
}
]
}
array:9 [▼
0 => 2
1 => 0
2 => 0
3 => 0
4 => 4
5 => 0
6 => 3
7 => 6
8 => 1
]

这是我的剑:

<script type="text/javascript">
FusionCharts.ready(function() {
    var revenueChart = new FusionCharts({
type: 'pie2d',
renderAt: 'chart-container',
width: '500',
height: '500',
dataFormat: 'json',
dataSource: {
  "chart": {
    "caption": "Municipal Ordinances by subject categories",
    // "subCaption": "Last year",
    "numberPrefix": null,
    "showPercentInTooltip": "1",
    "decimals": "1",        
    "theme": "fusion",
  },
  "data": [  // I want THOSE VALUES HERE... but I don't know how..
    {"label": "h","value": "25" },   
    {"label": "h2","value": "25"},    
    {"label": "h3","value": "55"},  
  ]

}
}).render();
});
</script>

<div id="chart-container">FusionCharts will render here..</div>
voj3qocg

voj3qocg1#

您可以尝试使用以下文件来实现您的案例-

<?php
    //address of the server where db is installed
    $servername = "localhost";
    //username to connect to the db
    //the default value is root
    $username = "root";
    //password to connect to the db
    //this is the value you specified during installation of WAMP stack
    $password = "password";
    //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);
    }
?>

文件参考-https://www.fusioncharts.com/blog/charts-laravel-web-application/
https://www.fusioncharts.com/dev/using-with-server-side-languages/tutorials/php-mysql-charts

相关问题