- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个名为收入和支出的表,每个表也都有一个货币类型。
我正在查询每种货币每月产生的收入和支出。
查询正常,它返回:
[
{"type":"income","monto":"1000","currency":"1","month":"6","year":"2016","idcurrency":"1","name":"Pesos argentinos","simbolo":"$"},
{"type":"expenditure","monto":"-500","currency":"1","month":"6","year":"2016","idcurrency":"1","name":"Pesos argentinos","simbolo":"$"},
{"type":"income","monto":"5000","currency":"2","month":"6","year":"2016","idcurrency":"2","name":"Dolares estadounidenses","simbolo":"u$d"},
{"type":"expenditure","monto":"-5000","currency":"2","month":"6","year":"2016","idcurrency":"2","name":"Dolares estadounidenses","simbolo":"u$d"},
{"type":"expenditure","monto":"-5000","currency":"3","month":"6","year":"2016","idcurrency":"3","name":"Pesos uruguayos","simbolo":"$"}
]
我需要使用图表js绘制它,但问题是根据图表js的文档,我需要设置标签以及每个标签的值。可能会发生这样的情况:在任何一个月,某种货币没有任何变动,而另一种货币却发生了变动,因此数据将显示错误。我该如何解决这个问题?
谢谢
编辑:我尝试使用条形图,对于标签,我实际上使用它来生成它们:
$desde = new DateTime();
$desde->modify('first day of this month');
$desde = $desde->sub(new DateInterval('P1Y'));
$hasta = new DateTime();
$hasta->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($desde, $interval, $hasta);
$info = array();
foreach ($period as $dt) {
array_push($info,$dt->format("Y-m"));
}
帕特里克解决方案,但我无法让它工作:
// Bar chart
var ctx = document.getElementById("graficoIngresosCostos");
let dataSource =
<?=$dataSource?>;
//build the labels (you already did it in your OP. This is your info array
// that holds the first day of each month)
let labels = <?=$labels?>;
let currencies = <?=$monedas?>;
let datasets = [];
currencies.forEach(c => {
//filter out the incomes and expenditures for just the currency
// you're interested in
let incomeSource = dataSource.filter(
source=>source.nombre===c && source.concepto==='ingresos');
let expenseSource = dataSource.filter(
source=>source.nombre===c && source.concepto==='gastos');
let incomeDataset = { //dataset that holds this currency's income
label:c + ': ingresos',
data:[],
backgroundColor:'#03586A' //set color of bars
};
let expenseDataset = {//dataset that holds this currency's expense
label:c + ': gastos',
data:[],
backgroundColor:'#03586A' //set color of bars
};
//add datapoints to income and expense datasets for this currency
incomeSource.forEach(source=>{incomeDataset.data.push(source.monto)});
expenseSource.forEach(source=>{expenseDataset.data.push(source.monto)});
//todo: set backgroundColor for the bars of this dataset
//add the datasets to the chart
datasets.push(incomeDataset,expenseDataset);
});
//at this point you have all the datasets organized. Build chart
//ctx refers to the 2dContext of the canvas
let chart = new Chart(ctx,{
type:'bar',
data:{labels:labels, datasets:datasets}
});
最佳答案
您可能应该为每种货币拥有一个数据集。所有数据集共享相同的标签数组,但每个数据集都有自己的数据点。因此基本策略是,循环遍历每种货币,将该货币的所有数据点构建到数据集中,并将其添加到 datasets
数组中。
let dataSource = [
{"type":"income","monto":"1000","currency":"1","month":"6","year":"2016","idcurrency":"1","name":"Pesos argentinos","simbolo":"$"},
{"type":"expenditure","monto":"-500","currency":"1","month":"6","year":"2016","idcurrency":"1","name":"Pesos argentinos","simbolo":"$"},
{"type":"income","monto":"5000","currency":"2","month":"6","year":"2016","idcurrency":"2","name":"Dolares estadounidenses","simbolo":"u$d"},
{"type":"expenditure","monto":"-5000","currency":"2","month":"6","year":"2016","idcurrency":"2","name":"Dolares estadounidenses","simbolo":"u$d"},
{"type":"expenditure","monto":"-5000","currency":"3","month":"6","year":"2016","idcurrency":"3","name":"Pesos uruguayos","simbolo":"$"}
];
//build the labels (you already did it in your OP. This is your info array
// that holds the first day of each month)
let labels = [/*info*/];
//if you want to build this dynamically, loop through dataSource and
//push the currency (be sure to check for duplicates) to the array
let currencies = ['Dolares estadounidenses','Pesos uruguayos','Pesos argentinos'];
let datasets = [];
currencies.forEach(c => {
//filter out the incomes and expenditures for just the currency
// you're interested in
let incomeSource = dataSource.filter(
source=>source.name===c && source.type==='income');
let expenseSource = dataSource.filter(
source=>source.name===c && source.type==='expenditure');
let incomeDataset = { //dataset that holds this currency's income
label:c + ': income',
data:[],
backgroundColor:'' //set color of bars
};
let expenseDataset = {//dataset that holds this currency's expense
label:c + ': expenditures',
data:[],
backgroundColor:'' //set color of bars
};
//add datapoints to income and expense datasets for this currency
incomeSource.forEach(source=>{incomeDataset.data.push(source.monto)});
expenseSource.forEach(source=>{expenseDataset.data.push(source.monto)});
//todo: set backgroundColor for the bars of this dataset
//add the datasets to the chart
datasets.push(incomeDataset,expenseDataset);
});
//at this point you have all the datasets organized. Build chart
//ctx refers to the 2dContext of the canvas
let chart = new Chart(ctx,{
type:'bar',
data:{labels:labels, datasets:datasets}
});
说实话,你的图表会非常拥挤,因为如果你只有 4 种货币,那么你将有 8 个数据集。这意味着每个 x 轴值最多可以有 8 个条形。您可能需要考虑制作几张图表(一张用于收入,另一张用于支出)。
您可以找到具有多个数据集的条形图的良好示例 here .
附录1
如果您的某些数据集存在漏洞(几个月没有数据),您将需要做更多工作。 Chart.js
无法知道漏洞在哪里,因为它只能看到一组数字。因此,在构建数据集之前,您需要填充dataSource
,以便所有货币的收入和支出都具有与标签相同数量的数据值。逻辑如下:
foreach(labels)
//extract month and year that this label is responsible for
month=...; year=...;
foreach(currencies as c)
//capture the data for the current month and currency
currencyData = dataSource.filter(
source => source.name===c && source.month===month
&& source.year===year);
//separate into income/expense data for this month
incomeDatapoints = currencyData.filter(
source=>source.type==='income');
exprenseDatapoints = dataSource.filter(
source=>source.type==='expenditure');
//if income data is missing, add a null datapoint the Chart.js
//will keep data and labels in alignment
if(!incomeDatapoints.length)
dataSource.push({
type:'income',
monto: 0, //or maybe null
currency:c,
month:month,
year:year
});
end if
//repeat the if block above with expanseDatapoints
endforeach(currencies)
end foreach(labels)
此时,每个数据集都应该为标签中的每个项目都有一个值最后要做的是将数据源按年排序,然后按月排序以保证当您将值添加到图表时,它们的顺序将与标签。我将让您了解如何根据对象属性对对象数组进行排序,但是 this is where I learned it我自己。
关于javascript - Charts js php 实现按多货币时间戳分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37934212/
您好,我正在处理 BIRT 报告。我有一个查询,我必须对父级的重复数据进行分组,但子级也不能分组! 在我的查询中: item 是父项,item_ledger_entry 是子项。我有来自 item.N
我正在使用 GA API。 这是针对 MCF 目标报告(底部)的标准目标完成指标表(顶部) 看一下这个: 总数加起来 (12,238),但看看按 channel 分组的分割有多么不同!我以为这些会很接
我正在开发一个流量计数器,我想获得 IP 和重复计数,但是如何? 就像是 :select ip, count(ip) from Redirect 返回 : null total ip count 重定
我尝试编写一个正则表达式来匹配条件表达式,例如: a!=2 1+2=2+a 我尝试提取运算符。我当前的正则表达式是“.+([!=<>]+).+” 但问题是匹配器总是尝试匹配组中可能的最短字符串
在 MS Transact SQL 中,假设我有一个这样的表(订单): Order Date Order Total Customer # 09/30/2008 8
我想按 m.ID 分组,并对每个 m.id 求和 (pm.amount_construction* prod.anzahl) 实际上我有以下结果: Meterial_id | amount_const
我想根据多列中的值对值进行分组。这是一个例子: 我想得到输出: {{-30,-50,20},{-20,30,60},{-30,NULL or other value, 20}} 我设法到达: SELE
我正在尝试找出运行此查询的最佳方式。我基本上需要返回在我们的系统中只下了一个订单的客户的“登录”字段列表(登录字段基本上是客户 ID/ key )。 我们系统的一些背景...... 客户在同一日期下的
给定以下mysql结果集: id code name importance '1234', 'ID-CS-B', 'Chocolate Sauce'
大家好,我的数据框中有以下列: LC_REF 1 DT 16 2C 2 DT 16 2C 3 DT 16 2C 1 DT 16 3C 6 DT 16 3C 3
我有这样的 mongoDB 集合 { "_id" : "EkKTRrpH4FY9AuRLj", "stage" : 10, }, { "_id" : "EkKTRrpH4FY9
假设我有一组数据对,其中 index 0 是值,index 1 是类型: input = [ ('11013331', 'KAT'), ('9085267',
java中用stream进行去重,排序,分组 一、distinct 1. 八大基本数据类型 List collect = ListUtil.of(1, 2, 3, 1, 2).stream().fil
基本上,我从 TABLE_A 中的这个开始 France - 100 France - 200 France - 300 Mexico - 50 Mexico - 50 Mexico - 56 Pol
我希望这个正则表达式 ([A-Z]+)$ 将选择此示例中的最后一次出现: AB.012.00.022ABC-1 AB.013.00.022AB-1 AB.014.00.022ABAB-1 但我没有匹配
我创建了一个数据透视表,但数据没有组合在一起。 任何人都可以帮助我获得所需的格式吗? 我为获取数据透视表而编写的查询: DECLARE @cols AS NVARCHAR(MAX), -- f
我想按时间段(月,周,日,小时,...)选择计数和分组。例如,我想选择行数并将它们按 24 小时分组。 我的表创建如下。日期是时间戳。 CREATE TABLE MSG ( MSG_ID dec
在 SQL Server 2005 中,我有一个包含如下数据的表: WTN------------Date 555-111-1212 2009-01-01 555-111-1212 2009-
题 假设我有 k 个标量列,如果它们沿着每列彼此在一定距离内,我想对它们进行分组。 假设简单 k 是 2 并且它们是我唯一的列。 pd.DataFrame(list(zip(sorted(choice
问题 在以下数据框中 df : import random import pandas as pd random.seed(999) sz = 50 qty = {'one': 1, 'two': 2
我是一名优秀的程序员,十分优秀!