Gre*_*ver 5 javascript jquery highcharts media-queries highstock
是否有可能在打印布局中隐藏svg的部分内容.
特别是我喜欢隐藏highstock rangeSelector和navigator将打印页面.
这应该没有js triggert按钮.它应该在使用浏览器打印按钮时起作用.
是否有可能使用css media = print显示/隐藏元素并使用jquery绑定此事件?
需要隐藏在打印布局上的黄色部分:http: //i49.tinypic.com/24mbxop.png
对于这个例子:
$(function() {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
// Create the chart
window.chart = new Highcharts.StockChart({
chart : {
renderTo : 'container'
},
rangeSelector : {
selected : 1
},
title : {
text : 'AAPL Stock Price'
},
series : [{
name : 'AAPL',
data : data,
tooltip: {
valueDecimals: 2
}
}]
});
});
});
Run Code Online (Sandbox Code Playgroud)
@Bondye说的是什么.
创建类似的类
@media print {
.unprintable {
visibility: hidden;
}
}
Run Code Online (Sandbox Code Playgroud)
并将该类应用于您不想打印的svg元素
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<circle cx="50" cy="50" r="40" fill="red" />
<circle cx="150" cy="50" r="40" fill="red" />
<circle cx="50" cy="150" r="40" fill="blue" class="unprintable" />
<circle cx="150" cy="150" r="40" fill="red" />
</svg>
Run Code Online (Sandbox Code Playgroud)
而你尝试打印,蓝色圆圈将是隐形的.
如果visibility: hidden;不适合您,请尝试display: none;.
如果在绘制类时无法添加类,请在页面加载后使用Javascript添加该类.
你不能使用hide(),因为它也会从屏幕中删除元素.您必须打开一个新的选项卡/窗口并调用hide(),但正如问题中提到的,用户可以使用浏览器菜单进行打印.然后,您没有机会打开新的选项卡/窗口并调用hide().
因此,您必须在页面加载时添加.unprintable类.然后,在屏幕上显示所有内容,但在打印时,不会打印.unprintable元素.
如果你发布一个链接到网站,并告诉我你想隐藏什么,我可以帮助你编写JS代码,但它将是这样的:http://jsfiddle.net/EqDGQ/1/
$(function() {
$('svg circle[fill="blue"]').attr('class', 'unprintable');
});
Run Code Online (Sandbox Code Playgroud)
我编写了这个JS函数(需要jQuery),它将".unprintable"类添加到矩形区域内的所有svg元素:
setUnprintableArea = function(id, xMin, yMin, xMax, yMax, rightAligned) {
if (rightAligned) {
svgWidth = $('#'+id+' .highcharts-container svg')[0].getBoundingClientRect().width;
xMin += svgWidth;
xMax += svgWidth;
}
$('#'+id+' .highcharts-container svg *').filter(function() {
rect = this.getBoundingClientRect();
return (xMin <= rect.left && rect.right <= xMax &&
yMin <= rect.top && rect.bottom <= yMax);
}).attr('class', 'unprintable');
};
Run Code Online (Sandbox Code Playgroud)
你可以像这样调用这个函数:
setUnprintableArea('container', 15, 45, 240, 70); // Zoom
setUnprintableArea('container', -55, 15, 0, 40, true); // Top-right Buttons
setUnprintableArea('container', 0, 430, Number.MAX_VALUE, Number.MAX_VALUE); // Horiz Scroll Bar
Run Code Online (Sandbox Code Playgroud)
如果需要隐藏右对齐的内容,请将rightAlignedparam true设置为将y轴设置为svg的右边缘(意味着右边缘x = 0)并相应地调整xMin和xMax.
我把它放在小提琴上:http://jsfiddle.net/DXYne/1/
这可以解决吗?