jqPlot - 如何更改canvasOverlay的不透明度或z-index?

Kat*_*tya 5 jqplot

我想在我的图表上根据y轴值显示3个颜色区域,据我所知,我无法通过不同的颜色控制背景颜色.

我的想法是用canvasOverlay绘制3条水平线 - 这是有效的.问题是我想把这条线放在我的图形曲线后面,现在它在前面看到它覆盖了我的点线.

我可以更改z-index的属性或不透明度吗?

也许其他一些想法?

  $.jqplot( 'ChartDIV', [data],
        {
            series: [{ showMarker: true}],
            highlighter: {
                sizeAdjust: 10,
                show: true,
                tooltipLocation: 'n',
                useAxesFormatters: true
            },

            tickOptions: {
                formatString: '%d'
            },
            canvasOverlay: {
                show: true,
                objects: [ 
                            {
                                horizontalLine: 
                                {      
                                    name: 'low', 
                                    y: 1.0,
                                    lineWidth: 100,
                                    color: 'rgb(255, 0, 0)',
                                    shadow: false 
                                }
                            },      
                            {
                                horizontalLine:
                                { 
                                    name: 'medium',
                                    y: 2.0,
                                    lineWidth: 100, 
                                    color: 'rgb(250, 250, 0)', 
                                    shadow: true 
                                }
                            },
                            {
                                 horizontalLine:
                                {
                                    name: 'high',
                                    y: 3.0,
                                    lineWidth: 100,
                                    color: 'rgb(145, 213, 67)',
                                    shadow: false
                                }
                             },  
                        ]       
                    },
            axes: {
                xaxis:
                {
                    label: 'Dates',
                    renderer: $.jqplot.DateAxisRenderer,
                    rendererOptions: { tickRenderer: $.jqplot.CanvasAxisTickRenderer },
                    tickOptions: {
                        formatString: '%d/%m/%Y',
                        angle: -30,
                        fontFamily: 'Arial',
                        fontSize: '13px',
                        fontWeight: 'bold'
                    },
                    min: d[0] + "/" + d[1] + "/01", 
                    tickInterval: '2 month',
                    labelOptions: {
                        fontFamily: 'Arial',
                        fontSize: '14pt',
                        fontWeight: 'bold',
                        textColor: '#0070A3'
                    }
                },
                yaxis:
                {
                    label: 'Level',
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
                    tickOptions: {
                        formatter: $.jqplot.tickNumberFormatter
                    },
                    rendererOptions: { tickRenderer: $.jqplot.CanvasAxisTickRenderer },
                    labelOptions: {
                        fontFamily: 'Arial',
                        fontSize: '14pt',
                        fontWeight: 'bold',
                        textColor: '#0070A3',
                        angle: -90
                    }

                }
            }
        } );
Run Code Online (Sandbox Code Playgroud)

Bor*_*oro 8

我认为你的问题可能就是你画画的顺序.我认为你先创建图形,然后在其中绘制这条线,对吗?

因此,为了理清这一点,您可以尝试jqPlot图表提供的其中一个钩子.

要了解如何使用钩子,请参阅我的其他答案(BTW到我自己的问题:)我postDrawHooks在绘制图形时使用钩子来更改标签的格式.在你的情况下,你可以使用preDrawHooks或者更合适的是使用preDrawSeriesHooks,因为我不确定在调用传入的函数时是否准备好使用画布preDrawHooks.

请记住,根据文档,preDrawSeriesHooks每次绘制一个系列之前都会调用它,因此在您的情况下,您需要它只需要一次.

编辑

在这种情况下答案很简单,你可以做到这两点,这在我的jsfiddle中显示,可在这里找到.

你需要这段代码才能将叠加画布发送回来,你应该在绘制图形的代码之前放置它:

$.jqplot.postDrawHooks.push(function(){
    $(".jqplot-overlayCanvas-canvas").css('z-index', '0');//send overlay canvas to back
    $(".jqplot-series-canvas").css('z-index', '1');//send series canvas to front
});
Run Code Online (Sandbox Code Playgroud)

但是,当涉及到的不透明度,你可以把它应用到你喜欢的任何线(在我的代码所示),采用的rgba()方法,在系列是做这种方式:

seriesColors:['rgba(100, 150, 100, 0.75)']
Run Code Online (Sandbox Code Playgroud)

对于画布上的线条,你这样做:

color: 'rgba(145, 213, 67, 0.25)'
Run Code Online (Sandbox Code Playgroud)

EDIT2

最重要的想法被遗忘,因此以前的代码荧光笔不起作用.只是负责事件捕获和传播的事件画布隐藏在我们的画布下面.通过设置适当的代码,在当前版本的代码中对其进行了更正z-index.完整的方法如下:

$.jqplot.postDrawHooks.push(function() {
    $(".jqplot-overlayCanvas-canvas").css('z-index', '0'); //send overlay canvas to back  
    $(".jqplot-series-canvas").css('z-index', '1'); //send series canvas to front         
    $(".jqplot-highlighter-tooltip").css('z-index', '2'); //make sure the tooltip is over the series
    $(".jqplot-event-canvas").css('z-index', '5'); //must be on the very top since it is responsible for event catching and propagation
});
Run Code Online (Sandbox Code Playgroud)

编辑3: 一个更好的解决方案,我们不需要担心设置z-index.

$.jqplot.postDrawHooks.push(function() {
    var overlayCanvas = $($('.jqplot-overlayCanvas-canvas')[0])
    var seriesCanvas = $($('.jqplot-series-canvas')[0])
    seriesCanvas.detach();
    overlayCanvas.after(seriesCanvas);
});
Run Code Online (Sandbox Code Playgroud)

它在这里介绍.这个解决方案的灵感来自@Mark提供的类似问题的答案.