Skip to content

Commit

Permalink
feat: support axis labelExpr + add example with month initial
Browse files Browse the repository at this point in the history
Fix #5122
Fix #5249
  • Loading branch information
kanitw committed Aug 5, 2019
1 parent dd838c3 commit 3bcebd2
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 9 deletions.
4 changes: 4 additions & 0 deletions build/vega-lite-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@
}
]
},
"labelExpr": {
"description": "Vega expression for customizing labels text.\nNote that the string label and value can be assessed via the `label` and `value` of the backing `datum` object.",
"type": "string"
},
"labelFlush": {
"description": "Indicates if the first and last axis labels should be aligned flush with the scale range. Flush alignment for a horizontal axis will left-align the first label and right-align the last label. For vertical axes, bottom and top text baselines are applied instead. If this property is a number, it also indicates the number of pixels by which to offset the first and last labels; for example, a value of 2 will flush-align the first and last labels and also push them 2 pixels outward from the center of the axis. The additional adjustment can sometimes help the labels better visually group with corresponding axis ticks.\n\n__Default value:__ `true` for axis of a continuous x-scale. Otherwise, `false`.",
"type": [
Expand Down
1 change: 1 addition & 0 deletions examples/compiled/bar_month_temporal_initial.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
138 changes: 138 additions & 0 deletions examples/compiled/bar_month_temporal_initial.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "Using `labelExpr` to show only initial letters of month names.",
"autosize": "pad",
"padding": 5,
"width": 200,
"height": 200,
"style": "cell",
"data": [
{
"name": "source_0",
"url": "data/seattle-weather.csv",
"format": {"type": "csv", "parse": {"date": "date"}},
"transform": [
{
"type": "formula",
"as": "month_date",
"expr": "datetime(0, month(datum[\"date\"]), 1, 0, 0, 0, 0)"
},
{
"type": "formula",
"as": "month_date_end",
"expr": "datetime(0, month(datum[\"date\"])+1, 1, 0, 0, 0, 0)"
},
{
"type": "aggregate",
"groupby": ["month_date", "month_date_end"],
"ops": ["mean"],
"fields": ["precipitation"],
"as": ["mean_precipitation"]
},
{
"type": "filter",
"expr": "datum[\"month_date\"] !== null && !isNaN(datum[\"month_date\"])"
}
]
}
],
"marks": [
{
"name": "marks",
"type": "rect",
"style": ["bar"],
"from": {"data": "source_0"},
"encode": {
"update": {
"fill": {"value": "#4c78a8"},
"x2": [
{
"test": "datum[\"month_date\"] === null || isNaN(datum[\"month_date\"])",
"value": 0
},
{"scale": "x", "field": "month_date", "offset": 1}
],
"x": [
{
"test": "datum[\"month_date\"] === null || isNaN(datum[\"month_date\"])",
"value": 0
},
{"scale": "x", "field": "month_date_end"}
],
"y": {"scale": "y", "field": "mean_precipitation"},
"y2": {"scale": "y", "value": 0}
}
}
}
],
"scales": [
{
"name": "x",
"type": "time",
"domain": {
"data": "source_0",
"fields": ["month_date", "month_date_end"]
},
"range": [0, {"signal": "width"}]
},
{
"name": "y",
"type": "linear",
"domain": {"data": "source_0", "field": "mean_precipitation"},
"range": [{"signal": "height"}, 0],
"nice": true,
"zero": true
}
],
"axes": [
{
"scale": "x",
"orient": "bottom",
"gridScale": "y",
"grid": true,
"domain": false,
"labels": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "y",
"orient": "left",
"gridScale": "x",
"grid": true,
"tickCount": {"signal": "ceil(height/40)"},
"domain": false,
"labels": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "x",
"orient": "bottom",
"grid": false,
"title": "date (month)",
"labelAlign": "left",
"labelFlush": true,
"labelOverlap": true,
"encode": {
"labels": {
"update": {"text": {"signal": "timeFormat(datum.value, '%b')[0]"}}
}
},
"zindex": 0
},
{
"scale": "y",
"orient": "left",
"grid": false,
"title": "Mean of precipitation",
"labelOverlap": true,
"tickCount": {"signal": "ceil(height/40)"},
"zindex": 0
}
]
}
22 changes: 22 additions & 0 deletions examples/specs/bar_month_temporal_initial.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"description": "Using `labelExpr` to show only initial letters of month names.",
"data": {"url": "data/seattle-weather.csv"},
"mark": "bar",
"encoding": {
"x": {
"timeUnit": "month",
"field": "date",
"type": "temporal",
"axis": {
"labelAlign": "left",
"labelExpr": "datum.label[0]"
}
},
"y": {
"aggregate": "mean",
"field": "precipitation",
"type": "quantitative"
}
}
}
6 changes: 5 additions & 1 deletion site/docs/encoding/axis.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ By default, Vega-Lite automatically sets the axis extent (the space axis ticks a

### Labels

{% include table.html props="format,formatType,labels,labelAlign,labelAngle,labelBaseline,labelBound,labelColor,labelFlush,labelFlushOffset,labelFont,labelFontSize,labelFontStyle,labelFontWeight,labelLimit,labelOpacity,labelOverlap,labelPadding" source= "Axis" %}
{% include table.html props="format,formatType,labels,labelAlign,labelAngle,labelBaseline,labelBound,labelColor,labelExpr,labelFlush,labelFlushOffset,labelFont,labelFontSize,labelFontStyle,labelFontWeight,labelLimit,labelOpacity,labelOverlap,labelPadding" source= "Axis" %}

**See also:** [`guide-label` style config](mark.html#style-config) (common styles for axis, [legend](legend.html), and [header](facet.html#header) labels).

#### Example: Using Axis `labelExpr` to Display Initial Letters of Month Name

<div class="vl-example" data-name="bar_month_temporal_initial"></div>

{:#ticks}

### Ticks
Expand Down
7 changes: 7 additions & 0 deletions src/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ export interface AxisOrientMixins {
export type AxisConfig = VlOnlyGuideConfig & AxisOrientMixins & VgAxisConfigNoSignals;

export interface Axis extends AxisOrientMixins, VgAxisConfigNoSignals, Guide {
/**
* Vega expression for customizing labels text.
* Note that the string label and value can be assessed via the `label` and `value` of the backing `datum` object.
*/
labelExpr?: string;

/**
* The offset, in pixels, by which to displace the axis from the edge of the enclosing group or data rectangle.
*
Expand Down Expand Up @@ -417,6 +423,7 @@ export const COMMON_AXIS_PROPERTIES_INDEX: Flag<keyof (VgAxis | Axis)> = {

const AXIS_PROPERTIES_INDEX: Flag<keyof Axis> = {
...COMMON_AXIS_PROPERTIES_INDEX,
labelExpr: 1,
encoding: 1
};

Expand Down

0 comments on commit 3bcebd2

Please sign in to comment.