Cartojs4-具有多值甜甜圈图的标记

时间:2018-06-27 19:19:50

标签: mapping markerclusterer donut-chart cartodb cartocss

我想创建一个标记,就像我们在Carto SalesQuest产品中看到的那样。

根据CARTO提供的这篇文章,我从CARTOjs 4中制作了一个集群图

https://carto.com/blog/inside/tile-aggregation/

我可以使用CARTOCSS来设置图层样式,但是我想知道实现这样一个示例的最佳实践/方法是什么:请参见

Multivalues donut chart, cluster markers as seen in CARTO Salesquest

My own version of clusters with CARTO JS 4

我的代码如下(隐藏的API密钥)

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="initial-scale=1.0" />
    <meta charset="utf-8" />
    <!-- Include Carto.js -->
    <script src="https://cartodb-libs.global.ssl.fastly.net/carto.js/v4.0.2/carto.min.js"></script>
    <!-- Include Leaflet -->
    <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
    <link href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" rel="stylesheet" />
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        #map {
            position: absolute;
            height: 100%;
            width: 100%;
        }
    </style>
</head>

<body>
<div id="map"></div>

<script>
    const map = L.map('map').setView([30, 0], 3);
    L.tileLayer('http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png', {
        maxZoom: 18
    }).addTo(map);
    // define client
    const client = new carto.Client({
        apiKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        username: 'anagraph-clement'
    });
    // define source of data using a SQL query
    const source = new carto.source.SQL(`
            select *, 1 as count from pochesfils_carto
        `);
    // Aggregation option
    const aggregation = new carto.layer.Aggregation({
        threshold: 5,
        resolution: 32,
        placement: carto.layer.Aggregation.placement.SAMPLE,
        columns: {
            total_agg: {
                aggregateFunction: carto.layer.Aggregation.operation.SUM,
                aggregatedColumn: "count"
            }
        }
    });
    // define CartoCSS code to style data on map
    const style = new carto.style.CartoCSS(`
            #layer {
                marker-fill:  ramp([total_agg], (#3288bd, #99d594, #e6f598, #ffffbf, #fee08b, #fc8d59, #d53e4f), quantiles);
                marker-width: ramp([total_agg],  8 ,40 , quantiles);
                marker-line-color: ramp([total_agg], (#5F4690, #1D6996, #38A6A5, #0F8554, #73AF48, #EDAD08, #E17C05, #CC503E, #94346E, #6F4070, #666666), (5,10,20,30,50,66,75,100,150), "=", category);
                marker-line-width: 5;
                marker-line-opacity: 0.75;
            }
            #layer::labels {
                text-name: [total_agg];
                text-face-name: 'DejaVu Sans Book';
                text-size: 8;
                text-fill: #FFFFFF;
                text-label-position-tolerance: 0;
                text-halo-radius: 1;
                text-halo-fill: black;
                text-allow-overlap: true;
                text-placement: point;
                text-placement-type: dummy;
            }
        `);
    // create CARTO layer from source and style variables
    // and defining the interactivity of columns
    // when featureOver and featureClick events are executed
    const cartolayer = new carto.layer.Layer(source, style, { aggregation });
    // add CARTO layer to the client
    client.addLayer(cartolayer);
    // get tile from client and add them to the map object
    client.getLeafletLayer().addTo(map);
</script>
</body>

</html>

如果您对这种类型的标记创建有任何提示,请告诉我。 (svg?d3.js?chart.js?CARTOCSS + TurboCARTO?等...)

1 个答案:

答案 0 :(得分:0)

没有太多空间来改进样式,甜甜圈图需要去聚合的可视化数据中没有的去聚合数据。最重要的是,CartoCSS无法轻松渲染“甜甜圈部分”,因为这很可能涉及大量的三角计算,以设置角度来启动/停止图表的不同部分。 SalesQuest直接在加载GeoJSON数据的浏览器中使用传单插件和传统标记渲染,而不是使用CARTO Maps API。因此,要实现这种可视化,您需要使用SQL API呈现适当的数据,然后使用纯Leaflet编码将其呈现为标记。

一个快速的Google搜索给了我至少thisthis来启发自己。