【问题标题】:jqplot highlighting stacked column chartjqplot高亮堆积柱形图
【发布时间】:2012-12-05 17:52:27
【问题描述】:

我有一个堆积柱形图。 为了突出显示任何单个切片,我们可以使用“highlightColors”选项,但是如果我必须突出显示整个栏,是否有任何可用的选项。我当前的代码如下所示:

     seriesDefaults : {
        renderer : $.jqplot.BarRenderer,
        rendererOptions : {
            barWidth : this.barWidth,
            barMargin : this.barMargin,
            highlightColors : '#fffefe'
                 }

这需要进行修改,以使整个栏突出显示。请指教

【问题讨论】:

    标签: jquery charts jqplot


    【解决方案1】:

    我为它做了一些东西。 该解决方案涉及在高亮画布上进行自定义绘画。

    The sample is here.

    它已集成到我为一个问题制作的示例中,该问题询问如何在堆积条形图的顶部添加总和,details are here。我还升级了它的代码,所以现在它是“条形图方向友好”。

    以防万一JS代码也在下面:

    $(document).ready(function() {
        var ins = [2, 2, 3, 5];
        var outs = [2, 4, 3, 5];
        var swaps = [2, 2, 6, 5];
        var passes = [2, 4, 6, 5];
        var data = [ins, outs, swaps, passes, [3, 3, 3, 3]];
        var series = [
            {
            label: 'IN',
            pointLabels: {
                labels: [2, 2, 3, 5]
            }},
        {
            label: 'OUT',
            pointLabels: {
                labels: [2, 4, 3, 5]
            }},
        {
            label: 'SWAP',
            pointLabels: {
                labels: [2, 2, 6, 5]
            }},
        {
            label: 'PASS',
            pointLabels: {
                labels: [2, 4, 6, 5]
            }},
        {
            label: 'INVISIBLE',
            pointLabels: {
                labels: ['∑ 8', '∑ 12', '∑ 18', '∑ 20']
            },
            show: false,
            shadowAngle: 90,//for horizontal use (180 istead of 90)
            rendererOptions: {
                shadowDepth: 25,
                shadowOffset: 2.5,
                shadowAlpha: 0.01
            }}
        ];
        var ticks = ['Oi', 'Bike', 'Car', 'Truck'];
        var plot = $.jqplot('chart', data, {
            stackSeries: true,
            seriesDefaults: {
                renderer: $.jqplot.BarRenderer,
                rendererOptions: {
                    barMargin: 20
    //for horizontal uncomment this                
    //                ,barDirection: 'horizontal'
                },
                pointLabels: {
                    show: true,
                    stackedValue: false,
                    location: 's'
                }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
                }
    //for horiozontal use the below instead
    /*            
                yaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
                }
    */        
            },
            legend: {
                show: true,
                location: 'ne',
                placement: 'outside'
            },
            series: series,
            title: "Oi title"
        });
        //color used for tooltip title
        var color = 'rgb(50%,50%,100%)';
        //start span of a tooltip's title
        var spanStart = '<span style="font-size:14px;font-weight:bold;color:' + color + ';">';
        $('#chart').bind('jqplotDataHighlight', function(ev, seriesIndex, pointIndex, data) {
            var chart_left = $('#chart').offset().left;
            var chart_top = $('#chart').offset().top;
            var x = ev.pageX;
            var y = ev.pageY;
            var left = x;
            var top = y;
            var chartTooltipHTML = spanStart;
            if (plot.axes.xaxis.u2p && plot.axes.yaxis.u2p) { // pierenderer do not have u2p
                left = chart_left + plot.axes.xaxis.u2p(data[0]); // convert x axis units to pixels on grid
                top = chart_top + plot.axes.yaxis.u2p(data[1]); // convert y axis units to pixels on grid
            }        
            var barWidth = plot.series[0].barWidth;
            //tells if the bar chart is vertical
            var isVertical = false;
            if (plot.series[0].barDirection === "vertical")
                isVertical = true;
            if (isVertical) left -= barWidth / 2;
            else top -= barWidth / 2;
            //for stacked chart
            if(isVertical){
                top = chart_top;
                var sum = 0;
                for (var i = 0; i < seriesIndex + 1; i++)
                   sum += plot.series[i].data[pointIndex][1];
                top += plot.axes.yaxis.u2p(sum); 
            }else{
                left = chart_left;
                var sum = 0;
                for (var i = 0; i < seriesIndex + 1; i++)
                   sum += plot.series[i].data[pointIndex][0];
                left += plot.axes.xaxis.u2p(sum); 
            }        
            var seriesName = plot.series[seriesIndex].label;
            console.log("seriesName = " + seriesName + "   seriesIndex = " + seriesIndex + "   pointIndex= " + pointIndex + "   data= "+data+ "   plot.series[seriesIndex].data= " + plot.series[seriesIndex].data[pointIndex]);
    
            chartTooltipHTML += 'My custom tooltip: </span>' 
            + '<br/><b>Count:</b> ' + data[1] //data[1] has count of movements
            + '<br/><b>Movement type:</b> ' + seriesName;
    
            //start of part highlighting whole bar --- all bars (other than this bar is related to custom tooltip)
            //for painting just grab the first highlight canvas as there could be 'n' of them where 'n' is the number of series, I think
            var drawingCanvas = $(".jqplot-barRenderer-highlight-canvas")[0];
            var rX = chart_left + plot.axes.xaxis.u2p(data[0]) - $(drawingCanvas).offset().left - barWidth/2;
            var rY = chart_top + plot.axes.yaxis.u2p(data[1]) - $(drawingCanvas).offset().top - barWidth/2;
            var rW = 0;
            var rH = barWidth;        
            if (isVertical){
                rW = rH;
                rH = 0;
            }
            for(var i = 0; i < plot.series.length; i++){
                if (isVertical) {
                    rH += plot.series[i].data[pointIndex][1];
                }else{
                    rW += plot.series[i].data[pointIndex][0];
                }
            }  
            var canvasLeft = parseInt($(drawingCanvas).css('left'),10);
            var canvasTop = parseInt($(drawingCanvas).css('top'),10);
            if(isVertical){
               rY = plot.axes.yaxis.u2p(rH) - canvasTop;//- $(drawingCanvas).offset().top;
               rH = drawingCanvas.height - plot.axes.yaxis.u2p(rH) + canvasTop;//$(drawingCanvas).css('top').; 
            }else{
               rX = 0; 
               rW = plot.axes.xaxis.u2p(rW) - canvasLeft;  
            }       
            console.log("rX= "+rX+";  rY= "+rY+";  rW= "+rW+";  rH = "+ rH + "  drawingCanvas.height= "+drawingCanvas.height);
            var context = drawingCanvas.getContext('2d');
            context.clearRect(0, 0, drawingCanvas.width, drawingCanvas.height);                  context.strokeStyle = "#000000";
            context.strokeRect(rX, rY, rW, rH);
            //end of part doing custom painting of frame around all bars              
    
            var ct = $('#chartTooltip');
            ct.css({
                left: left,
                top: top
            }).html(chartTooltipHTML).show();
            if (plot.series[0].barDirection === "vertical") {
                var totalH = ct.height() + ct.padding().top + ct.padding().bottom + ct.border().top + ct.border().bottom;
                ct.css({
                    top: top - totalH
                });
            }
        });
        // Bind a function to the unhighlight event to clean up after highlighting.
        $('#chart').bind('jqplotDataUnhighlight', function(ev, seriesIndex, pointIndex, data) {
            $('#chartTooltip').empty().hide();
        });
    });​
    

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多