Ajax 计算每笔交易的总额。(谷歌图表)
我有一个图表,我需要计算随着时间的推移我的收入。在这个图表中,我有金额(红线)。我的目标是计算每一笔交易的总数。我当前的代码工作不正常,因为当时间为18:26:23时,它是1000金额当时间为18:26:24时,它仍然是1000。。。它应该是2000随着时间的推移,它应该可以解决这个问题。我已经在下面提供了我的代码以及我当前系统和目标的屏幕截图。先谢谢你 视图:Ajax 计算每笔交易的总额。(谷歌图表),ajax,codeigniter,charts,Ajax,Codeigniter,Charts,我有一个图表,我需要计算随着时间的推移我的收入。在这个图表中,我有金额(红线)。我的目标是计算每一笔交易的总数。我当前的代码工作不正常,因为当时间为18:26:23时,它是1000金额当时间为18:26:24时,它仍然是1000。。。它应该是2000随着时间的推移,它应该可以解决这个问题。我已经在下面提供了我的代码以及我当前系统和目标的屏幕截图。先谢谢你 视图: <div class="col-md-12"> <!-- LINE
<div class="col-md-12">
<!-- LINE CHART -->
<div class="card card">
<div class="card-header">
<h3 class="card-title">Stats Per Day</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" style="width:30%;">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<div class="chart">
<div id="wholechart" style="min-height: 250px; height: 250px; max-height: 250px; max-width: 100%;"></div>
</div>
</div>
<!-- /.card-body -->
</div>
</div>
function sampleeesasw(){
$.ajax({
type: 'post',
url: '<?=site_url('report/datas')?>',
dataType:'json',
success: function(result) {
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(function(){drawChart(result);});
function drawChart(result) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'req');
data.addColumn('number', 'total');
data.addColumn('number', 'amount');
var dataArray =[];
$.each(result,function(i,obj){
dataArray.push([obj.req,parseInt(obj.total),parseInt(obj.amount)]);
});
data.addRows(dataArray );
var options = {
seriesType: "line",
};
var chart = new google.visualization.ComboChart(document.getElementById('wholechart')).
// Line,Bar,Area,Clomun,pie
draw(data, {curveType: "function",
vAxes: {0: {logScale: false},
1: {logScale: false, maxValue: 2}},
series:{
0:{targetAxisIndex:0},
1:{targetAxisIndex:1},
2:{targetAxisIndex:1}}}
);
}
}
});
}
setInterval(function(){
sampleeesasw()
},1000);
public function datas(){
$data= $this->reports->wholedatachart();
foreach($data as $row){
$data['req']=$row['req'];
$data['amount']=$row['amount'];
$data['total']=$row['total'];
}
echo json_encode($data);
}
型号:
function wholedatachart(){
$query=$this->db->query("SELECT timeProcess as 'req', transID as 'total', amount as 'amount' FROM tbl_transaction");
return $query->result_array();
}
我想你可以试试这个,你需要增加每个数据的数量,你可以使用一个临时变量来增加所有的数量 我编辑了答案对不起我的错 将控制器更改为
public function datas(){
$data= $this->reports->wholedatachart();
$tempAmount = 0;
foreach($data as $row){
$tempAmount = $tempAmount + $row['amount'];
$output[]=array(
'req' => $timeline['time'],
'amount' => $tempAmount,
'amount' => $row['amount'],
);
}
echo json_encode($output);
}
如果您想添加总数,也可以执行相同的操作您可以在
drawChart
函数中执行计算部分。在该函数中,您只需将金额的总值保存在某个变量中,并将其传递给您的dataArray.push(…)
演示代码:
//假设数据如下所示。。
var结果=[{
“请求”:“1”,
“金额”:2000,
“总数”:1 000
},{
“请求”:“2”,
“金额”:1000,
“总数”:1 000
},{
“请求”:“3”,
“金额”:1000,
“总数”:1 000
}]
函数sampleesaw(){
/*$.ajax({
键入:“post”,
url:“”,
数据类型:“json”,
成功:功能(结果){*/
google.charts.load('current'{
“包”:[“核心图表”]
});
google.charts.setOnLoadCallback(函数(){
绘图(结果);
});
功能图(结果){
var data=new google.visualization.DataTable();
data.addColumn('string','req');
data.addColumn('number','total');
data.addColumn('number','amount');
var dataArray=[];
var total=0//intialze。。
$。每个(结果、功能(i、obj){
total+=parseInt(obj.amount)//在每次迭代中添加
dataArray.push([obj.req,parseInt(obj.total),parseInt(total)];//在这里添加值。。
});
data.addRows(dataArray);
变量选项={
序列类型:“行”,
};
var chart=new google.visualization.ComboChart(document.getElementById('wholechart')).draw(数据{
曲线类型:“函数”,
阀门:{
0: {
对数刻度:假
},
1: {
对数标度:假,
最大值:2
}
},
系列:{
0: {
targetAxisIndex:0
},
1: {
目标指数:1
},
2: {
目标指数:1
}
}
});
}
/* }
});*/
}
setInterval(函数(){
SampleeSasw()
}, 1000);代码>
每日统计数据
您能发布您的数据结构吗?这是您的意思吗?我很抱歉。我是新手。谢谢。所以您希望它递增并显示每个新数据的总量(例如18:26:23将显示1000,18:26:24将显示2000)或今天的总量?例如18:26:23将显示1000,18:26:24将显示2000)嗨,事务已经存储在db中,或者它们正在同时插入代码的其他部分,并且在您提取它们的间隔时间内?您好,我尝试了它,但没有任何更改,它仍然是相同的这是我所做的>>>您好,Daffa。我按照你的建议做了,事情就是这样。图形消失了,没有任何变化。如果我不能帮助你,我很抱歉,很高兴你找到了答案,谢谢你的反馈你好,斯瓦蒂。我明天早上一定要试试这个。(因为我现在不在家)我希望你能等我的答复。谢谢,我真的很感激。:P您需要obj.total
或obj.amount
的合计值是多少?因此您需要将obj.amount
的合计值显示在哪个标签的位置?很抱歉让您感到困惑:)在您的屏幕截图中,您还没有更改dataArray.push([obj.req,parseInt(obj.total),parseInt(total)]代码>仔细查看我的代码。我通过了total
作为最后一个参数哇,Swati。非常感谢你。我真的很感激你帮助我的努力。再次非常感谢你。