我正在try 创建一个基于json数据的条形图,该数据根据过滤器动态变化.

我希望图表显示与该期间相关的类型,如果它存在的话.如果它没有显示任何东西.

我能够得到根据this discussion显示数据的图表,但它只有在数据的所有选项都可用时才起作用,但事实并非如此.

但是,如果句点中缺少某个类型,它会将该类型移回前一段时间段,这是我不想要的.

这里有两个例子

  1. 两个月,有两种类型,但只有3个值(看起来正确)
 [
    {
        "Period": "2023-12",
        "Total": 2238.86,
        "Type": "Drinks"
    },
    {
        "Period": "2023-12",
        "Total": 4672.35,
        "Type": "Food"
    },
    {
        "Period": "2024-01",
        "Total": 1166.5,
        "Type": "Drinks"
    }
]

enter image description here

  1. 三个月,三种类型,4个值(数据从第二个周期移到第一个周期)
[
    {
        "Period": "2023-11",
        "Total": 2782.13,
        "Type": "Other"
    },
    {
        "Period": "2023-12",
        "Total": 2238.86,
        "Type": "Drinks"
    },
    {
        "Period": "2023-12",
        "Total": 4672.35,
        "Type": "Food"
    },
    {
        "Period": "2024-01",
        "Total": 1166.5,
        "Type": "Drinks"
    }
]

data from second period shifted to the first period

我已经找了好几天了,try 了不同的方法,但似乎都不管用.

这是我根据前面提到的主题使用的代码

jQuery.ajax({
    url: "../../ajax/barGraph.php?period=<?= $period; ?>",
    type: "GET",
    dataType: 'json',
    success: function(soldeInfo) {



          let labels = [...new Set(soldeInfo.map((x) => x.Period))];
          let datasets = soldeInfo.reduce(function (acc, cur) {
            let Type = acc.find((x) => x.label == cur.Type);
            if (Type == null) {
              let color = '';

              switch (cur.Type) {
                case 'Food':
                  color = 'red';
                  break;
                case 'Drinks':
                  color = 'green';
                  break;
                default:
                  color ='gray';
              }

              Type = {
                label: cur.Type,
                data: [cur.Total],
                backgroundColor: color,
                borderColor: color,
                borderWidth: 1,
              };

              acc.push(Type);
            } else {
              Type.data.push(cur.Total);
            }

            return acc;
          }, []);

          var soldeInfo = {
            type: 'bar',
            data: {
              labels: labels,
              datasets: datasets,
            },
          };

            const soldeChart = document.getElementById('chartSolde');
            new Chart(soldeChart, soldeInfo);



    }
});

非常感谢:)

推荐答案

每个数据集中的可用值从左到右绘制在自由位置. 因此,每个data数组必须在不存在实值的位置包含空值.

请看一看您修改后的可运行代码,看看如何做到这一点.

const soldeInfo = [
    { "Period": "2023-11", "Total": 2782.13, "Type": "Other" },
    { "Period": "2023-12", "Total": 2238.86, "Type": "Drinks" },
    { "Period": "2023-12", "Total": 4672.35, "Type": "Food" },
    { "Period": "2024-01", "Total": 1166.5, "Type": "Drinks" }
];

const colorOf = (type) => {
  switch (type) {
    case 'Food':
      return 'red';
    case 'Drinks':
      return 'green';
    default:
      return 'gray';
  }
}

let labels = [...new Set(soldeInfo.map((x) => x.Period))];
let datasets = soldeInfo.reduce(function(acc, cur) {
  let type = acc.find((x) => x.label == cur.Type);
  if (type == null) {
    let color = colorOf(cur.Type);
    type = {
      label: cur.Type,
      data: new Array(labels.length).fill(0),
      backgroundColor: color,
      borderColor: color,
      borderWidth: 1,
    };
    acc.push(type);
  }
  type.data[labels.indexOf(cur.Period)] = cur.Total;  
  return acc;
}, []);

new Chart('chartSolde', {
  type: 'bar',
  data: {
    labels: labels,
    datasets: datasets,
  },
});
<script src="https://cdn.jsdelivr.net/npm/chart.js@^4"></script>
<canvas id="chartSolde" height="90"></canvas>

Json相关问答推荐

基于两个条件替换扁平化的SON中的值

使用jolt删除空对象

JOLT转换过滤出特定值/对象

您可以使用Jolt对JSON执行OR条件吗

PowerShell:将Invoke-WebRequest与变量一起使用

使用更高级别架构中的字段值在$def内实现约束

JOLT分裂和数组数据

错误解析错误:意外令牌:在我的 .eslintrc.json 文件中.为什么?

将 GEOSwift.JSON 转换为 Swift 中的 struct

Groovy JsonBuilder 在for循环中添加数组元素

打印与 JSON 和 PowerShell 中的模式匹配的子项的父项名称

如何通过 jolt 将一个对象中的键和值添加到数组中的每个对象中

字典和对象的模型创建问题

N1QL 聚合查询 Couchbase

如何判断 Json 对象中是否存在键并获取其值

如何从 mysql 数据库构建 JSON 数组

IE8 原生 JSON.parse 错误导致堆栈溢出

Jackson 中的 readValue 和 readTree:何时使用哪个?

Json.NET 是否缓存类型的序列化信息?

如何将 MongoDB 查询转换为 JSON?