过滤数据的信号在 Vega 中无法正常工作

问题描述 投票:0回答:1

我有一个可视化,表示每月事故的累积总数,即发生的事故与已解决(关闭)的事故之间的差异。我从发生的每项索赔的一些数据开始(ID 号、进入日期、截止日期和提出索赔的客户)

我正在尝试放置一个由客户端过滤数据的信号,但它无法正常工作。

当我选择“Baukost”客户端时,会出现问题,它可以很好地连接点,但是当我再次选择“all”时,它会错误地连接线。

在第一张照片中,我展示了默认情况下的显示(全部)

在第二张照片中,我展示了当我通过“Baukost”客户端过滤时出现的可视化效果。

第三张图是我重新选择“全部”时出现的问题,应该和第一张图一样

我在这里放置了我已经完成的代码,请您帮助我看看我失败了。提前非常感谢您。

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",

 
 
 "width": 800,
  "height": 300,
 "padding": 10,


 "signals": [


    { "name": "Customer", "value": "All",
      "bind": {
        "input": "select",
        "name": "Customers",
        "options": [
          "All",
          "Baukost",
          "CatalanaOcc",
         ],
      }
    },

 ],

 "data": [

   {"name": "Accidents_source_load",
   "values": [
    {
        "Number" : "0691328/AF21",
        "EntryDay" : 1,
        "EntryMonth" : 7,
        "EntryYear" : 2021,
        "CloseDay" : 2,
        "CloseMonth" : 8,
        "CloseYear" : 2021,
        "Customer" : "Baukost",
    },
    {
        "Number" : "9989037919",        
        "EntryDay" : 16,
        "EntryMonth" : 7,
        "EntryYear" : 2021,
        "CloseDay" : 25,
        "CloseMonth" : 8,
        "CloseYear" : 2021,
        "Customer" : "Baukost",
    },



    {
        "Number" : "3157418 ",
        "EntryDay" : 24,
        "EntryMonth" : 2,
        "EntryYear" : 2022,
        "CloseDay" : null,
        "CloseMonth" : null,
        "CloseYear" : null,
        "Customer" : "Baukost",
    },
    {
        "Number" : "84032071",
        "EntryDay" : 24,
        "EntryMonth" : 2,
        "EntryYear" : 2022,
        "CloseDay" : null,
        "CloseMonth" : null,
        "CloseYear" : null,
        "Customer" : "Baukost",
    },
    {
        "Number" : "27800304",
        "EntryDay" : 27,
        "EntryMonth" : 5,
        "EntryYear" : 2021,
        "CloseDay" : 2,
        "CloseMonth" : 6,
        "CloseYear" : 2021,
        "Customer" : "CatalanaOcc",
    },
    {
        "Number" : "27802536",
        "EntryDay" : 27,
        "EntryMonth" : 5,
        "EntryYear" : 2021,
        "CloseDay" : 28,
        "CloseMonth" : 5,
        "CloseYear" : 2021,
        "Customer" : "CatalanaOcc",
    },
    {
        "Number" : "27808817",
        "EntryDay" : 28,
        "EntryMonth" : 5,
        "EntryYear" : 2021,
        "CloseDay" : 2,
        "CloseMonth" : 6,
        "CloseYear" : 2021,
        "Customer" : "CatalanaOcc",
    },
    {
        "Number" : "27821933","EntryDay" : 1,
        "EntryMonth" : 6,
        "EntryYear" : 2021,"CloseDay" : null,
        "CloseMonth" : null,
        "CloseYear" : null,"Customer" : "CatalanaOcc",
    },
    
]


   },



   { "name": "Accidents",
     "source": "Accidents_source_load",
     "transform": [

       
       { "type": "formula",
         "expr": "datetime(datum.EntryYear, datum.EntryMonth-1,datum.EntryDay)",
         "as":"EntryDate"
       },
   


       { "type": "formula",
         "expr": "if(datum.CloseMonth==null, null, datetime(datum.CloseYear, datum.CloseMonth-1,datum.CloseDay))",
         "as":"CloseDate"
       },
     


       {
         "type": "formula",
         "expr": "if(datum.CloseMonth==null,'Entry Cabinet', 'Closed Cabinet')",
         "as":"Type"
       },


       { "type": "formula",
          "expr": "if(Customer=='All'||datum.Customer==Customer,1,0)",
          "as": "cond_Customer"
       },
       
       { "type": "filter",
         "expr": "datum.cond_Customer==1"
       },
       

       { "type": "collect",
         "sort":{
           "field":["EntryDate"],
           "order": ["ascending"]
         }
       },

      ]
   },


   { "name": "Close_table",
     "source": "Accidents",
     "transform": [

        {
          "type": "filter",
          "expr": "datum.Type=='Closed Cabinet'"
        },

        {
          "type": "formula",
          "expr": "datum.CloseDate",
          "as": "Date"
        },

        {
          "type": "aggregate",
          "groupby":["Date"],
          "fields":["Number"],
          "ops":["count"],
          "as":["Total"]
        },
         {
           "type": "collect",
           "sort": 
            {
             "field": ["Date"],
             "order": ["ascending"]
            }
          },



      {
       "type": "window",
       "sort": {"field": "Date", "order": "ascending"},
       "ops": ["sum"],
       "fields": [ "Total"],
       "as": [ "Cumulative"]
      },
      {
        "type": "formula",
        "expr": "datum.Cumulative*(-1)",
        "as": "Cumulative"
      },
       { "type": "collect",
         "sort":{
           "field":["Date"],
           "order": ["ascending"]
         }
       },

     ]
   },


   { "name": "Entry_table",
     "source": "Accidents",
     "transform": [

       {
          "type": "formula",
          "expr": "datum.EntryDate",
          "as": "Date"
        },


        
        {
          "type": "aggregate",
          "groupby":["Date"],
          "fields":["Number"],
          "ops":["count"],
          "as":["Total"]
        },


         {
           "type": "collect",
           "sort": 
            {
             "field": ["Date"],
             "order": ["ascending"]
            }
          }, 




      {
       "type": "window",
       "ops": ["sum"],
       "fields": [ "Total"],
       "as": [ "Cumulative"]
      },
       { "type": "collect",
         "sort":{
           "field":["Date"],
           "order": ["ascending"]
         }
       },               
 
     ]
   },   



   { "name": "Entry_Close",
     "source":["Entry_table","Close_table"],
     "transform": [
       { "type": "collect",
         "sort":{
           "field":["Date"],
           "order": ["ascending"]
         }
       },
      { "type": "aggregate",
        "groupby": ["Date"],
        "fields": ["Cumulative"],
        "ops": ["sum"],
        "as": ["Cumulative_Total"]
      }, 

      { "type": "collect",
        "sort":{
          "field":["Date"],
          "order": ["ascending"]
         }
      },      
     ]
   },


 ],


 "scales": [
       { "name": "yscale",
         "type": "linear",
         "domain": {"data": "Entry_Close", "field": "Cumulative_Total"},
         "range": "height"
       },
    
       {
         "name": "xscale",
         "type": "time",
         "domain": {"data": "Entry_Close", "field":"Date","sort": true},
         "range": "width",
       },

    ],

