如何反转来自PowerBI模式的颜色顺序

huangapple go评论77阅读模式
英文:

How to reverse the order of colors coming from PowerBI schema

问题

我在Deneb中使用pbiColorNominal来为自定义堆叠条形图着色。与类似的默认堆叠条形图视觉相比,此类图表中的颜色顺序是相反的。

是否有一种方法可以颠倒通过pbiColorNominal导入的颜色顺序?还有其他解决方法吗?

英文:

I use pbiColorNominal in the Deneb for coloring a custom stacked bar chart. The order of colors in such chart is reversed comparing to a similar out-of-the box stacked bar visual.

Is there a way to reverse the order of colors imported via pbiColorNominal? Any other workarounds?

答案1

得分: 0

可以使用reverse()函数来颠倒传递给pbiColorNominal的颜色值数组的顺序,从而颠倒自定义堆叠条形图中颜色的顺序。请参见以下示例,展示了如何颠倒颜色的顺序:

let colors = ['#FF0000', '#00FF00', '#0000FF'];
let colorScale = visuals.colors.createColorScale('categorical', { colors: colors.reverse() });

visuals.colors.SQExprUtils.exprFromResultName('Category')
    .getMetadata({ kind: SQExprKind.Column })
    .then(columnMetadata => {
        let colorExpr = visuals.createStaticEval('Category', columnMetadata);
        let colorFunc = colorScale.getKeyColor.bind(colorScale);
        let colorExprVal = visuals.converterHelper.colorExpressionRewriter(colorExpr.expr, colorFunc);
        let colorExprRewritten = { expr: colorExprVal, source: 'Color' };

        let stackedBarChart = new visuals.StackedBarChart('divId', colorExprRewritten);
        // 根据需要使用堆叠条形图
    });

在上面的示例中,colors数组在传递给pbiColorNominal之前使用reverse()函数进行了颠倒。

英文:

Yes, you can reverse the order of colors in a custom stacked bar chart that uses pbiColorNominal by using the reverse() function to reverse the order of the color values in the array that is passed to pbiColorNominal.

Please see the example below, which show how you can reverse the order of colors:

let colors = ['#FF0000', '#00FF00', '#0000FF'];
let colorScale = visuals.colors.createColorScale('categorical', { colors: colors.reverse() });

visuals.colors.SQExprUtils.exprFromResultName('Category')
    .getMetadata({ kind: SQExprKind.Column })
    .then(columnMetadata => {
        let colorExpr = visuals.createStaticEval('Category', columnMetadata);
        let colorFunc = colorScale.getKeyColor.bind(colorScale);
        let colorExprVal = visuals.converterHelper.colorExpressionRewriter(colorExpr.expr, colorFunc);
        let colorExprRewritten = { expr: colorExprVal, source: 'Color' };

        let stackedBarChart = new visuals.StackedBarChart('divId', colorExprRewritten);
        // Use the stacked bar chart as desired
    });

The colors array, in the example above, is reversed using the reverse() function before being passed to pbiColorNominal.

答案2

得分: 0

无法打开您的 .pbix 文件,但从屏幕截图来看,颜色似乎已正确分配,但堆栈顺序不同。要对堆栈中的元素进行排序,请使用"order"属性,如此处所述:https://vega.github.io/vega-lite/docs/stack.html#sorting-stack-order

英文:

I can't open your .pbix but from the screenshots, it looks like colours are assigned correctly but the stack is in a different order. To order the elements in a stack, use the order attribute as described here: https://vega.github.io/vega-lite/docs/stack.html#sorting-stack-order

huangapple
  • 本文由 发表于 2023年3月1日 14:59:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定