Skip to content

Commit 19bd88e

Browse files
authored
Merge pull request #86 from sgratzl/release/v4.2.5
Release v4.2.5
2 parents c324521 + 77f3933 commit 19bd88e

File tree

5 files changed

+110
-5
lines changed

5 files changed

+110
-5
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default defineConfig({
3131
{ text: 'Tree with Labels', link: '/examples/label' },
3232
{ text: 'Directed Tree', link: '/examples/directed' },
3333
{ text: 'Tree Orientations', link: '/examples/orientation' },
34+
{ text: 'Fully Linked', link: '/examples/linked' },
3435
],
3536
},
3637
{

docs/examples/linked.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Linked
3+
---
4+
5+
# Linked
6+
7+
<script setup>
8+
import {config} from './linked';
9+
</script>
10+
11+
<ForceDirectedGraphChart
12+
:options="config.options"
13+
:data="config.data"
14+
/>
15+
16+
### Code
17+
18+
:::code-group
19+
20+
<<< ./linked.ts#config [config]
21+
22+
<<< ./linked.ts#data [data]
23+
24+
:::

docs/examples/linked.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { ChartConfiguration } from 'chart.js';
2+
import type {} from '../../src';
3+
import 'chartjs-plugin-datalabels';
4+
5+
// #region data
6+
7+
const edges = [
8+
{ source: 1, target: 0 },
9+
{ source: 2, target: 0 },
10+
{ source: 2, target: 1 },
11+
{ source: 3, target: 1 },
12+
{ source: 3, target: 0 },
13+
{ source: 3, target: 2 },
14+
];
15+
16+
const widths = [2, 5, 10, 15, 20, 25];
17+
const dashes = [
18+
[2, 2],
19+
[5, 5],
20+
[10, 10],
21+
[15, 15],
22+
[20, 20],
23+
[25, 25],
24+
];
25+
const colors = ['blue', 'red', 'green', 'purple', 'pink', 'yellow'];
26+
const nodeColors = ['yellow', 'pink', 'teal', 'violet'];
27+
28+
export const data: ChartConfiguration<'tree'>['data'] = {
29+
labels: ['A', 'B', 'C', 'D'],
30+
datasets: [
31+
{
32+
data: [{}, {}, {}, {}],
33+
edges: edges,
34+
pointRadius: 20,
35+
pointBackgroundColor: (ctx) => nodeColors[ctx.index],
36+
edgeLineBorderWidth: (ctx) => widths[ctx.index],
37+
edgeLineBorderDash: (ctx) => dashes[ctx.index],
38+
edgeLineBorderColor: (ctx) => colors[ctx.index],
39+
},
40+
],
41+
};
42+
// #endregion data
43+
// #region config
44+
export const config: ChartConfiguration<'tree'> = {
45+
type: 'forceDirectedGraph',
46+
data,
47+
options: {
48+
scales: {
49+
x: { min: -1.5, max: 1.5 },
50+
y: { min: -1.5, max: 1.5 },
51+
},
52+
plugins: { legend: { display: false } },
53+
},
54+
};
55+
// #endregion config

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "chartjs-chart-graph",
33
"description": "Chart.js module for charting graphs",
4-
"version": "4.2.4",
4+
"version": "4.2.5",
55
"author": {
66
"name": "Samuel Gratzl",
77
"email": "sam@sgratzl.com",

src/controllers/GraphController.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ export class GraphController extends ScatterController {
207207
this.stopLayout();
208208
}
209209

210+
declare getContext: (index: number, active: boolean, mode: UpdateMode) => unknown;
211+
210212
/**
211213
* @hidden
212214
*/
@@ -215,14 +217,33 @@ export class GraphController extends ScatterController {
215217
_cachedDataOpts: this._cachedDataOpts,
216218
dataElementType: this.dataElementType,
217219
_sharedOptions: this._sharedOptions,
220+
// getDataset: this.getDataset,
221+
// getParsed: this.getParsed,
218222
};
219223
this._cachedDataOpts = {};
220224
this.dataElementType = this.edgeElementType;
221225
this._sharedOptions = this._edgeSharedOptions;
226+
227+
const dataset = this.getDataset();
222228
const meta = this._cachedMeta;
223-
const nodes = meta.data;
229+
const nodeElements = meta.data;
224230
const data = (this._cachedMeta as unknown as IExtendedChartMeta)._parsedEdges;
225231

232+
// get generic context to prefill cache
233+
this.getContext(-1, false, mode);
234+
this.getDataset = () => {
235+
return new Proxy(dataset, {
236+
get(obj: any, prop: string) {
237+
return prop === 'data' ? obj.edges ?? [] : obj[prop];
238+
},
239+
});
240+
};
241+
this.getParsed = (index: number) => {
242+
return data[index] as any;
243+
};
244+
// patch meta to store edges
245+
meta.data = (meta as any).edges;
246+
226247
const reset = mode === 'reset';
227248

228249
const firstOpts = this.resolveDataElementOptions(start, mode);
@@ -253,11 +274,11 @@ export class GraphController extends ScatterController {
253274
const parsed = data[index];
254275

255276
const properties: any = {
256-
source: nodes[parsed.source],
257-
target: nodes[parsed.target],
277+
source: nodeElements[parsed.source],
278+
target: nodeElements[parsed.target],
258279
points: Array.isArray(parsed.points) ? parsed.points.map((p) => copyPoint(p)) : [],
259280
};
260-
properties.points._source = nodes[parsed.source];
281+
properties.points._source = nodeElements[parsed.source];
261282
if (includeOptions) {
262283
if (sharedOptions !== dummyShared) {
263284
properties.options = sharedOptions;
@@ -271,6 +292,10 @@ export class GraphController extends ScatterController {
271292

272293
this._edgeSharedOptions = this._sharedOptions;
273294
Object.assign(this, bak);
295+
delete (this as any).getDataset;
296+
delete (this as any).getParsed;
297+
// patch meta to store edges
298+
meta.data = nodeElements;
274299
}
275300

276301
/**

0 commit comments

Comments
 (0)