Skip to content

Commit 9d300cf

Browse files
committed
Forms: added in the legend
1 parent 11c5954 commit 9d300cf

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

demos/forms.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
return uPlot.assign({}, {
236236
width: 800,
237237
height: 567,
238+
legend: { markers: { dash: 'dashed', before: false, }, },
238239
cursor: { points: {
239240
size: (u, si) => u.series[si].points.size * 2,
240241
width: (u, si, size) => size / 4,

src/domClasses.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const LEGEND = pre + "legend"
1717
export const LEGEND_LIVE = pre + "live";
1818
export const LEGEND_INLINE = pre + "inline";
1919
export const LEGEND_SERIES = pre + "series";
20-
export const LEGEND_MARKER = pre + "marker";
20+
export const LEGEND_MARKERR = pre + "marker-r";
21+
export const LEGEND_MARKERL = pre + "marker-l";
2122
export const LEGEND_LABEL = pre + "label";
2223
export const LEGEND_VALUE = pre + "value";

src/opts.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ export const legendOpts = {
400400
mount: noop,
401401
markers: {
402402
show: true,
403-
width: 2,
403+
before: true,
404+
width: 10,
404405
stroke: legendStroke,
405406
fill: legendFill,
406407
dash: "solid",

src/uPlot.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,20 @@
6969
display: inline-block;
7070
}
7171

72-
.u-legend .u-marker {
72+
.u-legend .u-marker-l {
7373
width: 1em;
7474
height: 1em;
7575
margin-right: 4px;
7676
background-clip: padding-box !important;
7777
}
7878

79+
.u-marker-r {
80+
width: 1em;
81+
height: 1em;
82+
margin-left: 4px;
83+
background-clip: padding-box !important;
84+
}
85+
7986
.u-inline.u-live th::after {
8087
content: ":";
8188
vertical-align: middle;

src/uPlot.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ import {
106106
LEGEND_LIVE,
107107
LEGEND_INLINE,
108108
LEGEND_SERIES,
109-
LEGEND_MARKER,
109+
LEGEND_MARKERR,
110+
LEGEND_MARKERL,
110111
LEGEND_LABEL,
111112
LEGEND_VALUE,
112113
} from './domClasses';
@@ -614,6 +615,32 @@ export default function uPlot(opts, data, then) {
614615
const son = {show: true};
615616
const soff = {show: false};
616617

618+
function placeMarker(seriesIndex, label, before) {
619+
const svgProp = series[seriesIndex].points.form.svg;
620+
621+
const svgURI = 'http://www.w3.org/2000/svg';
622+
const svg = document.createElementNS(svgURI, 'svg');
623+
svg.classList.add(before ? LEGEND_MARKERL : LEGEND_MARKERR);
624+
const path = document.createElementNS(svgURI, 'path');
625+
label.appendChild(svg);
626+
svg.appendChild(path);
627+
path.setAttribute('d', svgProp.path);
628+
629+
const width = markers.width(self, seriesIndex);
630+
const dw = ceil(width/2);
631+
const vb = svgProp.viewBox;
632+
// Adapting the viewBox to the stroke's width
633+
svg.setAttribute('viewBox', (vb.minX - dw) + ' ' + (vb.minY - dw) + ' ' + (vb.width + 2 * dw) + ' ' + (vb.height + 2 * dw));
634+
if (width) {
635+
path.setAttribute('stroke-width', width);
636+
path.setAttribute('stroke', markers.stroke(self, seriesIndex));
637+
const dash = markers.dash(self, seriesIndex);
638+
if (dash != 'solid') path.setAttribute('stroke-dasharray', Array.isArray(dash) ? dash.join(' ') : '35 15');
639+
}
640+
const fill = markers.fill(self, seriesIndex);
641+
path.setAttribute('fill', fill == null ? transparent : fill);
642+
}
643+
617644
function initLegendRow(s, i) {
618645
if (i == 0 && (multiValLegend || !legend.live || mode == 2))
619646
return nullNullTuple;
@@ -629,20 +656,9 @@ export default function uPlot(opts, data, then) {
629656

630657
let label = placeTag("th", null, row);
631658

632-
if (markers.show) {
633-
let indic = placeDiv(LEGEND_MARKER, label);
634-
635-
if (i > 0) {
636-
let width = markers.width(self, i);
637-
638-
if (width)
639-
indic.style.border = width + "px " + markers.dash(self, i) + " " + markers.stroke(self, i);
640-
641-
indic.style.background = markers.fill(self, i);
642-
}
643-
}
644-
659+
if (markers.show && markers.before && i > 0) placeMarker(i, label, true);
645660
let text = placeDiv(LEGEND_LABEL, label);
661+
if (markers.show && !markers.before && i > 0) placeMarker(i, label, false);
646662

647663
if (s.label instanceof HTMLElement)
648664
text.appendChild(s.label);

0 commit comments

Comments
 (0)