[英]How to display multiple datasets by month in highchart?
I have a database called sales which I'm trying to display in my chart like this but can't.我有一个名为 sales 的数据库,我试图像这样在我的图表中显示但不能。
id ID | products产品 | earnings收益 | created_at创建时间 |
---|---|---|---|
1 1个 | Jasmine Jasmine | 50 50 | 2023-01-14 18:55:34 2023-01-14 18:55:34 |
2 2个 | How to cook怎么做菜 | 150 150 | 2023-01-14 18:55:34 2023-01-14 18:55:34 |
3 3个 | Jasmine Jasmine | 100 100 | 2023-01-14 18:55:34 2023-01-14 18:55:34 |
4 4个 | Dictionary字典 | 200 200 | 2023-01-14 18:55:34 2023-01-14 18:55:34 |
5 5个 | How to cook怎么做菜 | 70 70 | 2023-02-20 17:23:54 2023-02-20 17:23:54 |
It is my first time trying to do it in chart and tried other codes from tutorials and ended up with this result这是我第一次尝试在图表中执行此操作并尝试了教程中的其他代码并最终得到了这个结果
I'm trying to display it like the first chart, but instead by month with multiple datasets where the month will contain the name of the products and its earning on that month.我试图像第一个图表一样显示它,但不是按月显示多个数据集,其中月份将包含产品名称及其当月的收入。 like this像这样
Pardon my unsightly editing请原谅我难看的编辑
This is the working code that I managed to follow and do, but don't know how to modify it to obtain the desired output. I tried but gets an error, so I put it back.这是我设法遵循并执行的工作代码,但不知道如何修改它以获得所需的 output。我试过但出现错误,所以我把它放回去了。
$now = Carbon::now();
$yr = $now->format('Y/m/d');
//monthly
{
$m= $now->format('m');
$date_start = Carbon::create(date('Y'), "1");//january start
$data=[];
$monthdata=[];
$x=0;
for ($month = $m; $x <= 7; $month++)
{
$x=$x+1;
$date = Carbon::create(date('Y'), $month);
$date_end = $date->copy()->endOfMonth();
$saledate = DB::table('sales')
->select(DB::raw('sum(earnings) as sale'))
->where('created_at', '>=', $date)
->where('created_at', '<=', $date_end)
->get();
$sum=0;
foreach ($saledate as $row)
{
$sum=$row->sale;
}
array_push($data,$sum);
array_push($monthdata,$date->format('M'));
}
$saleChart1= new UserChart;
$saleChart1->labels($monthdata);
$saleChart1->dataset('Sales', 'bar',$data )->options([
'color' => '#2596be',
]);
}
Inside my blade file,在我的刀片文件中,
<div class="col-lg-6 mb-5">
<div class="card card card-raised h-100 ">
<div class="card-header text-dark bg-success px-4" style="background-color:#198754 !important">
<i class="fas fa-chart-area me-1 text-white"></i>
Monthly Sales
</div>
<div class="card-body bg-white">
<div class="col-lg-12 " >
<div id="myAreaChart1" class="mt-4" style="height: 300px;">
{!! $saleChart1->container() !!}
<script src=https://cdnjs.cloudflare.com/ajax/libs/echarts/4.0.2/echarts-en.min.js charset=utf-8></script>
{!! $saleChart1->script() !!}
</div>
</div>
</div>
</div>
</div>
My UserChart class,我的用户图表 class,
<?php
namespace App\Charts;
use ConsoleTVs\Charts\Classes\Chartjs\Chart;
class UserChart extends Chart
{
/**
* Initializes the chart.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
}
Please help, can't find other tutorials, all I found now are pie charts and line graphs and doesn't display similar to what I'm looking for.请帮忙,找不到其他教程,我现在找到的都是饼图和折线图,显示方式与我要找的不一样。 What should I do to achieve the desired output?我应该怎么做才能达到所需的 output?
It doesn't seem like ConsoleTVs\Charts\Classes\Chartjs\Chart
has that capability.似乎ConsoleTVs\Charts\Classes\Chartjs\Chart
没有这种能力。 However, you might be able to get ConsoleTVs\Charts\Classes\Echarts\Chart
to work closer to what you'd need.但是,您可以让ConsoleTVs\Charts\Classes\Echarts\Chart
更接近您的需要。
If I were you I'd change your UserChart class to如果我是你,我会将你的 UserChart class 更改为
<?php
namespace App\Charts;
use ConsoleTVs\Charts\Classes\Echart\Chart;
class UserChart extends Chart
{
/**
* Initializes the chart.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
}
Then using example from https://echarts.apache.org/examples/en/editor.html?c=bar-label-rotation you should be able to mold your data into the correct format.然后使用https://echarts.apache.org/examples/en/editor.html?c=bar-label-rotation中的示例,您应该能够将数据塑造成正确的格式。
It seems as though most of the setup is pushed into an option
array.似乎大部分设置都被推入了一个option
数组。 Some of which you can see on the class itself https://github.com/ConsoleTVs/Charts/blob/main/src/Classes/Echarts/Chart.php其中一些你可以在 class 本身看到https://github.com/ConsoleTVs/Charts/blob/main/src/Classes/Echarts/Chart.php
You should be able to set the options with $saleChart1->options([//...]);
您应该能够使用$saleChart1->options([//...]);
设置选项
Happy coding:-)快乐编码:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.