如何使用jqplot和堆积条形图更改某一特定条的颜色

Dario CarŠagud

我有一个直截了当的问题。是否可以通过任何方式(使用jqplot选项或依靠hack)更改堆积条形图中一个条形的颜色?

我有这个:

在此处输入图片说明

我要这个:

在此处输入图片说明

因此,您已经可以假设我在堆叠条形图中使用3种不同的颜色:

seriesColors: ['#afafaf', '#c4c6c4', '#dbdcdd']

问题是我想为1个特定的条添加一种特定的颜色。

这是JS代码:

$(document).ready(
    function() {

        var el = [ 3, 6, 0, 10, 12 ];
        var ael = [ 14, 5, 0, 4, 2 ];
        var ipv = [ 4, 9, 0, 8, 4 ];
        var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May' ];
        var colors = ['blue', 'red', 'white'];

        plot3 = $.jqplot('elDiagram', [ el, ael, ipv ], {
            stackSeries : true,
            seriesColors: colors,
            captureRightClick : true,
            seriesDefaults : {
                renderer : $.jqplot.BarRenderer,
                rendererOptions : {
                    barMargin : 30,
                    varyBarColor : true,
                    highlightMouseDown : true,
                    barWidth: 60
                },
                pointLabels : {
                    show : true
                }
            },
            axes : {
                xaxis : {
                    renderer : $.jqplot.CategoryAxisRenderer,
                    ticks : months,
                    tickOptions : {
                        mark : 'outside'
                    }
                },
                yaxis : {
                    tickOptions : {
                        show : false
                    },
                    padMin : 0
                }
            },
            series : [ {
                label : 'bla1'
            }, {
                label : 'bla2'
            }, {
                label : 'bla3'
            } ],
            legend : {
                show : true,
                location : 'ne',
                placement : 'inside'
            }
        });     
    });

谢谢!

Dario CarŠagud

好的,解决方案是一个hack,我将在此处显示和描述:

  1. 您需要覆盖名为$ .jqplot.BarRenderer.prototype.draw的函数并更改某些行
  2. 您需要覆盖名为getStart(sidx,didx,comp,plot,axis)的函数
  3. 您需要覆盖名为$ .jqplot.ShapeRenderer.prototype.draw的函数并更改某些行

1 .:

