通过缩放以编程方式在 Highcharts 中绘制矩形和线条
Programmatically draw rect and line in Highcharts with zoom
我正在使用 Highcharts.Renderer 使用 path()
和 rect()
在 Highcharts 中进行一些编程绘图。在下面的代码中,我手动绘制了直线和矩形的坐标。实际上,它们与主要数据系列(带值的日期)有关。
如何以编程方式绘制内容并进行缩放?
主图,缩放:
chart: {
zoomType: 'x',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
程序图:
chart.renderer.rect(100, 110, 100, 100, 5)
.attr({
'stroke-width': 2,
stroke: 'red',
fill: 'transparent',
zIndex: 3
})
.add();
var path = [
'M', 100, 100,
'L', 130, 110,
'L', 160, 105,
'L', 190, 150,
];
chart.renderer.path(path)
.attr({
'stroke-width': 2,
stroke: 'blue',
zIndex: 4
})
.add();
您可以在函数中使用 series.data
值,方法是:
chart.series[0].data[i].y
与i
系列中的点数。
那是你想做的吗?
现在你并没有真正使用你的图表值 - 你正在独立于你的系列绘制你的矩形和路径。您可以使用点 y 和 x 值和 Axis.toPixels() 方法将绘图与图表连接:http://api.highcharts.com/highcharts/Axis.toPixels
$(function() {
var addRect = function(chart) {
$('.rect').remove();
var xAxis = chart.xAxis[0],
yAxis = chart.yAxis[0]
chart.renderer.rect(xAxis.toPixels(1), 110, xAxis.toPixels(2) - xAxis.toPixels(1), 100, 5)
.attr({
'stroke-width': 2,
stroke: 'red',
fill: 'transparent',
zIndex: 0
}).addClass('rect')
.add();
chart.renderer.rect(0, 0, chart.plotLeft, chart.chartHeight + chart.plotTop, 5)
.attr({
fill: 'white',
zIndex: 0
}).addClass('rect')
.add();
};
$('#container').highcharts({
chart: {
zoomType: 'x',
events: {
redraw: function() {
addRect(this);
},
load: function() {
addRect(this);
}
}
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
在这里您可以看到它如何工作的示例:http://jsfiddle.net/n8ro1b9m/4/
我正在使用 Highcharts.Renderer 使用 path()
和 rect()
在 Highcharts 中进行一些编程绘图。在下面的代码中,我手动绘制了直线和矩形的坐标。实际上,它们与主要数据系列(带值的日期)有关。
如何以编程方式绘制内容并进行缩放?
主图,缩放:
chart: {
zoomType: 'x',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
程序图:
chart.renderer.rect(100, 110, 100, 100, 5)
.attr({
'stroke-width': 2,
stroke: 'red',
fill: 'transparent',
zIndex: 3
})
.add();
var path = [
'M', 100, 100,
'L', 130, 110,
'L', 160, 105,
'L', 190, 150,
];
chart.renderer.path(path)
.attr({
'stroke-width': 2,
stroke: 'blue',
zIndex: 4
})
.add();
您可以在函数中使用 series.data
值,方法是:
chart.series[0].data[i].y
与i
系列中的点数。
那是你想做的吗?
现在你并没有真正使用你的图表值 - 你正在独立于你的系列绘制你的矩形和路径。您可以使用点 y 和 x 值和 Axis.toPixels() 方法将绘图与图表连接:http://api.highcharts.com/highcharts/Axis.toPixels
$(function() {
var addRect = function(chart) {
$('.rect').remove();
var xAxis = chart.xAxis[0],
yAxis = chart.yAxis[0]
chart.renderer.rect(xAxis.toPixels(1), 110, xAxis.toPixels(2) - xAxis.toPixels(1), 100, 5)
.attr({
'stroke-width': 2,
stroke: 'red',
fill: 'transparent',
zIndex: 0
}).addClass('rect')
.add();
chart.renderer.rect(0, 0, chart.plotLeft, chart.chartHeight + chart.plotTop, 5)
.attr({
fill: 'white',
zIndex: 0
}).addClass('rect')
.add();
};
$('#container').highcharts({
chart: {
zoomType: 'x',
events: {
redraw: function() {
addRect(this);
},
load: function() {
addRect(this);
}
}
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
在这里您可以看到它如何工作的示例:http://jsfiddle.net/n8ro1b9m/4/