- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我将鼠标悬停在堆积折线图上时,它会显示不在范围内的所有线条的零。有没有办法隐藏这些值而不是向悬停工具添加噪音?
最小示例
Plotly.newPlot('test', [{
line: { shape : 'vh' },
stackgroup: '1',
x: [1, 2],
y: [1, 1],
}, {
line: { shape : 'vh' },
stackgroup: '1',
x: [3, 4],
y: [2, 2],
}, {
line: { shape : 'vh' },
stackgroup: '1',
x: [3, 4, 5, 6],
y: [3, 3, 3, 3],
}], {
hovermode: 'x unified',
width: '100%',
});
作为
jsfiddle和图像:
最佳答案
TL; 博士
这是最终产品,如下所示。看看我的逻辑如何使用 CSS 自定义属性来对抗 Plotly 的持久性。不幸的是,在深入研究 Plotly 的文档一段时间后,似乎没有使用 Plotly 的函数实现这一目标的简单或原生方法,但这不会阻止我们利用我们的知识来解决其生态系统的自然法则.
const testPlot = document.getElementById('test');
Plotly.newPlot('test', [{
line: { shape : 'vh' },
stackgroup: '1',
x: [1, 2, null, null],
y: [1, 1, null, null],
}, {
line: { shape : 'vh' },
stackgroup: '1',
x: [3, 4, null, null],
y: [2, 2, null, null],
}, {
line: { shape : 'vh' },
stackgroup: '1',
x: [3, 4, 5, 6],
y: [3, 3, 3, 3],
}], {
hovermode: 'x unified',
width: '100%'
});
const addPxToTransform = transform => 'translate(' + transform.replace(/[^\d,.]+/g, '').split(',').map(e => e + 'px').join(',') + ')';
['plotly_hover', 'plotly_click', 'plotly_legendclick', 'plotly_legenddoubleclick'].forEach(trigger => {
testPlot.on(trigger, function(data) {
if (document.querySelector('.hoverlayer > .legend text.legendtext')) {
const legend = document.querySelector('.hoverlayer > .legend');
legend.style.setProperty('--plotly-legend-transform', addPxToTransform(legend.getAttribute('transform')));
const legendTexts = Array.from(document.querySelectorAll('.hoverlayer > .legend text.legendtext'));
const legendTextGroups = Array.from(document.querySelectorAll('.hoverlayer > .legend text.legendtext')).map(text => text.parentNode);
const transformValues = [];
legendTexts.filter(label => (transformValues.push(addPxToTransform(label.parentNode.getAttribute('transform'))), label.textContent.includes(' : 0'))).forEach(zeroValue => zeroValue.parentNode.classList.add('zero-value'));
legendTextGroups.filter(g => !g.classList.contains('zero-value')).forEach((g,i) => g.style.setProperty('--plotly-transform', transformValues[i]));
const legendBG = document.querySelector('.hoverlayer > .legend > rect.bg');
legendBG.style.setProperty('--plotly-legend-bg-height', Math.floor(legendBG.nextElementSibling.getBBox().height + 10) + 'px');
}
});
});
.hoverlayer > .legend[style*="--plotly-legend-transform"] {
transform: var(--plotly-legend-transform) !important;
}
.hoverlayer > .legend > rect.bg[style*="--plotly-legend-bg-height"] {
height: var(--plotly-legend-bg-height) !important;
}
.hoverlayer > .legend [style*="--plotly-transform"] {
transform: var(--plotly-transform) !important;
}
.hoverlayer > .legend .zero-value {
display: none !important;
}
<div id="test"></div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
plotly_hover
事件,我正在取得进展。此事件的运行频率远低于
mousemove
所以它对浏览器来说不那么累人,而且它只在 Plotly JS 事件上运行,所以它真的只在你需要的时候运行,这样很方便。但是,我仍然遇到了一些闪烁。
transform
所有图例键的属性值,包括那些具有零值的键,然后在隐藏零值键之后,我遍历那些仍然可见的键,并使用我提到的自定义 CSS 属性重置它们的转换属性。如果我尝试内联添加 CSS,Plotly 会很快被覆盖,因此我使用了单独的样式表。
transform
存储在 SVG 属性中的值没有
px
在它们中,所以我编写了一个快速的正则表达式函数来清理它,然后在设置键上的自定义属性之前将这些更新的值推送到数组。即使迈出了这一步,我还没有走出困境。这允许我隐藏零值键并重新组织可见键以再次堆叠在顶部(摆脱尴尬的空白)但图例容器本身和图例框仍然有一点闪烁还是原来的高度。
transform
属性使用 CSS 自定义属性来保持它的持久性,即使 Plotly 试图适应我的更改并覆盖我的样式。然后,为了解决高度问题,在我所有其他逻辑的最后,我使用自定义 CSS 属性(您猜对了)重新计算了背景(一个单独的矩形元素)的高度。我们也不能使用
offsetHeight
或
clientHeight
在 SVG 元素上,所以我使用边界框计算高度
elem.getBBox().height
.
forEach()
循环遍历它们。 .我支持的事件处理程序是
plotly_hover
,
plotly_click
,
plotly_legendclick
, 和
plotly_legenddoubleclick
.
const testPlot = document.getElementById('test');
Plotly.newPlot('test', [{
line: { shape : 'vh' },
stackgroup: '1',
x: [1, 2, null, null],
y: [1, 1, null, null],
}, {
line: { shape : 'vh' },
stackgroup: '1',
x: [3, 4, null, null],
y: [2, 2, null, null],
}, {
line: { shape : 'vh' },
stackgroup: '1',
x: [3, 4, 5, 6],
y: [3, 3, 3, 3],
}], {
hovermode: 'x unified',
width: '100%'
});
const addPxToTransform = transform => 'translate(' + transform.replace(/[^\d,.]+/g, '').split(',').map(e => e + 'px').join(',') + ')';
['plotly_hover', 'plotly_click', 'plotly_legendclick', 'plotly_legenddoubleclick'].forEach(trigger => {
testPlot.on(trigger, function(data) {
if (document.querySelector('.hoverlayer > .legend text.legendtext')) {
const legend = document.querySelector('.hoverlayer > .legend');
legend.style.setProperty('--plotly-legend-transform', addPxToTransform(legend.getAttribute('transform')));
const legendTexts = Array.from(document.querySelectorAll('.hoverlayer > .legend text.legendtext'));
const legendTextGroups = Array.from(document.querySelectorAll('.hoverlayer > .legend text.legendtext')).map(text => text.parentNode);
const transformValues = [];
legendTexts.filter(label => (transformValues.push(addPxToTransform(label.parentNode.getAttribute('transform'))), label.textContent.includes(' : 0'))).forEach(zeroValue => zeroValue.parentNode.classList.add('zero-value'));
legendTextGroups.filter(g => !g.classList.contains('zero-value')).forEach((g,i) => g.style.setProperty('--plotly-transform', transformValues[i]));
const legendBG = document.querySelector('.hoverlayer > .legend > rect.bg');
legendBG.style.setProperty('--plotly-legend-bg-height', Math.floor(legendBG.nextElementSibling.getBBox().height + 10) + 'px');
}
});
});
.hoverlayer > .legend[style*="--plotly-legend-transform"] {
transform: var(--plotly-legend-transform) !important;
}
.hoverlayer > .legend > rect.bg[style*="--plotly-legend-bg-height"] {
height: var(--plotly-legend-bg-height) !important;
}
.hoverlayer > .legend [style*="--plotly-transform"] {
transform: var(--plotly-transform) !important;
}
.hoverlayer > .legend .zero-value {
display: none !important;
}
<div id="test"></div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
关于javascript - Plotly - 根据值隐藏悬停工具提示上的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67077685/
代码如下: http://jsfiddle.net/t2nite/KCY8g/ 我正在使用 jquery 创建这些隐藏框。 每个框都有一些文本和一个“显示”和“隐藏”按钮。我正在尝试创建一个“显示/隐
我正在尝试做某事。如果单击一个添加 #add-conferance 然后菜单将显示.add-contact。当点击隐藏然后它显示隐藏。我也将 setTimeout 设置为 7sec,但我希望当我的鼠标
我有一个多步骤(多页?)表单,只要用户按下“下一步”或“上一步”按钮,表单字段就会通过 div 显示和隐藏。 我只想禁用第一个 div (div id="page1"class="pageform")
我有一个使用 IIS 6 和 7 的当前系统,用 ASP.NET 和 .NET 4 中的 C# 编写。 My purpose is to hide the url completely (as per
我正在建立一个网站,并有一个幻灯片。幻灯片有标题和索引,覆盖整个页面。当覆盖被激活时,标题需要消失。当覆盖层被停用时,通过单击退出按钮、缩略图链接或菜单链接,字幕必须返回。 这就是我目前所拥有的
我正在尝试为显示/隐藏功能制作简单的 jquery 代码。但我仍然做错了什么。 $(document).ready(function(){ $('.arrow').click(function
我有一个自定义对话框并使用它来代替 optionMenu。所以我希望 myDialog 表现得像菜单,即在按下菜单时显示/隐藏。我尝试了很多变体,但结果相同: 因为我为 myDialog 设置了一个
在我的项目中,我通过 ViewPager 创建我的 tabBar,如下所示: MainActivity.java mViewPager = (ViewPager) findViewById(R.id.
我目前正在使用一个 Excel 表,我将第 1-17 行分组并在单元格 B18 中写入了一个单元格值。我想知道当我在展开/折叠行时单击 +/- 符号时是否有办法更改 B18 中的值。 例如:我希望 B
我想创建一个按钮来使用 VBA 隐藏和取消隐藏特定组。我拥有的代码将隐藏或取消隐藏指定级别中的所有组: Sub Macro1() ActiveSheet.Outline.ShowLevels RowL
我是 VBA 新手。我想隐藏从任何行到工作表末尾的所有行。 我遇到的问题是我不知道如何编程以隐藏最后写入的行。 我使用下一个函数知道最后写入的单元格,但我不知道在哪里放置隐藏函数。 last = Ra
我想根据另一个字段的条件在 UI 上隐藏或更新一个字段。 例如,如果我有一个名为 Color 的字段: [PXUIField(DisplayName="Color")] [PXStringList("
这是我尝试开始收集通常不会遇到的 GCC 特殊功能。这是@jlebedev 在另一个问题中提到g++的“有效C++”选项之后, -Weffc++ This option warns about C++
我开发了一个 Flutter 应用程序,我使用了 ProgressDialog小部件 ( progress_dialog: ^1.2.0 )。首先,我展示了 ProgressDialog小部件和一些代
我需要在 API 17+ 的同一个 Activity(Fragment) 中显示/隐藏状态栏。假设一个按钮将隐藏它,另一个按钮将显示它: 节目: getActivity().getWindow().s
是否可以通过组件的 ts 代码以编程方式控制下拉列表的显示/隐藏(使用 Angular2 清楚)- https://vmware.github.io/clarity/documentation/dro
我想根据 if 函数的结果隐藏/显示 NiceScroll。 在我的html中有三个部分,从左到右逐一滚动。 我的脚本如下: var section2 = $('#section2').offset(
我有这个 jquery 代码: $(document).ready(function(){ //global vars var searchBoxes = $(".box"); var searchB
这个问题已经有答案了: Does something like jQuery.toggle(boolean) exist? (5 个回答) 已关闭 6 年前。 在 jQuery 中(我当前使用的是 1
我在这样的选择标签上使用 jQuery 的 selectMenu。 $('#ddlReport').selectmenu() 在某些情况下我想隐藏它,但我不知道如何隐藏。 这不起作用: $('#ddl
我是一名优秀的程序员,十分优秀!