"axes": [
    { "orient": "bottom", "scale": "xscale" },
    { "orient": "left", "scale": "yscale" }
    
      ],

"marks": [ 
   {
          "type": "line",
          "from": {"data": "Entry_Close"},
          "encode": {
            
            "update": {
              "x": {"scale": "xscale", "field": "Date"},
              "y": {"scale": "yscale", "field": "Cumulative_Total"},
              "stroke": {"value": "blue"},
              
              "strokeWidth": {"value": 4},
              "interpolate": {"value": "linear"}, 
               "strokeOpacity": {"value": 1},

            },
            "hover": {
              "strokeOpacity": {"value": 0.5}
            }
          }
        },      

        { "type": "symbol",
          "style": ["point"],
          "from": {"data": "Entry_Close"},
          "encode": {
           "update": {
              "x": {"scale": "xscale", "field": "Date"},
              "y": {"scale": "yscale", "field": "Cumulative_Total"},
             "size": {"value": 60},
             "fill": {"value": "green"},
             "cursor": { "value": "pointer" },
              "tooltip": {"signal": "{'title': 'Cumulative', 'Date': datum.Date,'Total': datum.Cumulative_Total}"}
            }
          }
        },  
       

    ],  










} ```

json charts visualization vega
1个回答
0
投票

您需要为您的标记添加排序字段。这应该对你有用。

编辑器

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 800,
  "height": 300,
  "padding": 10,
  "signals": [
    {
      "name": "Customer",
      "value": "All",
      "bind": {
        "input": "select",
        "name": "Customers",
        "options": ["All", "Baukost", "CatalanaOcc"]
      }
    }
  ],
  "data": [
    {
      "name": "Accidents_source_load",
      "values": [
        {
          "Number": "0691328/AF21",
          "EntryDay": 1,
          "EntryMonth": 7,
          "EntryYear": 2021,
          "CloseDay": 2,
          "CloseMonth": 8,
          "CloseYear": 2021,
          "Customer": "Baukost"
        },
        {
          "Number": "9989037919",
          "EntryDay": 16,
          "EntryMonth": 7,
          "EntryYear": 2021,
          "CloseDay": 25,
          "CloseMonth": 8,
          "CloseYear": 2021,
          "Customer": "Baukost"
        },
        {
          "Number": "3157418 ",
          "EntryDay": 24,
          "EntryMonth": 2,
          "EntryYear": 2022,
          "CloseDay": null,
          "CloseMonth": null,
          "CloseYear": null,
          "Customer": "Baukost"
        },
        {
          "Number": "84032071",
          "EntryDay": 24,
          "EntryMonth": 2,
          "EntryYear": 2022,
          "CloseDay": null,
          "CloseMonth": null,
          "CloseYear": null,
          "Customer": "Baukost"
        },
        {
          "Number": "27800304",
          "EntryDay": 27,
          "EntryMonth": 5,
          "EntryYear": 2021,
          "CloseDay": 2,
          "CloseMonth": 6,
          "CloseYear": 2021,
          "Customer": "CatalanaOcc"
        },
        {
          "Number": "27802536",
          "EntryDay": 27,
          "EntryMonth": 5,
          "EntryYear": 2021,
          "CloseDay": 28,
          "CloseMonth": 5,
          "CloseYear": 2021,
          "Customer": "CatalanaOcc"
        },
        {
          "Number": "27808817",
          "EntryDay": 28,
          "EntryMonth": 5,
          "EntryYear": 2021,
          "CloseDay": 2,
          "CloseMonth": 6,
          "CloseYear": 2021,
          "Customer": "CatalanaOcc"
        },
        {
          "Number": "27821933",
          "EntryDay": 1,
          "EntryMonth": 6,
          "EntryYear": 2021,
          "CloseDay": null,
          "CloseMonth": null,
          "CloseYear": null,
          "Customer": "CatalanaOcc"
        }
      ]
    },
    {
      "name": "Accidents",
      "source": "Accidents_source_load",
      "transform": [
        {
          "type": "formula",
          "expr": "datetime(datum.EntryYear, datum.EntryMonth-1,datum.EntryDay)",
          "as": "EntryDate"
        },
        {
          "type": "formula",
          "expr": "if(datum.CloseMonth==null, null, datetime(datum.CloseYear, datum.CloseMonth-1,datum.CloseDay))",
          "as": "CloseDate"
        },
        {
          "type": "formula",
          "expr": "if(datum.CloseMonth==null,'Entry Cabinet', 'Closed Cabinet')",
          "as": "Type"
        },
        {
          "type": "formula",
          "expr": "if(Customer=='All'||datum.Customer==Customer,1,0)",
          "as": "cond_Customer"
        },
        {"type": "filter", "expr": "datum.cond_Customer==1"},
        {
          "type": "collect",
          "sort": {"field": ["EntryDate"], "order": ["ascending"]}
        }
      ]
    },
    {
      "name": "Close_table",
      "source": "Accidents",
      "transform": [
        {"type": "filter", "expr": "datum.Type=='Closed Cabinet'"},
        {"type": "formula", "expr": "datum.CloseDate", "as": "Date"},
        {
          "type": "aggregate",
          "groupby": ["Date"],
          "fields": ["Number"],
          "ops": ["count"],
          "as": ["Total"]
        },
        {
          "type": "collect",
          "sort": {"field": ["Date"], "order": ["ascending"]}
        },
        {
          "type": "window",
          "sort": {"field": "Date", "order": "ascending"},
          "ops": ["sum"],
          "fields": ["Total"],
          "as": ["Cumulative"]
        },
        {
          "type": "formula",
          "expr": "datum.Cumulative*(-1)",
          "as": "Cumulative"
        },
        {"type": "collect", "sort": {"field": ["Date"], "order": ["ascending"]}}
      ]
    },
    {
      "name": "Entry_table",
      "source": "Accidents",
      "transform": [
        {"type": "formula", "expr": "datum.EntryDate", "as": "Date"},
        {
          "type": "aggregate",
          "groupby": ["Date"],
          "fields": ["Number"],
          "ops": ["count"],
          "as": ["Total"]
        },
        {
          "type": "collect",
          "sort": {"field": ["Date"], "order": ["ascending"]}
        },
        {
          "type": "window",
          "ops": ["sum"],
          "fields": ["Total"],
          "as": ["Cumulative"]
        },
        {"type": "collect", "sort": {"field": ["Date"], "order": ["ascending"]}}
      ]
    },
    {
      "name": "Entry_Close",
      "source": ["Entry_table", "Close_table"],
      "transform": [
        {
          "type": "collect",
          "sort": {"field": ["Date"], "order": ["ascending"]}
        },
        {
          "type": "aggregate",
          "groupby": ["Date"],
          "fields": ["Cumulative"],
          "ops": ["sum"],
          "as": ["Cumulative_Total"]
        },
        {"type": "collect", "sort": {"field": ["Date"], "order": ["ascending"]}}
      ]
    }
  ],
  "scales": [
    {
      "name": "yscale",
      "type": "linear",
      "domain": {"data": "Entry_Close", "field": "Cumulative_Total"},
      "range": "height"
    },
    {
      "name": "xscale",
      "type": "time",
      "domain": {"data": "Entry_Close", "field": "Date", "sort": true},
      "range": "width"
    }
  ],
  "axes": [
    {"orient": "bottom", "scale": "xscale"},
    {"orient": "left", "scale": "yscale"}
  ],
  "marks": [
    {
      "type": "line",
      "from": {"data": "Entry_Close"},
      "sort": {"field": "datum.Date"},
      "encode": {
        "update": {
          "x": {"scale": "xscale", "field": "Date"},
          "y": {"scale": "yscale", "field": "Cumulative_Total"},
          "stroke": {"value": "blue"},
          "strokeWidth": {"value": 4},
          "interpolate": {"value": "linear"},
          "strokeOpacity": {"value": 1}
        },
        "hover": {"strokeOpacity": {"value": 0.5}}
      }
    },
    {
      "type": "symbol",
      "style": ["point"],
      "from": {"data": "Entry_Close"},
      "encode": {
        "update": {
          "x": {"scale": "xscale", "field": "Date"},
          "y": {"scale": "yscale", "field": "Cumulative_Total"},
          "size": {"value": 60},
          "fill": {"value": "green"},
          "cursor": {"value": "pointer"},
          "tooltip": {
            "signal": "{'title': 'Cumulative', 'Date': datum.Date,'Total': datum.Cumulative_Total}"
          }
        }
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.