echarts icon indicating copy to clipboard operation
echarts copied to clipboard

桑基图自定义node颜色无效

Open realeve opened this issue 10 years ago • 6 comments

桑基图不能自定义NODE颜色

需求及问题说明

目前官方的桑基图边缘的颜色为统一指定或单独设置每个link的颜色,如果需要边缘设置为source或者target的颜色,则需要在node定义中先加入每个node的颜色,在link中设置即可,实现方法类似于:

links = [{
        "source": "源结点",
        "target": "目标结点",
        "value": 356,
            "lineStyle": {
                "normal": {
                    color:"#a22"
                }
            }
    }]
nodes =[{
        "name": "源结点",
            "itemStyle": {
                "normal": {
                    "color": "#a22"
                }
            }
}]

目前版本的eCharts中,设置nodes的item颜色无效,同时无法取到nodes实际设置的颜色,所以该需求通过设置配置项无法达成。

image

重现步骤 (Steps to reproduce)

1.官方文档 http://echarts.baidu.com/option.html#series-sankey.data.itemStyle.normal.color 中指出可以设置NODE的颜色; 2.测试地址 http://echarts.baidu.com/demo.html#sankey-energy 3.测试代码:

myChart.showLoading();
$.get('data/asset/data/energy.json', function (data) {
    myChart.hideLoading();
    data.nodes.map(function(node,idx){
        if(node.name == 'Nuclear'){
            console.log(idx);
        }
    })

    //设置左上角第一个NODE颜色为红色
    data.nodes[35].itemStyle = {
        normal:{
            color:"#FF0000"
        }
    }

    myChart.setOption(option = {
        title: {
            text: 'Sankey Diagram'
        },
        tooltip: {
            trigger: 'item',
            triggerOn: 'mousemove'
        },
        series: [
            {
                type: 'sankey',
                layout: 'none',
                data: data.nodes,
                links: data.links,
                itemStyle: {
                    normal: {
                        borderWidth: 1,
                        borderColor: '#aaa'
                    }
                },
                lineStyle: {
                    normal: {
                        curveness: 0.5,
                        opacity:0.4
                    }
                }
            }
        ]
    });
});

4.运行后NODE颜色不改变

第16行添加代码: data.links[0].value = 1000; 所有nodes颜色重绘,说明实际是按数值来控制各结点的颜色

期望结果 (Expected behaviour)

nodes的颜色可以用户手动指定,这样link的边缘颜色与node的源结点或目标结点能够一致,比如这样 image

可能哪里有问题 (What went wrong)

在源代码中 https://github.com/ecomfe/echarts/blob/master/src/chart/sankey/sankeyVisual.js 第25、26行,结点的颜色是用visualMap根据各link的值大小重新计算得到的,

var mapValueToColor = mapping.mapValueToVisual(node.getLayout().value);
                node.setVisual('color', mapValueToColor);

暂时的解决方案

1.node结点颜色采用visualMap计算的方案不变,修改各link在渲染时设置的颜色为源结点或目标结点的颜色 2.修改源代码文件:https://github.com/ecomfe/echarts/blob/master/src/chart/sankey/SankeyView.js

第6行增加 : var VisualMapping = require('../../visual/VisualMapping');

69行增加

            var nodes = graph.nodes;    
            nodes.sort(function (a, b) {
                return a.getLayout().value - b.getLayout().value;
            });

            var minValue = nodes[0].getLayout().value;
            var maxValue = nodes[nodes.length - 1].getLayout().value;

            var mapping = new VisualMapping({
                type: 'color',
                mappingMethod: 'linear',
                dataExtent: [minValue, maxValue],
                visual: seriesModel.get('color')
            });

155行修改为:

        var edgeLineStyle = lineStyleModel.getItemStyle();                                              
        var mapValueToColor = mapping.mapValueToVisual(n1Layout.value);
        edgeLineStyle.fill = mapValueToColor;                           
                curve.setStyle(edgeLineStyle);              
                //curve.setStyle(lineStyleModel.getItemStyle());

3.重新编译后,边缘颜色自动设置为源结点的颜色

建议

1.能否在edge颜色设置中增加配置项——edge颜色设置为 target或source的颜色或自定义颜色; 2.node结点的颜色自定义或采用series.color中设置的颜色或VisualMap设置的颜色。 3.桑基图能否设置排列的方向呢?目前为横向,能否自定义为纵向,比如 image

realeve avatar May 01 '16 17:05 realeve

能不能增加自定义节点顺序?

Darren-Li avatar Sep 30 '16 09:09 Darren-Li

谢谢!可以改note的颜色了,但能否单独改变每条link的颜色?即不简单由source或target的颜色决定,而想根据source和target之间变化的内容来改变link的颜色。求支持!

jhr621 avatar Jan 31 '18 13:01 jhr621

@jhr621 在这之后的版本中增加了color一项的配置可以定义每个link的颜色,具体去翻一下option手册。

realeve avatar Feb 01 '18 05:02 realeve

@realeve 谢谢回复!我在每个link后设置了linestyle,不起作用。手册没有详细说明,不知道具体该怎么设置?@pissang http://echarts.baidu.com/option.html#series-sankey.lineStyle.color

links = [{ "source": "源结点", "target": "目标结点", "value": 356, "lineStyle": { "normal": { color:"#FFFF00" } } }]

jhr621 avatar Feb 02 '18 01:02 jhr621

@jhr621

http://echarts.baidu.com/changelog.html#v3-2-0 中关于 桑基图优化的部分

option = {
	color: ['#145', '#4e5', '#45e'],
	series: {
		type: 'sankey',
		layout: 'none',
		data: [{
			name: 'a'
		}, {
			name: 'a1'
		}, {
			name: 'a2'
		}],
		links: [{
			source: 'a',
			target: 'a1',
			value: 5,
			lineStyle: {
				color: 'source'
			}
		}, {
			source: 'a',
			target: 'a2',
			value: 3,
			lineStyle: {
				color: '#f22',
				opacity:0.7
			}
		}]
	}
};

realeve avatar Feb 02 '18 08:02 realeve

@realeve That's fantastic! Thank you!

jhr621 avatar Feb 02 '18 12:02 jhr621