maintainer: Franco Gervasi
Q chart is a charting tool based on Vega for Q
git clone [email protected]:nzzdev/Q-chart.git
cd Q-chart
nvm use
npm install
npm run build
Q-chart is configured by two env variables FONTS
and DIVERGING_COLOR_SCALES
and through config passed in toolRuntimeConfig
.
Q-chart renderes the SVG for the chart on the server. For the measurement of the label lengths to be correct you need to have to fonts loaded in node-canvas. Q-chart still uses vega 4 which needs node-canvas 1.6.
The font loading is achieved by some code based on the information in this blogpost: https://medium.com/@adamhooper/fonts-in-node-canvas-bbf0b6b0cabf
For this to work you need to have the FONTS
env variable defined like this:
[
{
"name": "Roboto",
"filename": "roboto.ttf",
"url": "https://github.com/google/fonts/blob/master/apache/roboto/Roboto-Regular.ttf?raw=true"
}
]
You can define multiple fonts if you need them. the name
property is the font-family name defined as the label font in the vega config.
If you registered any color schemes using toolRuntimeConfig you can configure them in env DIVERGING_COLOR_SCHEMES
to have them available as selectable options for the custom color scheme option (see below). The label
is used as the select element option label, the key is stored in the items data and the scheme_name is the reference to the registered scheme name in vega.
Example:
[
{
"label": "one",
"key": 0,
"scheme_name": "diverging_one"
},
{
"label": "two",
"key": 1,
"scheme_name": "diverging_two"
},
{
"label": "three",
"key": 2,
"scheme_name": "diverging_three"
}
]
Q-chart needs color/font configuration for vega passed in toolRuntimeConfig
to the /rendering-info/web
endpoint.
Example:
toolRuntimeConfig: {
axis: {}, // see https://vega.github.io/vega/docs/config/#axes for details
text: { // some default text configs
fontSize: 11,
fontWeight: 100,
fill: #FF0000,
font: 'Roboto'
},
colorSchemes: { // to be refactored for Q-chart v2
categorical_normal: ['#FF0000','#00FF00','#0000FF'], // any number of colors. used for categorial data
categorical_light: ['#AA0000','#00AA00','#0000AA'], // the same number of colors, used when on category is highlighted (then the others are shown in this color),
grays: ['#D3D3D3', ....] // a bunch of grays, currently only the 4th and 8th grays are used for the legend in some special cases with colorOverwrites
},
"diverging_one": [ // 3 color codes per array, they get registered using vega.scheme()
"#...",
"#...",
"#..."
]
}
}
The toolRuntimeConfig
is also configured in cli-config.js
to be used with https://github.com/nzzdev/Q-cli
`
Start the Q dev server
npx @nzz/q-cli server -c cli-config.js
Run the Q tool:
node index.js
The testing framework used in this repository is Code.
Run the tests:
npm run test
When changing or implementing...
- A
route
, it needs to be tested in thee2e-tests.js
file - Something on the frontend, it needs to be tested in the
dom-tests.js
file
We provide automatically built docker images at https://hub.docker.com/r/nzzonline/q-chart/. There are three options for deployment:
- use the provided images
- build your own docker images
- deploy the service using another technology
- Deploy
nzzonline/q-chart
to a docker environment - Set the ENV variables as described in the configuration section
The tool structure follows the general structure of each Q tool. Further information can be found in Q server documentation - Developing tools.
- rendering-info request comes in with toolRuntimeConfig
- the handler uses nunjucks to render the header/legend/footer markup
- if there is an exact pixel width given in toolRuntimeConfig, the svg gets build by injecting a request to /web-svg. otherwise a script is returned that measures the width of the container in the browser and then requests the svg from /web-svg
- in /web-svg we figure out the chart type
- the final vega spec then gets computed from
- the chart types
vega-spec.json
file - the config passed in toolRuntimeConfig (see below)
- the mappings defined in
chartTypes/type/mappings.js
- the prerender.js in
chartTypes/type/prerender.js
- the chart types
- after the svg is rendered by vega any
chartTypes/type/postprocessings.js
are executed to change the svg string
The following chart types are available:
- Bars
- Stacked Bars
- Line
- Dot Plot
- Arrow (only if exactly two data series)
This option is not available for arrow charts. For any other chart type it changes the color of all but the highlighted data series to the light variant.
If checked, the label on the X axis is hidden. This is mostly useful if you have dates on the X-axis.
Only available for linecharts with a single line.
These are only available if the chartType is bar
or stackedBar
If true, the bars are rendered horizontally (not columns).
If true the bars are rendered horizontally if the width is below 500px.
If defined this is the max value of the domain used for the axis. This is useful if you want to have multiple columm/bar charts comparable.
Only available if a date series is detected in the first column of the data array.
One of ["auto", "decade", "year", "quarter", "month", "day", "hour", "minute", "second"]
. This is used for the tick interval and the label format of the dates on the X axis. auto
will pick the interval based on the detected date format.
This is a dynamicEnum
resulting in the selected index to be stored with the item. It marks the beginning of the prognosis in a data series.
For linecharts this results in a dashed line after this point. For bar/column charts the bars get striped.
Only available for Line Charts
The Y-axis minimum value. Be careful to not create misleading charts!
The Y-axis maximum value.
If checked, the Y scale is reverse. Useful if a decreasing number is a positive thing.
Only available for users with the expert-chart
role. One of ["linear", "step-before", "step-after", "monotone"]
.
This is a direct mapping to vegas line mark option interpolation
. Only the mentioned interpolations are allowed though. See also https://vega.github.io/vega/docs/marks/line/
If checked the X axis is not interpreted as a date series anymore. This is usefule if you have stock market data where there is no trading during the night and you only show one or a couple of days/hours. This results in only the first and the last tick on the X axis beging labeled.
Only available for Dot Plots.
Results in a line beging drawn between the dots.
The min value for the Y axis
The max value for the Y axis
Only available for Arrow Charts.
The min value for the Y axis
The max value for the Y axis
Stores the key from one of the color schemes defined in the DIVERGING_COLOR_SCHEMES
env var. The json-schema gets built dynamically on runtime in resources/dynamicSchema.js
where the env var is read and the enum
options for colorScheme
get set using the configured color schemes.
This is useful to have different diverging color schemes available for different datasets.
The actual colors for these schemes need to get passed in toolRuntimeConfig.colorSchemes
where there needs to be a color scheme with the name configured as the scheme_name
in DIVERGING_COLOR_SCHEMES
given.
This is only available for chartType Bar
, StackedBar
, Line
and Dotplot
. It changes the colors at the specified positions in the category color scheme (given in toolRuntimeConfig.colorSchemes.categorical_normal
/toolRuntimeConfig.colorSchemes.categorical_light
) to the color given in this option.
The option is an array of objects with color
, colorLight
and position
values. The color
is used if no highlighting is in place. The colorLight
is used if any other data series than the one at position
is highlighted.
This is only available if no colorOverwritesRows
is given.
This is only available for chartType Bar
, StackedBar
, Line
and Dotplot
. It changes the colors of the rows at the specified positions to the color given in this option.
The option is an array of objects with color
, colorLight
and position
values. The color
is used if no highlighting is in place. The colorLight
is used if any other data series than the one at position
is highlighted.
This is only available if no colorOverwritesSeries
is given.
Options for how to handle large numbers, available for all chart types.
One of [0, 1, 1e3, 1e6, 1e9]
.
- If
0
(default), the divisor is chosen automatically depending on the maximum value in the data, and (if applicable)in Tausend
,in Millionen
,in Milliarden
is appended to the subtitle. - If not
0
, values are divided by the provided divisor, and the subtitle is not modified.
Copyright (c) Neue Zürcher Zeitung.
This software is licensed under the MIT License.