5

I would like to show 3 color zones on my graph on the background according to y axis value, as I understand, I cannot control the background color by different colors.

My idea is to draw 3 horizontal lines with canvasOverlay - that is working. The problem is I want to place this lines behind my graph curve, now it seen on the front and it overlays my points line.

Can I change the property of z-index or the opacity?

Maybe some other ideas?

  $.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
                    }

                }
            }
        } );
Katya
  • 125
  • 1
  • 2
  • 8
  • I am not sure how and where exactly do you paint it, a code sample would be useful. Could you provide one, probably at http://jsfiddle.net/ ? – Boro May 03 '12 at 08:30
  • Please check out my **EDIT**. My recommendation for you for future is to use `jsfiddle` instead of just code snippets. This seriously saves time to one answering plus you and answering person are sure the code shows your problem accurately. In this one I had to add my data etc had to scrap some of it as well. – Boro May 03 '12 at 11:42
  • BTW what is `formatter: $.jqplot.tickNumberFormatter` in `tickOptions` for? I have never seen this one and couldn't find it on the net. Also since it is a class its first letter might have to be capital. Which plugin does it belongs to? – Boro May 03 '12 at 11:43
  • I'm sorry. I forgot about this - it's my own formatter for y axis. Thanks about jsfiddle - I didn't know about it. – Katya May 03 '12 at 12:25

2 Answers2

8

I think that your problem might be the order in which you do your painting. I think that you first create the graph and then in it you draw this line, right?

Thus to sort out this you might try one of the hooks the jqPlot chart provides.

To see how you could use a hook, please see my other answer (BTW to my own question:) where I used a postDrawHooks hook to change format of labels once the graph is drawn. In your case you could use preDrawHooks or maybe more appropriate would be to use preDrawSeriesHooks, since I am not sure if a canvas is ready to use when function passed in preDrawHooks is called.

Remember that, according to the documentation, the preDrawSeriesHooks is called each time before a series is drawn, thus in your case you would need it to work just once.

EDIT

In this case the answer is simple, well you could do both, which is shown in my jsfiddle, available here.

You need this piece of code to send overlay canvas to back, which you should place before the code painting your graph:

$.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
});

But when it comes to opacity you could apply it to whichever line you like (also shown in my code), using of the rgba() method, for series it is done this way:

seriesColors:['rgba(100, 150, 100, 0.75)']

for the lines on canvas, you do it like this:

color: 'rgba(145, 213, 67, 0.25)'

EDIT2

The most important think was forgotten therefore with the previous code the highlighter was not working. Simply the event canvas which is responsible for event catching and propagation was hidden underneath our canvas. It was corrected in the current version of code, by setting of an appropriate z-index for it. The complete method would look like:

$.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
});

EDIT3: A much nicer solution where we do not need to worry about setting the z-index.

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

It is presented here. This solution is inspired by the answer provided by @Mark to a similar sort of problem.

Boro
  • 7,913
  • 4
  • 43
  • 85
  • Hi Boro, one last question - now I don't see the highlighter tooltip when I'm clicking on the point. If I cancel the $(".jqplot-series-canvas").css('z-index', '1'); line it's working but then the z-index of the line is not good. – Katya May 03 '12 at 14:24
  • My quick suggestions would be to try: 1. Setting appropriate `z-index` also for the highlighter tooltip. Or 2. Trying to set appropriate negative values for the other two canvas, as I think that a default `z-index` of an element is zero then it only matters the order in which they are added to a page. – Boro May 03 '12 at 14:27
  • Hi Boro, I tried a lot but I just can't get it working. Please HELP!!! that what I did : $( ".jqplot-overlayCanvas-canvas" ).css( 'z-index', '0' ); $( ".jqplot-series-canvas" ).css( 'z-index', '1' ); $( 'jqplot-highlight-canvas' ).css( 'z-index', '10' ); $( 'jqplot-highlighter-tooltip' ).css( 'z-index', '20' ); – Katya May 06 '12 at 06:34
  • Hi Katya. I will take a look at it. In the mean time I think you should consider making a jsfiddle with your problem and putting it as new question, maybe referring to this question as well. This might get others' attention to your problem. – Boro May 07 '12 at 11:28
  • Thanks Boro, I added all the code to jsfiddle, you can see it running on: http://jsfiddle.net/buzyonok/WdLnm/5/ – Katya May 08 '12 at 13:29
  • I did something about it in my current version of code. Please see EDIT2. The only think what bothers me is why in my case highlighter's tooltip is above where in yours it is under the series and I am using a very similar code? – Boro May 08 '12 at 13:41
  • Well in my case it was also showing under just the series transparency made it look as if it was over. Please see my code now or for your code see http://jsfiddle.net/WdLnm/7/ – Boro May 08 '12 at 13:47
  • Hi Boro, THANK YOU SO MUCH, you helped me a lot! I missed the jqplot-event-canvas class. I wish would me more people like you, you really helped me. It was just driving me crazy. Now my project manager will be very satisfied :) – Katya May 09 '12 at 05:44
  • No worries @Katya pleasure to help. Let's hope that maybe one day you will help someone else too. All the best :) – Boro May 10 '12 at 12:32
  • Katya please see **EDIT3** for a neater solution which I have noticed in [other question](http://stackoverflow.com/q/11216263/613495), check out @Mark 's answer there. – Boro Jun 28 '12 at 08:41
  • I highly recommend EDIT3 as EDIT2 breaks the use of the enhancedLegendRenderer. Some canvas z value is out of whack as you can't click the legend to filter anymore. I imagine other plugins will suffer too. – Olmstov Feb 24 '18 at 00:05
1

A much better solution is to use Canvas rectangle object without any hacking http://services.mbi.ucla.edu/jqplot/examples/draw-rectangles.html

$(document).ready(function(){
  var plot1 = $.jqplot ('chart1', [[30,-10,90,20,50,130,80,120,50]], {
      canvasOverlay: {
        show: true,
        objects: [
          { rectangle: { ymax: 0, xminOffset: "0px", xmaxOffset: "0px", yminOffset: "0px", ymaxOffset: "0px",
                    color: "rgba(0, 0, 200, 0.3)", showTooltip: true, tooltipFormatString: "Too Cold" } },
          { rectangle: { ymin: 100, xminOffset: "0px", xmaxOffset: "0px", yminOffset: "0px", ymaxOffset: "0px",
                    color: "rgba(200, 0, 0, 0.3)", showTooltip: true, tooltipFormatString: "Too Warm" } }
        ]
      } 
  });
});
devXen
  • 3,013
  • 3
  • 35
  • 44