CountryMap.stories.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { useEffect, useState } from 'react';
  20. import {
  21. JsonObject,
  22. seed,
  23. SuperChart,
  24. SequentialD3,
  25. useTheme,
  26. } from '@superset-ui/core';
  27. import CountryMapChartPlugin, {
  28. countries,
  29. } from '@superset-ui/legacy-plugin-chart-country-map';
  30. import { withResizableChartDemo } from '../../../shared/components/ResizableChartDemo';
  31. new CountryMapChartPlugin().configure({ key: 'country-map' }).register();
  32. export default {
  33. title: 'Legacy Chart Plugins/legacy-plugin-chart-country-map',
  34. decorators: [withResizableChartDemo],
  35. component: SuperChart,
  36. parameters: {
  37. initialSize: { width: 500, height: 300 },
  38. },
  39. };
  40. function generateData(geojson: JsonObject) {
  41. return geojson.features.map(
  42. (feat: {
  43. properties: { ISO: string };
  44. type: string;
  45. geometry: JsonObject;
  46. }) => ({
  47. metric: Math.round(Number(seed(feat.properties.ISO)()) * 10000) / 100,
  48. country_id: feat.properties.ISO,
  49. }),
  50. );
  51. }
  52. export const BasicCountryMapStory = (
  53. {
  54. country,
  55. colorSchema,
  56. }: {
  57. country: string;
  58. colorSchema: string;
  59. },
  60. { width, height }: { width: number; height: number },
  61. ) => {
  62. const theme = useTheme();
  63. const [data, setData] = useState<JsonObject>();
  64. useEffect(() => {
  65. const controller = new AbortController();
  66. const { signal } = controller;
  67. fetch(countries[country as keyof typeof countries], { signal })
  68. .then(resp => resp.json())
  69. .then(geojson => {
  70. setData(generateData(geojson));
  71. });
  72. return () => {
  73. controller.abort();
  74. };
  75. }, [country]);
  76. if (!data) {
  77. return (
  78. <div
  79. style={{
  80. color: theme.colorTextLabel,
  81. textAlign: 'center',
  82. padding: 20,
  83. }}
  84. >
  85. Loading...
  86. </div>
  87. );
  88. }
  89. return (
  90. <SuperChart
  91. chartType="country-map"
  92. width={width}
  93. height={height}
  94. queriesData={[{ data }]}
  95. formData={{
  96. linearColorScheme: colorSchema,
  97. numberFormat: '.3s',
  98. selectCountry: country,
  99. }}
  100. />
  101. );
  102. };
  103. BasicCountryMapStory.args = {
  104. country: 'finland',
  105. colorSchema: 'schemeOranges',
  106. };
  107. BasicCountryMapStory.argTypes = {
  108. country: {
  109. control: 'select',
  110. options: Object.keys(countries),
  111. },
  112. colorSchema: {
  113. control: 'select',
  114. options: SequentialD3.map(x => x.id),
  115. description: 'Choose a color schema',
  116. defaultValue: 'schemeOranges',
  117. },
  118. };