我试着用Deneb的Power bi和Vega lite语言制作了两张面积图.

enter image description here

有两件事我很努力地go 做:

  • 1000以下的值(例如700)我想将它们显示为0.7k(当前格式似乎不能转换它们,它只格式化1000以上的值)
  • 我想用绿色突出显示两个图表的最大值,用红色突出显示最小值

以下是完整的代码:

{
  "data": {"name": "dataset"},
  "layer": [
    {
      "mark": {
        "type": "area",
        "line": {"color": "#063970"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {"offset": 0, "color": "white"},
            {"offset": 1, "color": "#063970"}
          ]
        }
      }
    },
    {
      "mark": {
        "type": "area",
        "line": {"color": "#2596be"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {"offset": 0, "color": "white"},
            {"offset": 1, "color": "#2596be"}
          ]
        }
      },
      "encoding": {
        "y": {"field": "Variable 1", "type": "quantitative"}
      }
    },
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "Variable 2", "format": ".2s"},
        "opacity": {
          "condition": {"test": {"field": "MONTH", "equal": "off"}, "value": 0.1},
          "value": 1
        },
        "y": {"field": "Variable 2", "type": "quantitative", "axis": null}
      }
    },
    
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "Variable 1", "format": ".2s"
},
        "opacity": {
          "condition": {"test": {"field": "MONTH", "equal": "off"}, "value": 0.1},
          "value": 1
        },
        "y": {"field": "Variable 1", "type": "quantitative", "axis": null}
      }
    }
  ],
  "encoding": {
    "x": {
      "field": "MONTH",
      "type": "ordinal",
      "axis": {"labelPadding": 0},
      "title": null
    },
    "y": {
      "field": "Variable 2",
      "type": "quantitative",
      "axis": null
    }
  }
}

推荐答案

这就是你要的.D3格式不能满足您的要求,因此只需使用转换和表达式来滚动您自己的格式即可.

enter image description here

{
  "data": {"name": "dataset"},
  "transform": [
    {
      "calculate": "format(datum['Leaves Count 2022']/1000,'0.1f')+'k'",
      "as": "l1"
    },
    {
      "calculate": "format(datum['Leaves Count 2023']/1000,'0.1f')+'k'",
      "as": "l2"
    },
    {
      "joinaggregate": [
        {
          "op": "max",
          "field": "Leaves Count 2022",
          "as": "l1max"
        },
        {
          "op": "max",
          "field": "Leaves Count 2023",
          "as": "l2max"
        },
        {
          "op": "min",
          "field": "Leaves Count 2022",
          "as": "l1min"
        },
        {
          "op": "min",
          "field": "Leaves Count 2023",
          "as": "l2min"
        }
      ]
    }
  ],
  "layer": [
    {
      "mark": {
        "type": "area",
        "line": {"color": "#063970"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {
              "offset": 0,
              "color": "white"
            },
            {
              "offset": 1,
              "color": "#063970"
            }
          ]
        }
      }
    },
    {
      "mark": {
        "type": "area",
        "line": {"color": "#2596be"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {
              "offset": 0,
              "color": "white"
            },
            {
              "offset": 1,
              "color": "#2596be"
            }
          ]
        }
      },
      "encoding": {
        "y": {
          "field": "Leaves Count 2022",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": {
        "type": "circle",
        "size": 50,
        "fill": {
          "expr": "datum['Leaves Count 2023']==datum.l2max? 'green':datum['Leaves Count 2023']==datum.l2min?'red':'#063970'"
        },
        "stroke": "white",
        "strokeWidth": 1
      },
      "encoding": {
        "x": {
          "field": "MONTH",
          "type": "ordinal"
        },
        "y": {
          "field": "Leaves Count 2023",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": {
        "type": "circle",
        "size": 50,
        "fill": {
          "expr": "datum['Leaves Count 2022']==datum.l1max? 'green':datum['Leaves Count 2022']==datum.l1min?'red':'#2596be'"
        },
        "stroke": "white",
        "strokeWidth": 1
      },
      "encoding": {
        "x": {
          "field": "MONTH",
          "type": "ordinal"
        },
        "y": {
          "field": "Leaves Count 2022",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "l2"},
        "opacity": {
          "condition": {
            "test": {
              "field": "MONTH",
              "equal": "off"
            },
            "value": 0.1
          },
          "value": 1
        },
        "y": {
          "field": "Leaves Count 2023",
          "type": "quantitative",
          "axis": null
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "l1"},
        "opacity": {
          "condition": {
            "test": {
              "field": "MONTH",
              "equal": "off"
            },
            "value": 0.1
          },
          "value": 1
        },
        "y": {
          "field": "Leaves Count 2022",
          "type": "quantitative",
          "axis": null
        }
      }
    }
  ],
  "encoding": {
    "x": {
      "field": "MONTH",
      "type": "ordinal",
      "axis": {"labelPadding": 0},
      "title": null
    },
    "y": {
      "field": "Leaves Count 2023",
      "type": "quantitative",
      "axis": null
    }
  }
}

Json相关问答推荐

在Oracle中循环整个json_opp_t以更改特定键的值

JoltChange:将输入数组的每个对象拆分为2个独立的对象,并将其放入输出数组中

使用Powershell脚本将配置信息块添加到json文件

使用Jolt将字符串数组转换为JSON对象数组

时间序列的Vega Lite分组条形图

如何将加权边列表导出到JSON树?

Flutter -控制器问题-JSON API

VBA json按特定属性名称提取所有数据

Oracle JSON 查询中的动态列列表

使用Powershell将JSON文件加载到SQL Server表格

jq EOF 处的无效数字文字将 json 值更新为 0.0.x

jq可以在两个JSON对象列表中依次添加对象吗?

如何在 powerapps 中将详细信息表单转换为 json?

如何使用 ConfigurationBuilder 解析现有的 json 字符串(不是文件)

如何将任意 json 对象发布到 webapi

apple-app-site-association json 文件是否会在应用程序中更新?

如何将 LinkedTreeMap 转换为 gson JsonObject

JSON.stringify 不会转义?

Golang struct 的 XML 和 JSON 标签?

如何将 Swift 对象转换为字典