Skip to content

Commit

Permalink
Merge branch '8-re-add-support-for-lcjs-headless-with-lcjs-v5-x' into…
Browse files Browse the repository at this point in the history
… 'master'

Resolve "Re-add support for lcjs-headless with LCJS v5.x"

Closes #8

See merge request arction/lcjs/lcjs-headless!3
  • Loading branch information
Niilo Keinänen committed Aug 5, 2024
2 parents c52e09d + 3d6a43f commit e769b69
Show file tree
Hide file tree
Showing 12 changed files with 3,167 additions and 94 deletions.
3 changes: 2 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
registry=https://registry.npmjs.org/
registry=https://registry.npmjs.org/
@lightningchart:registry=https://registry.npmjs.org/
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - yyyy-mm-dd

### Added

### Changed
Expand All @@ -15,31 +17,46 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security

## [2.0.0] - 2024-08-06

### Changed

- Re-added compatibility with later (v6.0.0^) LightningChart JS versions
- Using headless requires a deployment license always. Developer licenses are not supported.
- NPM organization changed from `@arction` to `@lightningchart`

## [1.6.0] - 2023-02-07

### Added

- Support for LightningChart JS 4.0.0+.

## [1.5.0] - 2021-12-01

### Added

- Support for LightningChart JS 3.3.0+ and ImageFill.

## [1.4.0] - 2021-07-28

### Added

- Support for LightningChart JS 3.1.0+ and Map Charts.

## [1.3.0] - 2021-05-05

### Added

- Support for LightningChart JS 3.0.0+

## [1.1.0] - 2020-12-01

### Added

- Support for LightningChart JS 2.1.0+

## [1.0.0] - 2020-04-28

### Added