$.jqplot.BarRenderer.prototype.draw = function(ctx, gridData, options, plot) {
   var i;
   // Ughhh, have to make a copy of options b/c it may be
   // modified later.
   var opts = $.extend({}, options);

   .................................
   <other code>
   .................................

   var clr = opts.fillStyle || this.color;
   this._dataColors.push(clr);
   this.renderer.shapeRenderer.draw(ctx, points, opts, i, pos); // changed line

我以将i和pos参数添加到函数中的方式更改了该行。原因是要指示当前的条和条中的位置。

2 .:

function getStart(sidx, didx, comp, plot, axis) {
   // check if sign change
   var seriesIndex = sidx, prevSeriesIndex = sidx - 1, start, prevVal, aidx = (axis === 'x') ? 0 : 1;
   // is this not the first series?
   if (seriesIndex > 0) {
       prevVal = plot.series[prevSeriesIndex]._plotData[didx][aidx];
       // is there a sign change
       if ((comp * prevVal) < 0) {
           start = getStart(prevSeriesIndex, didx, comp, plot, axis);
       }
       // no sign change.
       else {
           start = plot.series[prevSeriesIndex].gridData[didx][aidx];
       }
   }
   // if first series, return value at 0
   else {
       start = (aidx === 0) ? plot.series[seriesIndex]._xaxis.series_u2p(0) : plot.series[seriesIndex]._yaxis.series_u2p(0);
   }
   return start;
}

这里什么都没有改变。您只需要复制函数,因为新的覆盖函数无法从jQPlot库中使用它。

3 .:

$.jqplot.ShapeRenderer.prototype.draw = function(ctx, points, options, currentBar, position) {
    ctx.save();
    var opts = (options != null) ? options : {};
    var fill = (opts.fill != null) ? opts.fill : this.fill;
    var closePath = (opts.closePath != null) ? opts.closePath : this.closePath;
    var fillRect = (opts.fillRect != null) ? opts.fillRect : this.fillRect;
    var strokeRect = (opts.strokeRect != null) ? opts.strokeRect
            : this.strokeRect;
    var clearRect = (opts.clearRect != null) ? opts.clearRect : this.clearRect;
    var isarc = (opts.isarc != null) ? opts.isarc : this.isarc;
    var linePattern = (opts.linePattern != null) ? opts.linePattern
            : this.linePattern;
    var ctxPattern = $.jqplot.LinePattern(ctx, linePattern);
    ctx.lineWidth = opts.lineWidth || this.lineWidth;
    ctx.lineJoin = opts.lineJoin || this.lineJoin;
    ctx.lineCap = opts.lineCap || this.lineCap;
    ctx.strokeStyle = (opts.strokeStyle || opts.color) || this.strokeStyle;
    ctx.fillStyle = opts.fillStyle || this.fillStyle;
    if (currentBar == activeColumn && position == 0) { // adding different color for the specific bar
        ctx.fillStyle = defaultColors[0];
    } else if (currentBar == activeColumn && position == 1) {
        ctx.fillStyle = defaultColors[1];
    } else if (currentBar == activeColumn && position == 2) {
        ctx.fillStyle = defaultColors[2];
    }
    ctx.beginPath();
    if (isarc) {
        ctx.arc(points[0], points[1], points[2], points[3], points[4], true);
        if (closePath) {
            ctx.closePath();
        }
        if (fill) {
            ctx.fill();
        } else {
            ctx.stroke();
        }
        ctx.restore();
        return;
    } else if (clearRect) {
        ctx.clearRect(points[0], points[1], points[2], points[3]);
        ctx.restore();
        return;
    } else if (fillRect || strokeRect) {
        if (fillRect) {
            ctx.fillRect(points[0], points[1], points[2], points[3]);
        }
        if (strokeRect) {
            ctx.strokeRect(points[0], points[1], points[2], points[3]);
            ctx.restore();
            return;
        }
    } else if (points && points.length) {
        var move = true;
        for ( var i = 0; i < points.length; i++) {
            // skip to the first non-null point and move to it.
            if (points[i][0] != null && points[i][1] != null) {
                if (move) {
                    ctxPattern.moveTo(points[i][0], points[i][1]);
                    move = false;
                } else {
                    ctxPattern.lineTo(points[i][0], points[i][1]);
                }
            } else {
                move = true;
            }
        }
        if (closePath) {
            ctxPattern.closePath();
        }
        if (fill) {
            ctx.fill();
        } else {
            ctx.stroke();
        }
    }
    ctx.restore();
};

在这里,您需要检查当前所在的酒吧是否为默认酒吧。该代码的重要部分是:

if (currentBar == activeColumn && position == 0) { // adding different color for the specific bar
    ctx.fillStyle = defaultColors[0];
} else if (currentBar == activeColumn && position == 1) {
    ctx.fillStyle = defaultColors[1];
} else if (currentBar == activeColumn && position == 2) {
    ctx.fillStyle = defaultColors[2];
}

我为该酒吧添加了3种不同的颜色,只是为了获得“更漂亮”的图:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

更改特定变量R ggplot堆积条形图的颜色

来自分类Dev

如何使用ggplot2更改堆积条形图的顺序和配色方案?

来自分类Dev

堆积的条形图图例问题jqplot

来自分类Dev

堆积的条形图图例问题jqplot

来自分类Dev

R ggplot2条形图更改特定条形的颜色

来自分类Dev

如何在Google Charts API中更改堆积条形图的部分颜色?

来自分类Dev

如何更改Google Charts API中堆积的条形图的部分颜色?

来自分类Dev

如何使用Python-IDE Jupiter Notebook更改条形图中1个特定条的颜色

来自分类Dev

组合堆积图和条形图

来自分类Dev

JQPLOT堆积条形图数组输入问题

来自分类Dev

jqPlot堆积条形图显示为图表外

来自分类Dev

Python Pandas条形图-更改特定条形的颜色

来自分类Dev

使用data.table和plotly堆积条形图

来自分类Dev

使用data.table和plotly堆积条形图

来自分类Dev

如何使用hvplot绘制堆积的条形图?

来自分类Dev

堆积条形图

来自分类Dev

堆积条形图

来自分类Dev

堆积条形图

来自分类Dev

一栏堆积条形图

来自分类Dev

如何缩放ggplot堆积的条形图

来自分类Dev

如何水平显示堆积的条形图?

来自分类Dev

使用Achartengine的堆积条形图

来自分类Dev

仅使用数字的堆积条形图

来自分类Dev

使用Achartengine的堆积条形图

来自分类Dev

使用JavaScript的堆积条形图

来自分类Dev

使用ggplot2更改条形图中特定条的颜色

来自分类Dev

ggplot从堆积条形图中更改一个条形的颜色

来自分类Dev

如何使用geom_errorbar在堆积条形图中堆积误差条?

来自分类Dev

如何在Highcharts条形图中更改特定的条形颜色?