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

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

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

  1. import Chart from 'chart.js/auto';
  2. import ChartDataLabels from 'chartjs-plugin-datalabels';
  3. import { topojson, ChoroplethController, ProjectionScale, ColorScale, GeoFeature } from 'chartjs-chart-geo';
  4. Chart.register(ChartDataLabels, ChoroplethController, ProjectionScale, ColorScale, GeoFeature);
  5. Chart.defaults.set('plugins.datalabels', {
  6. color: 'white',
  7. textStrokeWidth: 2,
  8. textStrokeColor: '#363636',
  9. // textShadowBlur: 1,
  10. // textShadowColor: '363636',
  11. font: {
  12. family: 'lato, sans-serif',
  13. size: 16,
  14. weight: 600
  15. }
  16. });
  17. window.Chart = Chart;
  18. window.topojson = topojson;

这是包裹

  1. {
  2. "private": true,
  3. "scripts": {
  4. "dev": "npm run development",
  5. "development": "mix",
  6. "watch": "mix watch",
  7. "watch-poll": "mix watch -- --watch-options-poll=1000",
  8. "hot": "mix watch --hot",
  9. "prod": "npm run production",
  10. "production": "mix --production"
  11. },
  12. "devDependencies": {
  13. "laravel-mix": "^6.0.6"
  14. },
  15. "dependencies": {
  16. "chart.js": "^4.1.0",
  17. "chartjs-chart-geo": "^4.1.0",
  18. "chartjs-plugin-datalabels": "^2.1.0",
  19. "datamaps": "^0.5.9"
  20. }
  21. }

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

  1. <div class="p-2 space-y-2 bg-white border-1 border-neutral-200 rounded-md shadow">
  2. <canvas class="container" id="testtesttest"></canvas>
  3. </div>
  4. <style>
  5. .container {
  6. height: 1200px;
  7. min-width: 310px;
  8. max-width: 800px;
  9. margin: 0 auto;
  10. }
  11. .loading {
  12. margin-top: 10em;
  13. text-align: center;
  14. color: gray;
  15. }
  16. </style>
  17. @push('scripts')
  18. <script>
  19. function test(){
  20. fetch('https://unpkg.com/us-atlas/states-10m.json').then((r) => r.json()).then((us) => {
  21. const nation = topojson.feature(us, us.objects.nation).features[0];
  22. let states = topojson.feature(us, us.objects.states).features;
  23. states = states.map((d) => ({feature: d, value: Math.random() * 10}));
  24. const chart = new Chart(document.getElementById("testtesttest").getContext("2d"), {
  25. type: 'choropleth',
  26. data: {
  27. datasets: [{
  28. data: states,
  29. }]
  30. }
  31. });
  32. });
  33. }
  34. test();
  35. </script>
  36. @endpush

这将生成以下Map:

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

tmb3ates

tmb3ates1#

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

    • 在我看来,您的示例中可能需要labels字段**
  1. labels: statesFeatures.map((d) => d.properties.name)
  2. or
  3. 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插件。关闭后,它开始工作。

相关问题