• [原创.数据可视化系列之八]使用等d3进行灰度图转伪彩色


    对灰度图进行彩色化是数据可视化中常见的需求,使用d3在客户端比较容易实现,本文使用d3生成图片,并显示:

    代码如下:

    代码中首先下载数据文件,然后设定d3的色带信息,生成一个空白的canvas元素,并对元素的,通过d3插值颜色,给每个点设定颜色信息,并显示出来:

     1 d3.request("data/geos/current/" + IdwPro[options.pro].file)
     2             .responseType("arraybuffer")
     3             .response(function (req) {
     4              var values=req;
     5             console.info(values);
     6  
     7             //色带信息。
     8             var color = d3.scaleLinear().domain([193, 206, 219, 233.15, 255.372, 273.15, 275.15, 291, 298, 311, 328])
     9             .range([d3.rgb(37, 4, 42), d3.rgb(41, 10, 130), d3.rgb(81, 40, 40),
    10                      d3.rgb(192, 37, 149), d3.rgb(70, 215, 215), d3.rgb(21, 84, 187),
    11                      d3.rgb(24, 132, 14), d3.rgb(247, 251, 59), d3.rgb(235, 167, 21),
    12                      d3.rgb(230, 71, 39), d3.rgb(88, 27, 67)])
    13             .interpolate(d3.interpolateHcl);
    14             //图片高度
    15             var width = 720,
    16                 height = 360;
    17  
    18             var _blankcanvas = document.createElement("canvas");
    19             _blankcanvas.width = width;
    20             _blankcanvas.height = height;
    21             var blankcanvas = _blankcanvas.getContext("2d").getImageData(0, 0, width, height);
    22             var _idwcontext = document.createElement("canvas");
    23             _idwcontext.width = width;
    24             _idwcontext.height = height;
    25  
    26             var idwcontext = _idwcontext.getContext("2d");
    27  
    28  
    29             var c, i = 0,
    30                 n = width * height * 4,
    31                 blankimg = blankcanvas.data;
    32  
    33             var d = new Uint8ClampedArray(n);
    34             //console.info(image.data);
    35             for (var i = 0; i < height; i++) {
    36                 var istar = width * i;
    37                 var istart = (height - i) * width;
    38                 for (var j = 0; j < width; j++) {
    39  
    40                     c = d3.rgb(color(values.blocks[0][(istar + j)]));
    41                     blankimg[(istart + j) * 4 + 0] = c.r;
    42                     blankimg[(istart + j) * 4 + 1] = c.g;
    43                     blankimg[(istart + j) * 4 + 2] = c.b;
    44                     blankimg[(istart + j) * 4 + 3] = 255;
    45                 }
    46             }
    47  
    48             idwcontext.putImageData(blankcanvas, 0, 0);
    49             //
    50              
    51           
    52         }).get();
  • 相关阅读:
    UVA
    UVA
    模板——扩展欧几里得算法(求ax+by=gcd的解)
    UVA
    模板——2.2 素数筛选和合数分解
    模板——素数筛选
    Educational Codeforces Round 46 (Rated for Div. 2)
    Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
    Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence
    Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
  • 原文地址:https://www.cnblogs.com/songsgroup/p/6055314.html
Copyright © 2020-2023  润新知