Chartjs geo -示例美国Map在Map顶部打印所有要素

7y4bm7vi  于 2023-01-30  发布在  Chart.js
关注(0)|答案(2)|浏览(155)

我很难为chartjs geo包创建一个正常工作的例子:https://www.npmjs.com/package/chartjs-chart-geo/v/4.1.2
我使用Webpack与Laravel混合导入依赖项:

import Chart from 'chart.js/auto';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import { topojson, ChoroplethController, ProjectionScale, ColorScale, GeoFeature } from 'chartjs-chart-geo';

Chart.register(ChartDataLabels, ChoroplethController, ProjectionScale, ColorScale, GeoFeature);
Chart.defaults.set('plugins.datalabels', {
    color: 'white',
    textStrokeWidth: 2,
    textStrokeColor: '#363636',
    // textShadowBlur: 1,
    // textShadowColor: '363636',

    font: {
        family: 'lato, sans-serif',
        size: 16,
        weight: 600
    }

});

window.Chart = Chart;
window.topojson = topojson;

这是包裹

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production"
    },
    "devDependencies": {
        "laravel-mix": "^6.0.6"
    },
    "dependencies": {
        "chart.js": "^4.1.0",
        "chartjs-chart-geo": "^4.1.0",
        "chartjs-plugin-datalabels": "^2.1.0",
        "datamaps": "^0.5.9"
    }
}

下面是我尝试运行的代码:

<div  class="p-2 space-y-2 bg-white border-1 border-neutral-200 rounded-md shadow">
    <canvas class="container" id="testtesttest"></canvas>
</div>

<style>
    .container {
        height: 1200px;
        min-width: 310px;
        max-width: 800px;
        margin: 0 auto;
    }

    .loading {
        margin-top: 10em;
        text-align: center;
        color: gray;
    }
</style>
@push('scripts')
    <script>
        function test(){
            fetch('https://unpkg.com/us-atlas/states-10m.json').then((r) => r.json()).then((us) => {
                const nation = topojson.feature(us, us.objects.nation).features[0];
                let states = topojson.feature(us, us.objects.states).features;
                states = states.map((d) => ({feature: d, value: Math.random() * 10}));

                const chart = new Chart(document.getElementById("testtesttest").getContext("2d"), {
                    type: 'choropleth',
                    data: {
                        datasets: [{
                            data: states,
                        }]
                    }
                });
            });

        }

        test();

    </script>
@endpush

这将生成以下Map:

有人见过这个问题吗?我尝试过使用多个版本的chartjs和chartjs geo(尝试过4.1.2和3.9.0),但我似乎得到了相同的错误。我也尝试过渲染一个世界Map,它有完全相同的问题:

tmb3ates

tmb3ates1#

我以前没有使用过这个库,但我试图解决您的问题(我认为)。
这是我的方法:https://codepen.io/Cuchu/pen/PoBEXNx

    • 在我看来,您的示例中可能需要labels字段**
labels: statesFeatures.map((d) => d.properties.name)
or
labels: topojson.feature(us, us.objects.states).features.map...

从文档中,我发现这个repo包含不同的示例:https://github.com/sgratzl/chartjs-chart-geo/tree/main/samples.
我使用下面的例子来处理json数据:https://github.com/sgratzl/chartjs-chart-geo/blob/main/samples/earth.html

qybjjes1

qybjjes12#

感谢Maxi Schvindt,它触发了我查看标签。我已经打开了chartjs-plugin-datalabels插件。关闭后,它开始工作。

相关问题