Skip to content

Commit

Permalink
expose internal pubsub creator as uPlot.sync(key). close #354, #430.
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Feb 12, 2021
1 parent 1816629 commit a8daaa3
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 166 deletions.
38 changes: 0 additions & 38 deletions demos/log-scales2.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,44 +248,6 @@



// let negVals = vals.slice().reverse().map(v => -v);
// let vals2 = negVals.concat(0, vals);

let vals7 = [-1000, -100, -10, -1, -0.1, -0.01, 0, 0.01, 0.1, 1, 10, 100, 1000];

let data7 = [
vals7.map((v, i) => i + 1),
vals7,
];



const opts7 = {
width: 1600,
height: 600,
title: "Hypersine Y Scale (-1000 -> 1000)",
scales: {
x: {
time: false,
},
y: {
distr: 4,
log: 10,
},
},
series: [
{},
{
stroke: "blue",
fill: "rgba(0,0,255,0.1)",
},
],
};

let u7 = new uPlot(opts7, vals7, document.body);



// https://bost.ocks.org/mike/shuffle/
function shuffle(array) {
var m = array.length, t, i;
Expand Down
30 changes: 29 additions & 1 deletion demos/sync-cursor.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<script src="../dist/uPlot.iife.js"></script>
<h2 id="wait">Loading lib....</h2>

<button id="sync">Disable Sync</button>

<br>

<script>
function round2(val) {
return Math.round(val * 100) / 100;
Expand Down Expand Up @@ -79,13 +83,15 @@ <h2 id="wait">Loading lib....</h2>
function makeChart(data) {
console.time('chart');

let mooSync = uPlot.sync("moo");

const cursorOpts = {
lock: true,
focus: {
prox: 16,
},
sync: {
key: "moo",
key: mooSync.key,
setSeries: true,
},
};
Expand Down Expand Up @@ -194,6 +200,28 @@ <h2 id="wait">Loading lib....</h2>

wait.textContent = "Done!";
console.timeEnd('chart');

let synced = true;
let syncBtn = document.getElementById('sync');

let fooSync = uPlot.sync("foo");

syncBtn.onclick = () => {
synced = !synced;

if (synced) {
mooSync.sub(uplot1);
mooSync.sub(uplot2);
mooSync.sub(uplot3);
syncBtn.textContent = 'Disable Sync';
}
else {
mooSync.unsub(uplot1);
mooSync.unsub(uplot2);
mooSync.unsub(uplot3);
syncBtn.textContent = 'Enable Sync';
}
}
/*
setTimeout(() => {
uplot1.destroy();
Expand Down
47 changes: 29 additions & 18 deletions dist/uPlot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,24 +1305,31 @@ const yScaleOpts = assign({}, xScaleOpts, {

const syncs = {};

function _sync(opts) {
let clients = [];

return {
sub(client) {
clients.push(client);
},
unsub(client) {
clients = clients.filter(c => c != client);
},
pub(type, self, x, y, w, h, i) {
if (clients.length > 1) {
clients.forEach(client => {
client != self && client.pub(type, self, x, y, w, h, i);
});
function _sync(key, opts) {
let s = syncs[key];

if (!s) {
let clients = [];

s = {
key,
sub(client) {
clients.push(client);
},
unsub(client) {
clients = clients.filter(c => c != client);
},
pub(type, self, x, y, w, h, i) {
for (let i = 0; i < clients.length; i++)
clients[i] != self && clients[i].pub(type, self, x, y, w, h, i);
}
}
};
};

if (key != null)
syncs[key] = s;
}

return s;
}

function orient(u, seriesIdx, cb) {
Expand Down Expand Up @@ -4319,7 +4326,7 @@ function uPlot(opts, data, then) {

const syncKey = syncOpts.key;

const sync = (syncKey != null ? (syncs[syncKey] = syncs[syncKey] || _sync()) : _sync());
const sync = _sync(syncKey);

sync.sub(self);

Expand Down Expand Up @@ -4383,6 +4390,10 @@ uPlot.orient = orient;
uPlot.tzDate = tzDate;
}

{
uPlot.sync = _sync;
}

{
uPlot.addGap = addGap;
uPlot.clipGaps = clipGaps;
Expand Down
10 changes: 10 additions & 0 deletions dist/uPlot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ declare class uPlot {

/** helper function for grabbing proper drawing orientation vars and fns for a plot instance (all dims in canvas pixels) */
static orient: (u: uPlot, seriesIdx: number, callback: uPlot.OrientCallback) => any;

/** returns a pub/sub instance shared by all plots usng the provided key */
static sync: (key: string) => uPlot.SyncPubSub;
}

export = uPlot;
Expand Down Expand Up @@ -346,6 +349,13 @@ declare namespace uPlot {
over?: boolean; // true
}

export interface SyncPubSub {
key: string;
sub: (client: uPlot) => void;
unsub: (client: uPlot) => void;
pub: (type: string, client: uPlot, x: number, y: number, w: number, h: number, i: number) => void;
}

export namespace Cursor {
export type LeftTop = [left: number, top: number];

Expand Down
47 changes: 29 additions & 18 deletions dist/uPlot.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,24 +1303,31 @@ const yScaleOpts = assign({}, xScaleOpts, {

const syncs = {};

function _sync(opts) {
let clients = [];

return {
sub(client) {
clients.push(client);
},
unsub(client) {
clients = clients.filter(c => c != client);
},
pub(type, self, x, y, w, h, i) {
if (clients.length > 1) {
clients.forEach(client => {
client != self && client.pub(type, self, x, y, w, h, i);
});
function _sync(key, opts) {
let s = syncs[key];

if (!s) {
let clients = [];

s = {
key,
sub(client) {
clients.push(client);
},
unsub(client) {
clients = clients.filter(c => c != client);
},
pub(type, self, x, y, w, h, i) {
for (let i = 0; i < clients.length; i++)
clients[i] != self && clients[i].pub(type, self, x, y, w, h, i);
}
}
};
};

if (key != null)
syncs[key] = s;
}

return s;
}

function orient(u, seriesIdx, cb) {
Expand Down Expand Up @@ -4317,7 +4324,7 @@ function uPlot(opts, data, then) {

const syncKey = syncOpts.key;

const sync = (syncKey != null ? (syncs[syncKey] = syncs[syncKey] || _sync()) : _sync());
const sync = _sync(syncKey);

sync.sub(self);

Expand Down Expand Up @@ -4381,6 +4388,10 @@ uPlot.orient = orient;
uPlot.tzDate = tzDate;
}

{
uPlot.sync = _sync;
}

{
uPlot.addGap = addGap;
uPlot.clipGaps = clipGaps;
Expand Down
47 changes: 29 additions & 18 deletions dist/uPlot.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,24 +1306,31 @@ var uPlot = (function () {

var syncs = {};

function _sync(opts) {
var clients = [];

return {
sub: function sub(client) {
clients.push(client);
},
unsub: function unsub(client) {
clients = clients.filter(c => c != client);
},
pub: function pub(type, self, x, y, w, h, i) {
if (clients.length > 1) {
clients.forEach(client => {
client != self && client.pub(type, self, x, y, w, h, i);
});
function _sync(key, opts) {
var s = syncs[key];

if (!s) {
var clients = [];

s = {
key: key,
sub: function sub(client) {
clients.push(client);
},
unsub: function unsub(client) {
clients = clients.filter(c => c != client);
},
pub: function pub(type, self, x, y, w, h, i) {
for (var i$1 = 0; i$1 < clients.length; i$1++)
{ clients[i$1] != self && clients[i$1].pub(type, self, x, y, w, h, i$1); }
}
}
};
};

if (key != null)
{ syncs[key] = s; }
}

return s;
}

function orient(u, seriesIdx, cb) {
Expand Down Expand Up @@ -4345,7 +4352,7 @@ var uPlot = (function () {

var syncKey = syncOpts.key;

var sync = (syncKey != null ? (syncs[syncKey] = syncs[syncKey] || _sync()) : _sync());
var sync = _sync(syncKey);

sync.sub(self);

Expand Down Expand Up @@ -4409,6 +4416,10 @@ var uPlot = (function () {
uPlot.tzDate = tzDate;
}

{
uPlot.sync = _sync;
}

{
uPlot.addGap = addGap;
uPlot.clipGaps = clipGaps;
Expand Down
2 changes: 1 addition & 1 deletion dist/uPlot.iife.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"homepage": "https://github.com/leeoniya/uPlot#readme",
"devDependencies": {
"@rollup/plugin-buble": "^0.21.3",
"rollup": "^2.38.5",
"rollup": "^2.39.0",
"rollup-plugin-terser": "^7.0.2"
}
}
1 change: 0 additions & 1 deletion src/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ export function numAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foun
return splits;
}

// this doesnt work for sin, which needs to come off from 0 independently in pos and neg dirs
export function logAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
const splits = [];

Expand Down
39 changes: 23 additions & 16 deletions src/sync.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
export const syncs = {};

export function _sync(opts) {
let clients = [];
export function _sync(key, opts) {
let s = syncs[key];

return {
sub(client) {
clients.push(client);
},
unsub(client) {
clients = clients.filter(c => c != client);
},
pub(type, self, x, y, w, h, i) {
if (clients.length > 1) {
clients.forEach(client => {
client != self && client.pub(type, self, x, y, w, h, i);
});
if (!s) {
let clients = [];

s = {
key,
sub(client) {
clients.push(client);
},
unsub(client) {
clients = clients.filter(c => c != client);
},
pub(type, self, x, y, w, h, i) {
for (let i = 0; i < clients.length; i++)
clients[i] != self && clients[i].pub(type, self, x, y, w, h, i);
}
}
};
};

if (key != null)
syncs[key] = s;
}

return s;
}
Loading

0 comments on commit a8daaa3

Please sign in to comment.