- Initial release
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2020 Arction Ltd.
Copyright (c) 2024 LightningChart Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
118 changes: 74 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This package uses [JSDOM][jsdom], [node-canvas][node-canvas] and [headless-gl][g

### Linux

Only Ubuntu is currently officially supported. `@arction/lcjs-headless` most likely works on other distributions but might require extra work.
Only Ubuntu is currently officially supported. `@lightningchart/lcjs-headless` most likely works on other distributions but might require extra work.

#### Ubuntu

Expand Down Expand Up @@ -54,45 +54,57 @@ See [headless-gl system dependencies][gl-dependencies] for more details.

## Getting Started

Install both `@arction/lcjs-headless` and `@arction/lcjs` from npm.
Install both `@lightningchart/lcjs-headless` and `@lightningchart/lcjs` from npm.

`npm install @arction/lcjs-headless @arction/lcjs`
`npm install @lightningchart/lcjs-headless @lightningchart/lcjs`

When creating a new chart make sure to import the `lightningChart()` function from `@arction/lcjs-headless` instead of `@arction/lcjs`. Other LightningChart JS related imports can be imported from `@arction/lcjs`.
When creating a new chart make sure to import the `lightningChart()` function from `@lightningchart/lcjs-headless` instead of `@lightningchart/lcjs`. Other LightningChart JS related imports can be imported from `@lightningchart/lcjs`.

To render a chart to a buffer, call `chart.engine.renderFrame(width, height)`. This function will provide you a buffer containing a single image.

```js
import { lightningChart } from '@arction/lcjs-headless'
import { lightningChart } from "@lightningchart/lcjs-headless";

const lc = lightningChart()
const chart = lc.ChartXY()
const lc = lightningChart({
license: "my-deployment-license-key",
licenseInformation: "my-deployment-license-information",
});
const chart = lc.ChartXY();

chart.engine.renderFrame(1280, 720)
chart.engine.renderFrame(1280, 720);
```

The `@arction/lcjs-headless` package provides a couple of helper functions to make the use of LightningChart JS in Node JS environment easier. You can render an image directly to a `sharp` or `pngjs` objects with `renderToSharp` and `renderToPNG` helper functions.
The `@lightningchart/lcjs-headless` package provides a couple of helper functions to make the use of LightningChart JS in Node JS environment easier. You can render an image directly to a `sharp` or `pngjs` objects with `renderToSharp` and `renderToPNG` helper functions.

```js
import { lightningChart, renderToSharp } from '@arction/lcjs-headless'
import { lightningChart, renderToSharp } from "@lightningchart/lcjs-headless";

const lc = lightningChart()
const chart = lc.ChartXY()
const lc = lightningChart({
license: "my-deployment-license-key",
licenseInformation: "my-deployment-license-information",
});
const chart = lc.ChartXY();

renderToSharp(chart, 1920, 1080).toFile('out.png')
renderToSharp(chart, 1920, 1080).toFile("out.png");
```

```js
const fs = require('fs')
const { PNG } = require('pngjs')
const { lightningChart, renderToPNG } = require('@arction/lcjs-headless')

const lc = lightningChart()
const chart = lc.ChartXY()

const chartOutput = renderToPNG(chart, 1920, 1080)
const outputBuff = PNG.sync.write(chartOutput)
fs.writeFileSync('./chartOutput.png', outputBuff)
const fs = require("fs");
const { PNG } = require("pngjs");
const {
lightningChart,
renderToPNG,
} = require("@lightningchart/lcjs-headless");

const lc = lightningChart({
license: "my-deployment-license-key",
licenseInformation: "my-deployment-license-information",
});
const chart = lc.ChartXY();

const chartOutput = renderToPNG(chart, 1920, 1080);
const outputBuff = PNG.sync.write(chartOutput);
fs.writeFileSync("./chartOutput.png", outputBuff);
```

### Local Resources
Expand All @@ -101,8 +113,17 @@ When using Map Chart with in Node JS you need to provide the path to the LCJS re

```js
const lcjs = lightningChart({
resourcesBaseUrl: `fs:${path.resolve(__dirname, 'node_modules', '@arction', 'lcjs', 'dist', 'resources')}`
})
license: "my-deployment-license-key",
licenseInformation: "my-deployment-license-information",
resourcesBaseUrl: `fs:${path.resolve(
__dirname,
"node_modules",
"@lightningchart",
"lcjs",
"dist",
"resources"
)}`,
});
```

### Headless in Linux machine
Expand All @@ -126,13 +147,16 @@ There is a few helper methods available that are exported by this package.
- Prepares the frame to a "sharp" object, which allows the use of `sharp` to manipulate the image further or export it to a many different image formats.

```js
import { lightningChart, renderToSharp } from '@arction/lcjs-headless'
import { lightningChart, renderToSharp } from "@lightningchart/lcjs-headless";

const lc = lightningChart()
const lc = lightningChart({
license: "my-deployment-license-key",
licenseInformation: "my-deployment-license-information",
});

const chart = lc.ChartXY()
const chart = lc.ChartXY();

renderToSharp(chart, 1920, 1080).toFile('out.png')
renderToSharp(chart, 1920, 1080).toFile("out.png");
```

> Note: There is a known issue with using `sharp` on Windows. https://sharp.pixelplumbing.com/install#canvas-and-windows
Expand All @@ -144,11 +168,11 @@ renderToSharp(chart, 1920, 1080).toFile('out.png')
- Prepares the frame to a PNG image which can then be written to disk.

```js
const fs = require('fs')
const { PNG } = require('pngjs')
const chartOutput = renderToPNG(chart, 1920, 1080)
const outputBuff = PNG.sync.write(chartOutput)
fs.writeFileSync('./chartOutput.png', outputBuff)
const fs = require("fs");
const { PNG } = require("pngjs");
const chartOutput = renderToPNG(chart, 1920, 1080);
const outputBuff = PNG.sync.write(chartOutput);
fs.writeFileSync("./chartOutput.png", outputBuff);
```

### `renderToBase64`
Expand Down Expand Up @@ -177,15 +201,18 @@ If the font is not a system font, then it needs to be registered before it can b
This function is re-exported by this package from the `node-canvas` package.

```js
import { lightningChart, registerFont } from '@arction/lcjs-headless'
import { lightningChart, registerFont } from "@lightningchart/lcjs-headless";
// Register Open Sans font from a font file
registerFont('OpenSans-Regular.ttf', { family: 'Open Sans' })
registerFont("OpenSans-Regular.ttf", { family: "Open Sans" });

// Create a chart
const lc = lightningChart()
const chart = lc.ChartXY()
const lc = lightningChart({
license: "my-deployment-license-key",
licenseInformation: "my-deployment-license-information",
});
const chart = lc.ChartXY();
// Use the registered font
chart.setTitleFont((f) => f.setFamily('Open Sans'))
chart.setTitleFont((f) => f.setFamily("Open Sans"));
```

## Anti-aliasing
Expand All @@ -195,15 +222,18 @@ Anti-aliasing that is normally available in browsers is not available when using
The `devicePixelRatio` option when creating a chart can be used to render the chart with higher resolution while scaling all elements so that when the image is downsampled to the target resolution it's displayed correctly but with the benefits of using higher resolution. Rendering at higher resolution is more work so the rendering is slower.

```js
import { lightningChart, renderToSharp } from '@arction/lcjs-headless'
import { lightningChart, renderToSharp } from "@lightningchart/lcjs-headless";

// Create a chart
const lc = lightningChart()
const lc = lightningChart({
license: "my-deployment-license-key",
licenseInformation: "my-deployment-license-information",
});
// Create the chart with a devicePixelRatio 3 to render at higher resolution for downsampling
const chart = lc.ChartXY({ devicePixelRatio: 3 })
const chart = lc.ChartXY({ devicePixelRatio: 3 });
// render the chart to a sharp object
// the renderToSharp has built in support for downsampling by providing the pixelRatio as the fourth parameter
renderToSharp(chart, 1920, 1080, false, 3).toFile('out.png')
renderToSharp(chart, 1920, 1080, false, 3).toFile("out.png");
```

Only the `renderToSharp` helper has a built in scaling to downsample the image.
Expand All @@ -219,7 +249,7 @@ Make sure to install `fontconfig` package.

If the font is not a system font, you will need to register the font file with `registerFont` function.

[lcjs]: https://www.arction.com/lightningchart-js/
[lcjs]: https://www.lightningchart.com/js-charts/
[gl]: https://github.com/stackgl/headless-gl
[jsdom]: https://github.com/jsdom/jsdom
[node-canvas]: https://github.com/Automattic/node-canvas
Expand Down
Loading

0 comments on commit e769b69

Please sign in to comment.