- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我设法弄清楚如何增强我在 stackoverflow 中找到的解决方案,以便将垂直线和水平线添加到 ChartJS v2+ 中的折线图以应用于气泡图 ChartJS Bubble w/Lines (我建议跳到最后的更新以获得更好的方法)
var originalBubbleDraw = Chart.controllers.bubble.prototype.draw;
Chart.helpers.extend(Chart.controllers.bubble.prototype, {
draw: function() {
originalBubbleDraw.apply(this, arguments);
var chart = this.chart;
var ctx = chart.chart.ctx;
var xaxis = chart.scales['x-axis-0'];
var yaxis = chart.scales['y-axis-0'];
var xvalue = chart.config.data.queryLimits['x'];
var yvalue = chart.config.data.queryLimits['y'];
var xcolor = chart.config.data.queryLimits['xcolor'];
var ycolor = chart.config.data.queryLimits['ycolor'];
var lineThickness = 3;
function drawLine(x1,y1,x2,y2,color) {
console.log("color="+color+", x1="+x1+", x2="+x2+", y1="+y1+", y2="+y2);
ctx.save();
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.strokeStyle = color;
ctx.lineWidth=lineThickness;
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.restore();
}
// draw vertical line
if (xvalue) {
x1 = xaxis.getPixelForValue(xvalue);
x2 = xaxis.getPixelForValue(xvalue);
y1 = yaxis.top;
y2 = yaxis.bottom;
drawLine(x1,y1,x2,y2,xcolor);
}
// draw horizontal line
if (yvalue) {
x1 = xaxis.left;
x2 = xaxis.right;
y1 = yaxis.getPixelForValue(yvalue);
y2 = yaxis.getPixelForValue(yvalue);
drawLine(x1,y1,x2,y2,ycolor);
}
}
});
var config = {
type: 'bubble',
data: {
queryLimits: {x: 42, y: 21, xcolor: '#00FF00', ycolor: '#0000ff'},
datasets: [
{
label: '',
data: [
{x: 20, y: 30, r: 15},
{x: 40, y: 10, r: 10},
{x: 100, y: 15, r: 10},
{x: 50, y: 22, r: 5},
{x: 80, y: 26, r: 3},
{x: 63, y: 28, r: 10},
{x: 71, y: 18, r: 12}
],
backgroundColor:"#FF6384",
hoverBackgroundColor: "#FF6384",
}]
}
};
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
我需要更进一步,在垂直线的一侧和水平线上方或下方用不同的颜色(如浅灰色或其他微妙的颜色)为图表的区域/背景着色。
我不确定该方法是尝试更改背景的一部分还是添加大小和位置的矩形来模拟背景阴影。
想法?
这是目标模型示例:
更新
对于将来观看此内容的人来说,我最终发现了一种更好的方法来绘制线条和矩形,方法是使用此处的 chartjs 注释插件 https://github.com/chartjs/chartjs-plugin-annotation .使用它要容易得多,并且不会产生不必要的触发代码来绘制线条和矩形的后果。此外,我最终能够使用另一个插件 https://github.com/compwright/chartjs-plugin-draggable用于拖动从第一个插件创建的注释。我将按原样保留已接受的答案,因为它确实回答了我从如何按照原始扩展方法解决矩形阴影区域的上下文中提出的问题,但是在了解更多相关信息之后,我现在推荐使用插件方法。
最佳答案
正如您对 queryLimits
属性所做的那样,您可以用相同的方式填充您想要的部分的属性。
通过将以下属性添加到您的图表数据集:
fillBackground: {
// In this porperty, add the string portion you want to fill
// Inputs are : "tr" for top-right
// "tl" for top-left
// "br" for bottom-right
// "bl" for bottom-left
pos: ["tr", "bl", "br"],
// A single color will be used in all the portions
// But you can also set an array of colors which must have the same length as the pos
// i.e color: ["rgba(0, 0, 0, 0.1)", "rgba(30, 30, 30, 0.3)", "rgba(60, 60, 60, 0.5)"]
color: "rgba(30, 30, 30, 0.15)"
}
然后在您的draw()
函数中添加以下代码:
function drawRect(x1, y1, x2, y2, color) {
ctx.save();
ctx.fillStyle = color;
ctx.fillRect(x1, y1, x2, y2, color);
ctx.restore();
}
// Checks if you have the attribute in your dataset
if (chart.config.data.fillBackground) {
// Make sure you have portions in your chart
if (!xvalue || !yvalue) return;
var pos = chart.config.data.fillBackground.pos;
var color = chart.config.data.fillBackground.color;
// For every position in your array ..
for (p in pos) {
// Based on the string code, fills the right portion
switch (pos[p]) {
case "tl":
drawRect(xaxis.left, yaxis.top, xaxis.getPixelForValue(xvalue) - lineThickness / 2 - xaxis.left, yaxis.getPixelForValue(yvalue) - lineThickness / 2 - yaxis.top, (Array.isArray(color)) ? color[p] : color);
break;
case "tr":
drawRect(xaxis.getPixelForValue(xvalue) + lineThickness / 2, yaxis.top, xaxis.right, yaxis.getPixelForValue(yvalue) - lineThickness / 2 - yaxis.top, (Array.isArray(color)) ? color[p] : color);
break;
case "bl":
drawRect(xaxis.left, yaxis.getPixelForValue(yvalue) + lineThickness / 2, xaxis.getPixelForValue(xvalue) - lineThickness / 2 - xaxis.left, yaxis.bottom - (yaxis.getPixelForValue(yvalue) + lineThickness / 2), (Array.isArray(color)) ? color[p] : color);
break
case "br":
drawRect(xaxis.getPixelForValue(xvalue) + lineThickness / 2, yaxis.getPixelForValue(yvalue) + lineThickness / 2, xaxis.right, yaxis.bottom - (yaxis.getPixelForValue(yvalue) + lineThickness / 2), (Array.isArray(color)) ? color[p] : color);
break;
}
}
}
这应该可以解决您的问题。
您可以在 this fiddle 中查看使用这些函数的示例,结果如下:
关于javascript - 如何对ChartJS气泡图的一部分背景进行阴影处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41861567/
我目前面临的挑战之一是使用 CSS 沿着图像中的山脉定位不同数量的圆圈/气泡。让我们假设图像看起来像这样(没有红色气泡): 图像始终必须使用设备的整个宽度(100% 宽度)。设备的分辨率差异很大(手机
我在 css3 中创建了一个气泡,但在某些浏览器(特别是 Windows Safari 5.1.7)中,我的跨度溢出了我的 anchor 标记,即使 1) 它是相对定位的并且 2) 具有静态宽度/高度
我使用 e.preventDefault(); 来禁止滚动页面到无效的 input。我试过 scrollintoview(false),默认情况下将网站滚动到顶部,但它没有做任何事情。 现在,为了将页
我可以制作这样的气泡图吗?' 我试试这个:Highcharts naming of individual bubbles但不起作用:/ 最佳答案 是的,你可以,你提到的所有帖子都指明了方向。基础知识是
我想使用 API v.3 自定义谷歌地图上信息窗口(信息气泡)中的关闭按钮。有简单的方法吗?这意味着 api 正在提供此功能,或者是否有一个好的解决方法。 感谢您的帮助。 更新 我必须补充一点,我想更
我需要帮助将标记(气泡)添加到简单的内容幻灯片中,这将帮助用户在 div 之间切换。 HTML: // etc CSS: #slideshow { position:a
我正在使用 Qtip2 来显示 ajax 请求的返回。我想仅当用户单击它时才显示它。所以我看到了 show: 'click',hide: 'click' 属性,但它不起作用。 我尝试了一些简单的方法,
我正在尝试更改气泡系列的背景和线条颜色。 目前默认为深蓝色边框的蓝色。我希望边框是白色的,气泡背景是透明的。关于如何实现这一目标的任何想法? 谢谢 雅克 最佳答案 您可以设置一些 plotOption
如何在 jQuery Mobile 中的图标(数据图标)顶部添加计数气泡或徽章。有没有更好的方法将它添加为小部件而不是使用 CSS 进行操作?我希望从服务器动态更新计数。 最佳答案 这是我的徽章图标版
我想知道是否有一种方法可以将 ToolTip 组件添加到气泡中,以显示 Avatar Group 组件中的其他头像。这是组件如何呈现的示例:(图片来自 Material UI 文档(https://m
我正在寻找 Jquery 中的插件,我可以在其中实现 float 气泡。例如,如果我输入数字 5,它必须显示 5 个气泡。气泡的大小也会根据我的输入而增长......我怎样才能实现这一点? 最佳答案
我正在为我的网站创建一个聊天框。我在其中使用 css3 来创建消息设计。我在使用 Firefox 进行设计时遇到问题,它在 Chrome 中运行良好。 这是图片-- FireFox 浏览器: Chro
jsfiddle:https://jsfiddle.net/leiming/5e6rtgwd/ class Sample extends React.Component { onInputFocu
QQ最新免费设置“天使圣殿”和“星云天使”气泡 QQ最新两款BUG气泡 “天使圣殿”和“星云天使” 现在限时免费设置! 透明的特别好看!好东西记得随手转发给朋友一起设置! 天使圣殿气泡:h
您好,我只是想知道我们是否可以使用 Javascript 捕获 Chrome 中的允许 Flash 事件,并自定义显示的消息以及单击允许或阻止的事件。 最佳答案 TL;DR; 否 这是内置的浏览器功能
我有一个网站,它从服务器端获取一组点,然后使用 azure-maps-control npm 包渲染 map 。 我希望 map 居中并缩放,以适合屏幕上的所有点。 实现这一目标的最佳方法是什么? 最
我有一个网站,它从服务器端获取一组点,然后使用 azure-maps-control npm 包渲染 map 。 我希望 map 居中并缩放,以适合屏幕上的所有点。 实现这一目标的最佳方法是什么? 最
我正在将一个 iPhone 应用程序移植到 WP7,其中包含一张带有多个标记/图钉的 map ,这些标记/图钉是我从网络服务(位置、图标和标题)获得的。 我已经设置了显示 map 所需的 XAML 以
我知道 pdMarker(http://www.pixeldevelopment.com/pdmarker.asp) 可以与 Maps API v2 完美配合以生成自定义工具提示和气泡。但是对于 V3
按照 http://bl.ocks.org/mbostock/4063269 上发布的示例开发 d3 气泡图: 我的数据集只有一个分支但有很多子节点(请参见下面的示例)。我所有的气泡都以相同的颜色显示
我是一名优秀的程序员,十分优秀!